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

valueObject() public method

Format values for the API
public valueObject ( mixed $value, boolean $exclude = false, integer $meaning = null ) : array
$value mixed
$exclude boolean [optional] If true, value will be excluded from datastore indexes.
$meaning integer [optional] The Meaning value. Maintained only for backwards compatibility.
return array
    public function valueObject($value, $exclude = false, $meaning = null)
    {
        switch (gettype($value)) {
            case 'boolean':
                $propertyValue = ['booleanValue' => $value];
                break;
            case 'integer':
                $propertyValue = ['integerValue' => $value];
                break;
            case 'double':
                $propertyValue = ['doubleValue' => $value];
                break;
            case 'string':
                $propertyValue = ['stringValue' => $value];
                break;
            case 'array':
                if (!empty($value) && $this->isAssoc($value)) {
                    $propertyValue = $this->convertArrayToEntityValue($value);
                } else {
                    $propertyValue = $this->convertArrayToArrayValue($value);
                }
                break;
            case 'object':
                $propertyValue = $this->objectProperty($value);
                break;
            case 'resource':
                $content = stream_get_contents($value);
                $propertyValue = ['blobValue' => $this->encode ? base64_encode($content) : $content];
                break;
            case 'NULL':
                $propertyValue = ['nullValue' => null];
                break;
                //@codeCoverageIgnoreStart
            //@codeCoverageIgnoreStart
            case 'unknown type':
                throw new InvalidArgumentException(sprintf('Unknown type for `%s', $content));
                break;
            default:
                throw new InvalidArgumentException(sprintf('Invalid type for `%s', $content));
                break;
                //@codeCoverageIgnoreEnd
        }
        if ($exclude) {
            $propertyValue['excludeFromIndexes'] = true;
        }
        if ($meaning) {
            $propertyValue['meaning'] = $meaning;
        }
        return $propertyValue;
    }

Usage Example

Beispiel #1
0
 /**
  * Format bound values for the API
  *
  * @param string $bindingType Either named or positional bindings.
  * @param array $bindings The bindings to map
  * @return array
  */
 private function mapBindings($bindingType, array $bindings)
 {
     $res = [];
     foreach ($bindings as $key => $binding) {
         $value = $this->entityMapper->valueObject($binding);
         if ($bindingType === self::BINDING_NAMED) {
             $res[$key] = ['value' => $value];
         } else {
             $res[] = ['value' => $value];
         }
     }
     return $res;
 }
All Usage Examples Of Google\Cloud\Datastore\EntityMapper::valueObject