Example #1
0
 /**
  * @param object[] $objects All instances are expected to be of the same class.
  * @param Context  $context
  *
  * @return array
  */
 public function normalizeObjectCollection(array $objects, Context $context)
 {
     if (count($objects) === 0) {
         return [];
     }
     $class = ClassUtils::getClass(reset($object));
     foreach ($objects as $object) {
         if (!is_object($object)) {
             throw new \InvalidArgumentException(sprintf('All arguments are expected to be instances of objects, "%s" found.', gettype($object)));
         }
         if (!$object instanceof $class) {
             throw new \InvalidArgumentException(sprintf('All objects are expected to be an instance of "%s", an instance of "%s" found.', $class, ClassUtils::getClass($object)));
         }
     }
     // "Includes" are stateful, so make sure they are always explicitly set.
     $this->manager->parseIncludes($context->getIncludesAsString());
     return $this->manager->createData(new Collection($objects, $this->transformerRegistry->get($class)))->toArray();
 }