<commit_before><?php
/**
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace GDS\Mapper;
use GDS\Entity;
use GDS\Property\Geopoint;
use GDS\Schema;

/**
 * Mapper for Datastore API v1, REST
 */
class RESTv1 extends \GDS\Mapper
{

    /**
     * This is the DateTime::format string required to support Datastore timestamps
     *
     * A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds. Example: "2014-10-02T15:01:23.045123456Z".
     */
    const DATETIME_FORMAT = 'Y-m-d\TH:i:s.u\Z';

    /**
     * Auto detect & extract a value
     *
     * @todo Support remaining types for auto extraction: keyValue, blobValue, entityValue
     *
     * @param object $obj_property
     * @return mixed
     */
    protected function extractAutoDetectValue($obj_property)
    {
        if(isset($obj_property->stringValue)) {
            return $obj_property->stringValue;
        }
        if(isset($obj_property->integerValue)) {
            return $obj_property->integerValue;
        }
        if(isset($obj_property->doubleValue)) {
            return $obj_property->doubleValue;
        }
        if(isset($obj_property->booleanValue)) {
            return $obj_property->booleanValue;
        }
        if(isset($obj_property->timestampValue)) {
            return $this->extractDatetimeValue($obj_property);
        }
        if(isset($obj_property->geoPointValue)) {
            return $this->extractGeopointValue($obj_property);
        }
        if(isset($obj_property->arrayValue)) {
            return $this->extractStringListValue($obj_property);
        }
        if(property_exists($obj_property, 'nullValue')) {
            return null;
        }
    }

    /**
     * Extract a datetime value
     *
     * We will lose accuracy
     * - past seconds in version 3.0
     * - past microseconds (down from nanoseconds) in version 4.0
     *
     * @param $obj_property
     * @return mixed
     */
    protected function extractDatetimeValue($obj_property)
    {
        $arr_matches = [];
        if(preg_match('/(.{19})\.?(\d{0,6}).*Z/', $obj_property->timestampValue, $arr_matches) > 0) {
            $obj_dtm = new \DateTime($arr_matches[1] . '.' . $arr_matches[2] . 'Z');
        } else {
            $obj_dtm = new \DateTime($obj_property->timestampValue);
        }
        return $obj_dtm;
    }

    /**
     * Extract a String List value
     *
     * @param $obj_property
     * @throws \Exception
     * @return array
     */
    protected function extractStringListValue($obj_property)
    {
        $arr_values = [];
        foreach((array)$obj_property->arrayValue->values as $obj_value) {
            if(isset($obj_value->stringValue)) {
                $arr_values[] = $obj_value->stringValue;
            }
        }
        return $arr_values;
    }

    /**
     * Extract a Geopoint value
     *
     * @param $obj_property
     * @return Geopoint
     */
    protected function extractGeopointValue($obj_property)
    {
        return new Geopoint($obj_property->geoPointValue->latitude, $obj_property->geoPointValue->longitude);
    }

    /**
     * Map a single result out of the Raw response data array FROM Google TO a GDS Entity<commit_msg>added check for blank string list<commit_after><?php
/**
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace GDS\Mapper;
use GDS\Entity;
use GDS\Property\Geopoint;
use GDS\Schema;

/**
 * Mapper for Datastore API v1, REST
 */
class RESTv1 extends \GDS\Mapper
{

    /**
     * This is the DateTime::format string required to support Datastore timestamps
     *
     * A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds. Example: "2014-10-02T15:01:23.045123456Z".
     */
    const DATETIME_FORMAT = 'Y-m-d\TH:i:s.u\Z';

    /**
     * Auto detect & extract a value
     *
     * @todo Support remaining types for auto extraction: keyValue, blobValue, entityValue
     *
     * @param object $obj_property
     * @return mixed
     */
    protected function extractAutoDetectValue($obj_property)
    {
        if(isset($obj_property->stringValue)) {
            return $obj_property->stringValue;
        }
        if(isset($obj_property->integerValue)) {
            return $obj_property->integerValue;
        }
        if(isset($obj_property->doubleValue)) {
            return $obj_property->doubleValue;
        }
        if(isset($obj_property->booleanValue)) {
            return $obj_property->booleanValue;
        }
        if(isset($obj_property->timestampValue)) {
            return $this->extractDatetimeValue($obj_property);
        }
        if(isset($obj_property->geoPointValue)) {
            return $this->extractGeopointValue($obj_property);
        }
        if(isset($obj_property->arrayValue)) {
            return $this->extractStringListValue($obj_property);
        }
        if(property_exists($obj_property, 'nullValue')) {
            return null;
        }
    }

    /**
     * Extract a datetime value
     *
     * We will lose accuracy
     * - past seconds in version 3.0
     * - past microseconds (down from nanoseconds) in version 4.0
     *
     * @param $obj_property
     * @return mixed
     */
    protected function extractDatetimeValue($obj_property)
    {
        $arr_matches = [];
        if(preg_match('/(.{19})\.?(\d{0,6}).*Z/', $obj_property->timestampValue, $arr_matches) > 0) {
            $obj_dtm = new \DateTime($arr_matches[1] . '.' . $arr_matches[2] . 'Z');
        } else {
            $obj_dtm = new \DateTime($obj_property->timestampValue);
        }
        return $obj_dtm;
    }

    /**
     * Extract a String List value
     *
     * @param $obj_property
     * @throws \Exception
     * @return array
     */
    protected function extractStringListValue($obj_property)
    {
        $arr_values = [];

        if (!isset($obj_property->arrayValue->values)) {
            return $arr_values;
        }

        foreach((array)$obj_property->arrayValue->values as $obj_value) {
            if(isset($obj_value->stringValue)) {
                $arr_values[] = $obj_value->stringValue;
            }
        }
        return $arr_values;
    }

    /**
     * Extract a Geopoint value
     *
     * @param $obj_property
     * @return Geopoint
     */
    protected function extractGeopointValue($obj_property)
    {
        return new Geopoint($obj_property->geoPointValue->latitude, $obj_property->geoPointValue->longitude);
    }

    /**
     * Map a single result out of the Raw response data array FROM Google TO a GDS Entity
{'fraction_non_alphanumeric': 0.11408246607825057, 'fraction_numerical': 0.012646555131076275, 'mean_word_length': 2.4044843049327356, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7026558', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2180, 'n_words': 552}