Google\Cloud\Datastore\EntityMapper::convertValue PHP Method

convertValue() public method

Convert a Datastore value object to a simple value
public convertValue ( string $type, mixed $value ) : mixed
$type string The value type
$value mixed The value
return mixed
    public function convertValue($type, $value)
    {
        $result = null;
        switch ($type) {
            case 'nullValue':
                $result = null;
                break;
            case 'booleanValue':
                $result = (bool) $value;
                break;
            case 'integerValue':
                $result = $this->returnInt64AsObject ? new Int64((string) $value) : (int) $value;
                break;
            case 'doubleValue':
                $result = (double) $value;
                break;
            case 'timestampValue':
                $result = \DateTimeImmutable::createFromFormat(self::DATE_FORMAT, $value);
                if (!$result) {
                    $result = \DateTimeImmutable::createFromFormat(self::DATE_FORMAT_NO_MS, $value);
                }
                break;
            case 'keyValue':
                $namespaceId = isset($value['partitionId']['namespaceId']) ? $value['partitionId']['namespaceId'] : null;
                $result = new Key($this->projectId, ['path' => $value['path'], 'namespaceId' => $namespaceId]);
                break;
            case 'stringValue':
                $result = $value;
                break;
            case 'blobValue':
                if ($this->isEncoded($value)) {
                    $value = base64_decode($value);
                }
                $result = new Blob($value);
                break;
            case 'geoPointValue':
                $value += ['latitude' => null, 'longitude' => null];
                $result = new GeoPoint($value['latitude'], $value['longitude']);
                break;
            case 'entityValue':
                $props = $this->responseToEntityProperties($value['properties'])['properties'];
                if (isset($value['key'])) {
                    $namespaceId = isset($value['key']['partitionId']['namespaceId']) ? $value['key']['partitionId']['namespaceId'] : null;
                    $key = new Key($this->projectId, ['path' => $value['key']['path'], 'namespaceId' => $namespaceId]);
                    $result = new Entity($key, $props, ['populatedByService' => true]);
                } else {
                    $result = [];
                    foreach ($value['properties'] as $key => $property) {
                        $result[$key] = $this->getPropertyValue($property);
                    }
                }
                break;
            case 'arrayValue':
                $result = [];
                if (array_key_exists('values', $value)) {
                    foreach ($value['values'] as $val) {
                        $result[] = $this->getPropertyValue($val);
                    }
                }
                break;
            default:
                throw new RuntimeException(sprintf('Unrecognized value type %s. Please ensure you are using the latest version of google/cloud.', $type));
                break;
        }
        return $result;
    }

Usage Example

 public function testReturnsInt64AsObject()
 {
     $int = '914241242';
     $mapper = new EntityMapper('foo', true, true);
     $res = $mapper->convertValue('integerValue', $int);
     $this->assertInstanceOf(Int64::class, $res);
     $this->assertEquals($int, $res->get());
 }