Encode a value of type `object` as a Firestore value.

@param object $value
@return array
@throws \RuntimeException If an invalid object type is provided.
197160
private function encodeObjectValue($value)
    {
        if ($value instanceof \stdClass) {
            return $this->encodeAssociativeArrayValue((array) $value);
        }

        if ($value instanceof Blob) {
            return ['bytesValue' => (string) $value];
        }

        if ($value instanceof \DateTimeInterface) {
            return [
                'timestampValue' => [
                    'seconds' => $value->format('U'),
                    'nanos' => (int)($value->format('u') * 1000)
                ]
            ];
        }

        if ($value instanceof Timestamp) {
            return [
                'timestampValue' => [
                    'seconds' => $value->get()->format('U'),
                    'nanos' => $value->nanoSeconds()
                ]
            ];
        }

        if ($value instanceof GeoPoint) {
            return ['geoPointValue' => $value->point()];
        }

        if ($value instanceof DocumentReference || $value instanceof DocumentSnapshot) {
            return ['referenceValue' => $value->name()];
        }

        throw new \RuntimeException(sprintf(
            'Object of type %s cannot be encoded to a Firestore value type.',
            get_class($value)
        ));
    }
1288739
protected function castToStdClass($value)
    {
        if (is_object($value) && !$value instanceof \stdClass) {
            $value = get_object_vars($value);
        }

        return $this->castToObject($value);
    }
223948
protected function castValueAsJson($value)
    {
        $value = $this->asJson($value);

        if ($value === false) {
            throw new \RuntimeException(
                sprintf(
                    "Unable to encode value [%s] for element [%s] to JSON: %s.",
                    $this->getName(),
                    get_class($this),
                    json_last_error_msg()
                )
            );
        }

        return $value;
    }
440002
protected function convertType($value)
    {
        switch(true) {

            case is_bool($value):
                return new BoolType;

            case is_int($value):
                return new NumberType(['nativeType'=>'int']);

            case is_float($value):
                return new NumberType(['nativeType'=>'float']);

            case is_null($value):
                return new StringType(['canBeNull'=>true]);

            case $value instanceof DateTime:
                return new TemporalType;

            case $this->isSequence($value):
                return $this->createSequence($value);

            case is_object($value):
                return $this->createObject($value);

            case $this->hasKeysAndValues($value):
                return $this->createArrayType($value);

            default:
                return new StringType;
        }

    }
1096355
public static function allToArray($value)
    {
        if ($value instanceof DataSetInterface) {
            $value = $value->dump(true);
        } elseif ($value instanceof DataInterface) {
            $value = $value->dump(true);
        } elseif ($value instanceof \Traversable) {
            $value = iterator_to_array($value);
        } elseif (is_object($value)) {
            $value = get_object_vars($value);
        }

        if (is_array($value)) {
            foreach ($value as &$v) {
                $v = static::allToArray($v);
            }
        }

        return $value;
    }
655389
protected function transformObject($value)
    {
        if (! is_object($value)) {
            return;
        }
        if ($value instanceof JsonSerializable || $value instanceof StdClass) {
            return json_encode($value);
        }
        // If a toJson() method exists, we'll assume that
        // the object can cast itself automatically.
        if (method_exists($value, 'toJson')) {
            return $value;
        }
        // Otherwise, if the object doesn't even have a
        // __toString() method, we can't proceed.
        if (! method_exists($value, '__toString')) {
            throw new Exception('Cannot transform this object to JavaScript.');
        }

        return "'{$value}'";
    }
549137
private function setValue($value)
    {
        switch (gettype($value)) {
            case 'int':
            case 'integer':
                $this->attributes['type'] = 'int';

                return (int) $value;
            case 'real':
            case 'float':
            case 'double':
                $this->attributes['type'] = 'float';

                return (float) $value;
            case 'bool':
            case 'boolean':
                $this->attributes['type'] = 'bool';

                return (bool) $value;
            case 'object':
                if ($value instanceof Collection) {
                    $this->attributes['type'] = 'collection';

                    return json_encode($value);
                }
                $this->attributes['type'] = 'object';

                return json_encode($value);
            case 'array':
                $this->attributes['type'] = 'array';

                return json_encode($value);
            default:
                $this->attributes['type'] = 'string';

                return $value;
        }
    }
1221934
private static function encode($value, SplObjectStorage $objectIds)
    {
        $encoded = [$value];
        array_walk_recursive($encoded, function (&$value) use ($objectIds) {
            if (!is_object($value)) {
                return;
            }

            $isReference = isset($objectIds[$value]);

            $meta = [
                '#' => !$isReference ? $objectIds[$value] = count($objectIds) : $objectIds[$value],
                'fqn' => get_class($value),
            ];

            $value = ($isReference ? [] : self::encode(self::objectProperties($value), $objectIds)) + ['μ' => $meta];
        });

        return $encoded[0];
    }
