Exemplo n.º 1
0
            $collection = new Collection(['data' => [1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5]]);
            expect(Collection::toArray($collection))->toBe([1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5]);
        });
        it("converts objects which support __toString", function () {
            $stringable = Stub::classname();
            Stub::on($stringable)->method('__toString')->andReturn('hello');
            $collection = new Collection(['data' => [new $stringable()]]);
            expect(Collection::toArray($collection))->toBe(['hello']);
        });
        it("converts objects using handlers", function () {
            $handlable = Stub::classname();
            $handlers = [$handlable => function ($value) {
                return 'world';
            }];
            $collection = new Collection(['data' => [new $handlable()]]);
            expect(Collection::toArray($collection, compact('handlers')))->toBe(['world']);
        });
        it("doesn't convert unsupported objects", function () {
            $collection = new Collection(['data' => [(object) 'an object']]);
            expect(Collection::toArray($collection))->toEqual([(object) 'an object']);
        });
        it("converts nested collections", function () {
            $collection = new Collection(['data' => [1, 2, 3, new Collection(['data' => [4, 5, 6]])]]);
            expect(Collection::toArray($collection))->toBe([1, 2, 3, [4, 5, 6]]);
        });
        it("converts mixed nested collections & arrays", function () {
            $collection = new Collection(['data' => [1, 2, 3, [new Collection(['data' => [4, 5, 6]])]]]);
            expect(Collection::toArray($collection))->toBe([1, 2, 3, [[4, 5, 6]]]);
        });
    });
});
Exemplo n.º 2
0
 /**
  * Exports a `Collection` object to another format.
  *
  * The supported values of `format` depend on the registered handlers.
  *
  * Once the appropriate handlers are registered, a `Collection` instance can be converted into
  * any handler-supported format, i.e.:
  *
  * ```php
  * $collection->to('json'); // returns a JSON string
  * $collection->to('xml'); // returns an XML string
  * ```
  *
  * @param  string $format  By default the only supported value is `'array'`. However, additional
  *                         format handlers can be registered using the `formats()` method.
  * @param  array  $options Options for converting the collection.
  * @return mixed           The converted collection.
  */
 public function to($format, $options = [])
 {
     $defaults = ['cast' => true];
     $options += $defaults;
     $data = $options['cast'] ? Collection::toArray($this, $options) : $this;
     if (is_callable($format)) {
         return $format($data, $options);
     } elseif ($formatter = Collection::formats($format)) {
         return $formatter($data, $options);
     }
     return $data;
 }
Exemplo n.º 3
0
 /**
  * Converts the data in the record set to a different format, i.e. an array.
  *
  * @param string $format  Currently only `array`.
  * @param array  $options Options for converting:
  *                        - `'indexed'` _boolean_: Allows to control how converted data of nested collections
  *                        is keyed. When set to `true` will force indexed conversion of nested collection
  *                        data. By default `false` which will only index the root level.
  * @return mixed
  */
 public function to($format, $options = [])
 {
     $defaults = ['embed' => true, 'basePath' => null];
     $options += $defaults;
     if ($options['embed'] === true) {
         $options['embed'] = $this->hierarchy();
     }
     $schema = $this->schema();
     $tree = $schema->treeify($options['embed']);
     $basePath = $options['basePath'];
     $result = [];
     if ($schema->locked()) {
         $fields = array_merge($schema->fields($options['basePath']), $schema->relations());
     } else {
         $fields = array_keys($this->_data);
     }
     foreach ($fields as $field) {
         $path = $basePath ? $basePath . '.' . $field : $field;
         $rel = null;
         if ($schema->hasRelation($path)) {
             $rel = $schema->relation($path);
             if (!$rel->embedded()) {
                 if (!array_key_exists($field, $tree)) {
                     continue;
                 }
                 $options['embed'] = $tree[$field];
             }
         }
         if (!$this->has($field)) {
             continue;
         }
         $value = $this[$field];
         if ($value instanceof Document) {
             $options['basePath'] = $rel && $rel->embedded() ? $value->basePath() : '';
             $result[$field] = $value->to($format, $options);
         } elseif ($value instanceof Traversable) {
             $options['basePath'] = $rel && $rel->embedded() ? $value->basePath() : '';
             $result[$field] = Collection::toArray($value, $options);
         } else {
             $options['basePath'] = $path;
             $result[$field] = $schema->has($options['basePath']) ? $schema->format($format, $options['basePath'], $value) : $value;
         }
     }
     return $result;
 }
Exemplo n.º 4
0
 /**
  * Converts the current state of the data structure to an array.
  *
  * @param  array $options The options array.
  * @return array          Returns the array value of the data in this `Collection`.
  */
 public function data($options = [])
 {
     return array_values(Collection::toArray($this, $options));
 }
Exemplo n.º 5
0
 /**
  * Converts the data in the record set to a different format, i.e. an array.
  *
  * @param string $format  Currently only `array`.
  * @param array  $options Options for converting:
  *                        - `'indexed'` _boolean_: Allows to control how converted data of nested collections
  *                        is keyed. When set to `true` will force indexed conversion of nested collection
  *                        data. By default `false` which will only index the root level.
  * @return mixed
  */
 public function to($format, $options = [])
 {
     $defaults = ['embed' => true];
     $options += $defaults;
     $schema = static::schema();
     $tree = $schema->treeify($options['embed']);
     $result = [];
     foreach ($this as $field => $value) {
         if ($schema->hasRelation($field)) {
             if (!array_key_exists($field, $tree)) {
                 continue;
             }
             $options['embed'] = $tree[$field];
         }
         if ($value instanceof Model) {
             $result[$field] = $value->to($format, $options);
         } elseif ($value instanceof ArrayAccess) {
             $result[$field] = Collection::toArray($value, $options);
         } else {
             $result[$field] = static::schema()->format($format, $field, $value, $options);
         }
     }
     return $result;
 }