1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiFaker\Factory; |
6
|
|
|
|
7
|
|
|
use VGirol\JsonApiFaker\Contract\CollectionContract; |
8
|
|
|
use VGirol\JsonApiFaker\Exception\JsonApiFakerException; |
9
|
|
|
use VGirol\JsonApiFaker\Messages; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Factory for collection of resource object (@see ResourceObjectFactory) |
13
|
|
|
* or resource identifier (@see ResourceIdentifierFactory). |
14
|
|
|
*/ |
15
|
|
|
class CollectionFactory extends BaseFactory implements CollectionContract |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Array of objects implementing ResourceObjectContract or ResourceIdentifierContract |
19
|
|
|
* |
20
|
|
|
* @var array|null |
21
|
|
|
*/ |
22
|
|
|
protected $array; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Sets the collection. |
26
|
|
|
* |
27
|
|
|
* @param array|null $collection An array of objects implementing ResourceObjectContract |
28
|
|
|
* or ResourceIdentifierContract |
29
|
|
|
* |
30
|
|
|
* @return static |
31
|
|
|
*/ |
32
|
42 |
|
public function setCollection($collection) |
33
|
|
|
{ |
34
|
42 |
|
$this->array = $collection; |
35
|
|
|
|
36
|
42 |
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the collection. |
41
|
|
|
* |
42
|
|
|
* The collection is an array of objects implementing ResourceObjectContract or ResourceIdentifierContract |
43
|
|
|
* |
44
|
|
|
* @return array|null |
45
|
|
|
*/ |
46
|
6 |
|
public function getCollection(): ?array |
47
|
|
|
{ |
48
|
6 |
|
return $this->array; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Exports the factory as an array. |
53
|
|
|
* |
54
|
|
|
* @return array|null |
55
|
|
|
* @throws JsonApiFakerException |
56
|
|
|
*/ |
57
|
33 |
|
public function toArray(): ?array |
58
|
|
|
{ |
59
|
33 |
|
if (!isset($this->array)) { |
60
|
3 |
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
30 |
|
return $this->map( |
64
|
|
|
/** |
65
|
|
|
* @param ResourceObjectFactory|ResourceIdentifierFactory $resource |
66
|
|
|
* |
67
|
|
|
* @return array|null |
68
|
|
|
*/ |
69
|
30 |
|
function ($resource) { |
70
|
24 |
|
return $resource->toArray(); |
71
|
30 |
|
} |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Apply a supplied function to every element of the collection. |
77
|
|
|
* |
78
|
|
|
* @param callable $callback |
79
|
|
|
* |
80
|
|
|
* @return static |
81
|
|
|
* @throws JsonApiFakerException |
82
|
|
|
*/ |
83
|
6 |
|
public function each($callback) |
84
|
|
|
{ |
85
|
6 |
|
if ($this->array === null) { |
86
|
3 |
|
throw new JsonApiFakerException(Messages::ERROR_COLLECTION_NOT_SET); |
87
|
|
|
} |
88
|
3 |
|
array_walk($this->array, $callback); |
89
|
|
|
|
90
|
3 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Applies the callback to the elements of the collection. |
95
|
|
|
* |
96
|
|
|
* @param Callable $callback |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
* @throws JsonApiFakerException |
100
|
|
|
*/ |
101
|
36 |
|
public function map($callback): array |
102
|
|
|
{ |
103
|
36 |
|
if ($this->array === null) { |
104
|
3 |
|
throw new JsonApiFakerException(Messages::ERROR_COLLECTION_NOT_SET); |
105
|
|
|
} |
106
|
|
|
|
107
|
33 |
|
return array_map($callback, $this->array); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Fill the collection with fake values (resource identifers or resource objects). |
112
|
|
|
* |
113
|
|
|
* @param integer $options |
114
|
|
|
* @param integer $count |
115
|
|
|
* |
116
|
|
|
* @return static |
117
|
|
|
*/ |
118
|
24 |
|
public function fake(int $options = 0, int $count = 5) |
119
|
|
|
{ |
120
|
24 |
|
if ($options === 0) { |
121
|
3 |
|
$options = Options::FAKE_RESOURCE_OBJECT; |
122
|
|
|
} |
123
|
24 |
|
$fn = (($options & Options::FAKE_RESOURCE_IDENTIFIER) == Options::FAKE_RESOURCE_IDENTIFIER) ? |
124
|
24 |
|
'resourceIdentifier' : 'resourceObject'; |
125
|
|
|
|
126
|
24 |
|
$collection = []; |
127
|
24 |
|
for ($i = 0; $i < $count; $i++) { |
128
|
24 |
|
$collection[] = $this->generator->{$fn}()->fake(); |
129
|
|
|
} |
130
|
|
|
|
131
|
24 |
|
return $this->setCollection($collection); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|