/*
Default formatter for a collection of objects

Implements FormatterInterface::formatCollection($collection)
773542
public function formatCollection($collection, $format = 'json')
    {
        $items = array();

        foreach ($collection as $item) {
            $items[] = $item;
        }

        $context = new SerializationContext();
        $context->setSerializeNull(true);
        $context->enableMaxDepthChecks(true);

        return $this->getSerializer()->serialize($items, 'json', $context);
    }
1355833
public function renderFormFromCollection(ArgumentCollection $collection, $formName = "thelia.empty", $driverCode = null, $addHiddenFields = false)
    {
        $i = 0;
        $content = "";

        // Ensure that the template definition is on the BO
        $this->parser->setTemplateDefinition(TemplateHelper::getInstance()->getActiveAdminTemplate());

        /** @var \IndexEngine\Driver\Configuration\ArgumentInterface $argument */
        foreach ($collection->getArguments() as $argument) {
            if ($argument instanceof ParserAwareArgumentInterface) {
                $argument->setParser($this->parser);
            }

            $formattedTitle = $this->formatTitle($argument->getName());

            $content .= $this->parser->render("form-field/render-form-field.html", [
                "driver_code" => $driverCode,
                "form_name" => $formName,
                "form_field" => $argument->getName(),
                "argument" => $argument,
                "is_vector" => $argument instanceof VectorArgumentInterface,
                "is_view_builder" => $argument instanceof ViewBuilderInterface,
                "is_field_count_even" => $i % 2 === 0,
                "field_count" => $i++,
                "formatted_title" => $formattedTitle,
                "add_hidden_fields" => $addHiddenFields,
            ]);

            $addHiddenFields = false;
        }

        return $content;
    }
21509
public function stylesheets(string $collection, ?string $format = null) : string {
        return $this->serve($collection, 'stylesheets', $format);
    }
104720
private function initCollection(string $collection, string $class = null): void
    {
        $this->$collection = new Collection();
        $this->$collection->macroObject('createItem',
            function () use ($class): ?Assignable {
                if (null === $class) {
                    return null;
                }
                if (!class_exists($class)) {
                    throw new InvalidArgumentException(sprintf('Can not load class: %s', $class));
                }
                $item = new $class;
                if (!$item instanceof Assignable) {
                    throw new InvalidArgumentException(sprintf('%s must implement Assignable', $class));
                }

                return $item;
            });
    }
396985
public function setCollection($collection)
    {
        $this->collection = (array) $collection;
        $this->setCount(count($this->collection));

        return $this;
    }
396400
private function initFormatters()
    {
        $formatter = $this->getFormatter();

        $formatter->setStyle('warning', new OutputFormatterStyle('black', 'yellow'));
        $formatter->setStyle('error',   new OutputFormatterStyle('black', 'red', ['bold']));
        $formatter->setStyle('aside',   new OutputFormatterStyle('blue'));
        $formatter->setStyle('strong',  new OutputFormatterStyle(null, null, ['bold']));
        $formatter->setStyle('return',  new OutputFormatterStyle('cyan'));
        $formatter->setStyle('urgent',  new OutputFormatterStyle('red'));
        $formatter->setStyle('hidden',  new OutputFormatterStyle('black'));

        // Visibility
        $formatter->setStyle('public',    new OutputFormatterStyle(null, null, ['bold']));
        $formatter->setStyle('protected', new OutputFormatterStyle('yellow'));
        $formatter->setStyle('private',   new OutputFormatterStyle('red'));
        $formatter->setStyle('global',    new OutputFormatterStyle('cyan', null, ['bold']));
        $formatter->setStyle('const',     new OutputFormatterStyle('cyan'));
        $formatter->setStyle('class',     new OutputFormatterStyle('blue', null, ['underscore']));
        $formatter->setStyle('function',  new OutputFormatterStyle(null));
        $formatter->setStyle('default',   new OutputFormatterStyle(null));

        // Types
        $formatter->setStyle('number',   new OutputFormatterStyle('magenta'));
        $formatter->setStyle('string',   new OutputFormatterStyle('green'));
        $formatter->setStyle('bool',     new OutputFormatterStyle('cyan'));
        $formatter->setStyle('keyword',  new OutputFormatterStyle('yellow'));
        $formatter->setStyle('comment',  new OutputFormatterStyle('blue'));
        $formatter->setStyle('object',   new OutputFormatterStyle('blue'));
        $formatter->setStyle('resource', new OutputFormatterStyle('yellow'));
    }
