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

CollectionFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 9 2
A setCollection() 0 5 1
A map() 0 3 1
A each() 0 5 1
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