for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Chubbyphp\Serialization\Serializer\Field;
use Chubbyphp\Serialization\Accessor\AccessorInterface;
use Chubbyphp\Serialization\SerializerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
final class CollectionFieldSerializer implements FieldSerializerInterface
{
/**
* @var AccessorInterface
*/
private $accessor;
* @param AccessorInterface $accessor
public function __construct(AccessorInterface $accessor)
$this->accessor = $accessor;
}
* @param string $path
* @param Request $request
* @param object $object
* @param SerializerInterface|null $serializer
*
* @return mixed
public function serializeField(string $path, Request $request, $object, SerializerInterface $serializer = null)
$this->serializerOrException($serializer);
$collection = [];
foreach ($this->accessor->getValue($object) as $i => $childObject) {
$subPath = $path.'.['.$i.']';
$collection[$i] = $serializer->serialize($request, $childObject, $subPath);
$serializer
null
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:
function someFunction(A $objectMaybe = null) { if ($objectMaybe instanceof A) { $objectMaybe->doSomething(); } }
return $collection;
* @throws \RuntimeException
private function serializerOrException(SerializerInterface $serializer = null)
if (null === $serializer) {
throw new \RuntimeException(sprintf('Serializer needed: %s', SerializerInterface::class));
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: