Completed
Push — master ( a787fa...dc9af5 )
by Vincent
05:35
created

CollectionFactory::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiAssert\Factory;
6
7
class CollectionFactory extends BaseFactory
8
{
9
    /**
10
     * Array of ResourceObjectFactory or ResourceIdentifierFactory objects
11
     *
12
     * @var array
13
     */
14
    public $array;
15
16
    /**
17
     * Undocumented function
18
     *
19
     * @param array<ResourceIdentifierFactory>|array<ResourceObjectFactory> $collection
20
     * @return static
21
     */
22
    public function setCollection($collection)
23
    {
24
        $this->array = $collection;
25
26
        return $this;
27
    }
28
29
    public function toArray(): ?array
30
    {
31
        if (!isset($this->array)) {
32
            return null;
33
        }
34
35
        return $this->map(
36
            function ($resource) {
37
                return $resource->toArray();
38
            }
39
        );
40
    }
41
42
    /**
43
     * Undocumented function
44
     *
45
     * @param callable $callback
46
     * @return static
47
     */
48
    public function each($callback)
49
    {
50
        array_walk($this->array, $callback);
51
52
        return $this;
53
    }
54
55
    public function map($callback): array
56
    {
57
        return array_map($callback, $this->array);
58
    }
59
}
60