Completed
Push — master ( 883f28...097345 )
by Vincent
03:45 queued 11s
created

CollectionFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 20
dl 0
loc 85
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 0 3 1
A toArray() 0 9 2
A setCollection() 0 5 1
A fake() 0 14 4
A each() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker\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
     *
21
     * @return static
22
     */
23 12
    public function setCollection($collection)
24
    {
25 12
        $this->array = $collection;
26
27 12
        return $this;
28
    }
29
30
    /**
31
     * Undocumented function
32
     *
33
     * @return array|null
34
     */
35 11
    public function toArray(): ?array
36
    {
37 11
        if (!isset($this->array)) {
38 1
            return null;
39
        }
40
41 10
        return $this->map(
42
            function ($resource) {
43 8
                return $resource->toArray();
44 10
            }
45
        );
46
    }
47
48
    /**
49
     * Undocumented function
50
     *
51
     * @param callable $callback
52
     * @return static
53
     */
54 1
    public function each($callback)
55
    {
56 1
        array_walk($this->array, $callback);
57
58 1
        return $this;
59
    }
60
61
    /**
62
     * Undocumented function
63
     *
64
     * @param Callable $callback
65
     *
66
     * @return array
67
     */
68 11
    public function map($callback): array
69
    {
70 11
        return array_map($callback, $this->array);
71
    }
72
73
    /**
74
     * Undocumented function
75
     *
76
     * @return static
77
     */
78 6
    public function fake($options = null, $count = 5)
79
    {
80 6
        if (is_null($options)) {
81 1
            $options = self::FAKE_RESOURCE_OBJECT;
82
        }
83 6
        $class = (($options & self::FAKE_RESOURCE_IDENTIFIER) == self::FAKE_RESOURCE_IDENTIFIER) ?
84 6
            ResourceIdentifierFactory::class : ResourceObjectFactory::class;
85
86 6
        $collection = [];
87 6
        for ($i = 0; $i < $count; $i++) {
88 6
            $collection[] = (new $class)->fake();
89
        }
90
91 6
        return $this->setCollection($collection);
92
    }
93
}
94