17493
protected function extractObject($value)
    {
        if (empty($value)) {
            return (Object) [];
        }

        if ($value instanceof \Traversable) {
            $array = [];
            foreach ($value as $key => $value) {
                $array[$key] = $value;
            }
            return (Object) $array;
        }

        return (Object) $value;
    }
703223
public static function allToArray($value)
    {
        if ($value instanceof \Traversable) {
            $value = iterator_to_array($value);
        }

        if (is_object($value)) {
            $value = get_object_vars($value);
        }

        $value = (array) $value;

        foreach ($value as $k => $v) {
            if (!static::isValidValue($v)) {
                throw new \InvalidArgumentException('Value :' . $value . ' is invalid.');
            }
        }

        return $value;
    }
232293
public function convert($value)
	{
		if (is_array(($value))) {
			$retval = [];
			foreach ($value as $object) {
				$retval[] = $this->convert($object); // force sequential indexing
			}

			return $retval;
		} else if (is_object($value)) {
			return $this->convertObject($value);
		} else {
			return $value;
		}
	}
723443
protected function serializeObject($value)
    {
        if ($value instanceof Collection) {
            return $this->serializeEloquentCollection($value);
        }

        if ($value instanceof Paginator) {
            return $this->serializeEloquentPaginatedResource($value);
        }

        if (\is_subclass_of($value, Model::class, true)) {
            return $this->serializeEloquentModel($value);
        }

        return parent::serializeObject($value);
    }
8369
protected function encode($value)
    {
        try {
            return $this->encoders->getEncoderForValue($value)
                ->encode($value, new EncodingContext());
        } catch (EncodingException | InvalidArgumentException | OutOfBoundsException $ex) {
            throw new SerializationException('Failed to serialize value', 0, $ex);
        }
    }
72935
public function serialize($value)
    {
        $this->noMappingGuard();

        //LEGACY ARRAY SERIALIZATION
        if (\is_array($value) && !empty($value[Serializer::MAP_TYPE])) {
            $data = $this->legacySerializeArray($value);

            return $this->outputStrategy($data);
        }

        //HAL PAGINATION SERIALIZATION
        if (!empty($value[Serializer::CLASS_IDENTIFIER_KEY])) {
            if ($value[Serializer::CLASS_IDENTIFIER_KEY] === HalPagination::class) {
                foreach ($value[self::EMBEDDED_KEY][Serializer::SCALAR_VALUE] as $k => $v) {
                    $value[self::EMBEDDED_KEY][Serializer::SCALAR_VALUE][$k] = $this->serializeObject($v);
                }
            }
        }

        //HAL SERIALIZATION
        $data = $this->serializeObject($value);

        return $this->outputStrategy($data);
    }
197970
private function copyValue($value)
    {
        if (is_array($value)) {
            return $this->copyArray($value, true);
        }
        if (!is_object($value)) {
            return $value;
        }
        if ($value instanceof Collection) {
            return new ArrayCollection($this->copyArray($value->toArray(), false));
        }
        if ($value instanceof \Traversable) {
            throw new ClonerException(sprintf('Traversable class "%s" is not supported.', ClassUtils::getClass($value)));
        }

        return $this->cloneObject($value, false);
    }
609888
protected function castValue($value)
    {
        if (is_null($value)) {
            return $value;
        }

        if ($this->isElementOfDate() && $this->isDateCastable()) {
            return $this->fromDateTime($value);
        }

        switch ($this->getCast()) {
            case 'int':
            case 'integer':
                return (int) $value;
            case 'real':
            case 'float':
            case 'double':
                return (float) $value;
            case 'string':
                return (string) $value;
            case 'bool':
            case 'boolean':
                return (bool) $value;
            case 'object':
                return $this->fromJson($value, true);
            case 'array':
            case 'json':
                return $this->fromJson($value);
            case 'collection':
                return new Collection($this->fromJson($value));
            case 'comma':
                return $this->fromCommaSeparated($value);
            default:
                return $value;
        }
    }
913028
public function toArray($value)
    {
        if (!is_object($value)) {
            throw new TypeCastException('Is not object');
        }

        foreach ($this->rules as $class => $rule)
        {
            if (get_class($value) === $class) {
                return $this->createMap($class, $value);
            }
        }

        throw new TypeCastException('Rule not found');
    }