/** * @param Serializer $serializer * @param array $value * * @return mixed */ protected static function getTypedValue(Serializer $serializer, array $value) { foreach ($value as &$v) { $v = $serializer->unserialize($v); } return $value; }
/** * @param Serializer $serializer * @param string $className * @param array $value * * @return object */ public static function unserialize(Serializer $serializer, $className, array $value) { $ref = new ReflectionClass($className); foreach ($value as &$v) { if (is_array($v)) { $v = $serializer->unserialize($v); } } return $ref->newInstanceArgs($value); }
/** * @param Serializer $serializer * @param string $className * @param array $value * * @return mixed */ public static function unserialize(Serializer $serializer, $className, array $value) { $data = $serializer->unserialize($value[Serializer::SCALAR_VALUE]); /* @var SplFixedArray $instance */ $ref = new ReflectionClass($className); $instance = $ref->newInstanceWithoutConstructor(); $instance->setSize(count($data)); foreach ($data as $k => $v) { $instance[$k] = $v; } return $instance; }
/** * Extract the data from an object. * * @param mixed $value * * @return array */ protected function serializeObject($value) { if ($value instanceof \Illuminate\Database\Eloquent\Collection) { $items = []; foreach ($value as $v) { $items[] = $this->serializeObject($v); } return [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; } if ($value instanceof \Illuminate\Contracts\Pagination\Paginator) { $items = []; foreach ($value->items() as $v) { $items[] = $this->serializeObject($v); } return [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; } if (\is_subclass_of($value, Model::class, true)) { $stdClass = (object) $value->attributesToArray(); $data = $this->serializeData($stdClass); $data[self::CLASS_IDENTIFIER_KEY] = \get_class($value); $methods = $this->getRelationshipAsPropertyName($value, get_class($value), new ReflectionClass($value)); if (!empty($methods)) { $data = \array_merge($data, $methods); } return $data; } return parent::serializeObject($value); }
/** * @param $value * * @return array */ protected function flattenObject($value) : array { $result = $this->serializer->serialize($value); return array_map(function ($v) { if ($v === true || $v === false) { return $v ? 1 : 0; } return $v; }, $result); }
/** * Extract the data from an object. * * @param mixed $value * * @return array */ 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); }
public function __construct() { parent::__construct(new XmlStrategy()); }
public function testSerializationOfAnArrayOfScalars() { $scalar = 'a string'; $serializer = new Serializer(new \NilPortugues\Serializer\Strategy\JsonStrategy()); $serialized = $serializer->serialize($scalar); $this->assertEquals($scalar, $serializer->unserialize($serialized)); }
/** * @param JsonApiTransformer $strategy */ public function __construct(JsonApiTransformer $strategy) { parent::__construct($strategy); }
/** * @param HalJsonTransformer $transformer * @param Router $router */ public function __construct(HalJsonTransformer $transformer, Router $router) { $this->mapUrls($transformer, $router); parent::__construct($transformer); }