Exemple #1
0
 public function serializeCollection(Collection $collection, array $options = [])
 {
     $this->normalizeOptions($options);
     $xml = $this->initXml($options);
     if ($collection->any()) {
         $collection->rewind();
         $first = $collection->current();
         $inflector = $first->getService('inflector');
         if (!isset($options['root'])) {
             $arrayName = $inflector->pluralize($this->properElementName($options, ClassTools::getClassName($first)));
         } else {
             $arrayName = $options['root'];
         }
         $options['root'] = $inflector->singularize($arrayName);
         $xml->write($arrayName, ['type' => 'array'], function ($x) use($collection, $options) {
             foreach ($collection as $member) {
                 $this->writeModel($x, $member, $options);
             }
         });
     } else {
         $xml->write($this->properElementName($options, 'nullClasses'), ['type' => 'array']);
     }
     return $xml->markup();
 }
Exemple #2
0
 public function partialCollection(ModelCollection $collection, $partialName, array $options = [])
 {
     if (!$collection->any()) {
         return '';
     }
     if (isset($options['spacerTemplate'])) {
         $spacerTemplate = $options['spacerTemplate'];
         unset($options['spacerTemplate']);
     } else {
         $spacerTemplate = null;
     }
     $contents = '';
     foreach ($collection as $member) {
         $options['object'] = $member;
         $contents .= $this->partial($partialName, $options);
         if ($spacerTemplate) {
             $contents .= $this->partial($spacerTemplate);
         }
     }
     return $contents;
 }