355947
public function factory($collection)
    {
        if ($collection instanceof Collection) {
            $collectionName = $collection->getName();
        } else {
            $collectionName = $collection;
        }

        if (!isset($this->collections[$collectionName])) {
            if (!($collection instanceof Collection)) {
                $collection = Norm::createCollection(array(
                    'name' => $collection,
                    'connection' => $this,
                ));

                $this->applyHook('norm.after.factory', $collection);
            }

            $this->collections[$collectionName] = $collection;
        }

        return $this->collections[$collectionName];
    }
1153109
public function renderAllDivisionsHeader(DataCollection $collection) : void
    {
        $division = $collection->getDefaultProperties();
        $ua       = $division->getUserAgents()[0];

        if (empty($ua->getProperties())) {
            return;
        }

        $defaultproperties = $ua->getProperties();
        $properties        = array_merge(
            ['PropertyName', 'MasterParent', 'LiteMode', 'Parent'],
            array_keys($defaultproperties)
        );

        $values = [];

        foreach ($properties as $property) {
            if (!isset($this->outputProperties[$property])) {
                $this->outputProperties[$property] = $this->filter->isOutputProperty((string) $property, $this);
            }

            if (!$this->outputProperties[$property]) {
                continue;
            }

            $values[] = $this->formatter->formatPropertyName((string) $property);
        }

        fwrite($this->file, implode(',', $values) . PHP_EOL);
    }
555720
public function setCollection($collection)
    {
        if (!$this->loaded) {
            $this->load();
        }

        $this->collectionInfo->setCollection($collection);
        return $this;
    }
778485
public function setFormattedImages(FormattedImageCollection $formattedImages)
    {
        return parent::setFormattedImages($formattedImages->map(function (FormattedImage $formattedImage) {
            return $formattedImage->setImage($this);
        }));
    }
538530
public function renderAllDivisionsHeader(DataCollection $collection) : void
    {
        foreach ($this->writers as $writer) {
            $writer->renderAllDivisionsHeader($collection);
        }
    }
1373148
public function transform($collection)
    {
        if (null === $collection) {
            return null;
        }

        if (!($collection instanceof Collection)) {
            throw new UnexpectedTypeException($collection, 'Doctrine\Common\Collection\Collection');
        }

        $results = array();
        foreach ($collection as $entity) {
            $results[] = $this->accessor->getValue($entity, $this->identifier);
        }

        //Here, do not put the result in the cache because we must check the value in
        //reverseTransform (by QueryBuilder)

        return $results;
    }
717861
protected function methodToAction($method, $collection = false)
    {
        if ($method == 'GET' && $collection) {
            $method = $method . '-collection';
        }
        return isset($this->methodToAction[$method]) ? $this->methodToAction[$method] : $this->defaultAction;
    }
1361547
public function bulkSave($collection = [])
    {
        $params = [];

        $defaultIndex = $this->connection->getDefaultIndex();

        foreach ($collection as $item) {
            $modelIndex = $item->getDocumentIndex();

            $params['body'][] = [
                'index' => [
                    '_id'    => $item->getKey(),
                    '_type'  => $item->getDocumentType(),
                    '_index' => $modelIndex ? $modelIndex : $defaultIndex,
                ],
            ];
            $params['body'][] = $item->getDocumentData();
        }

        return $this->connection->bulkStatement($params);
    }
410339
protected function getCollection()
    {
        $collection = [];

        $class = $this->getCollectionClassName();
        if ($class) {
            /** @var Collection $collection */
            $collection = new $class();
            $collection->setModel($this->class);
            $collection->setFormatter($this);
        }

        return $collection;
    }
803145
private function formatOnlyTwo($collection)
    {
        return $this->translator->trans('only_two', [
            '%first%' => (string) $collection[0],
            '%second%' => (string) $collection[1],
        ], $this->catalogue);
    }
17047
public function setCollection($collection)
    {
        $this->collection = $collection;

        $this->collection->setCurPage($this->getCurrentPage());

        // we need to set pagination only if passed value integer and more that 0
        $limit = (int)$this->getLimit();
        if ($limit) {
            $this->collection->setPageSize($limit);
        }
        if ($this->getCurrentOrder()) {
            $this->collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        }

        return $this;
    }