|
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\Contract\ResourceIdentifierContract; |
|
9
|
|
|
use VGirol\JsonApiFaker\Contract\ResourceObjectContract; |
|
10
|
|
|
use VGirol\JsonApiFaker\Exception\JsonApiFakerException; |
|
11
|
|
|
use VGirol\JsonApiFaker\Messages; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Add "data" member to a factory. |
|
15
|
|
|
*/ |
|
16
|
|
|
trait HasData |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The "data" member |
|
20
|
|
|
* |
|
21
|
|
|
* @var ResourceIdentifierContract|ResourceObjectContract|CollectionContract|null |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $data; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Flag to indicate that "data" member has been set. |
|
27
|
|
|
* This can distinguish "data" member set to null to unset "data" member. |
|
28
|
|
|
* |
|
29
|
|
|
* @var boolean |
|
30
|
|
|
*/ |
|
31
|
|
|
private $hasBeenSet = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set "data" member |
|
35
|
|
|
* |
|
36
|
|
|
* @param ResourceIdentifierContract|ResourceObjectContract|CollectionContract|null $data |
|
37
|
|
|
* |
|
38
|
|
|
* @return static |
|
39
|
|
|
* @throws JsonApiFakerException |
|
40
|
|
|
*/ |
|
41
|
51 |
|
public function setData($data) |
|
42
|
|
|
{ |
|
43
|
51 |
|
if (($data !== null) |
|
44
|
51 |
|
&& (is_a($data, ResourceIdentifierContract::class) === false) |
|
45
|
51 |
|
&& (is_a($data, ResourceObjectContract::class) === false) |
|
46
|
51 |
|
&& (is_a($data, CollectionContract::class) === false) |
|
47
|
|
|
) { |
|
48
|
3 |
|
throw new JsonApiFakerException(Messages::SET_DATA_BAD_TYPE); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
48 |
|
$this->data = $data; |
|
52
|
48 |
|
$this->hasBeenSet = true; |
|
53
|
|
|
|
|
54
|
48 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get "data" member |
|
59
|
|
|
* |
|
60
|
|
|
* @return ResourceIdentifierContract|ResourceObjectContract|CollectionContract|null |
|
61
|
|
|
*/ |
|
62
|
24 |
|
public function getData() |
|
63
|
|
|
{ |
|
64
|
24 |
|
return $this->data; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Check if the "data" member has been set. |
|
69
|
|
|
* |
|
70
|
|
|
* @return boolean |
|
71
|
|
|
*/ |
|
72
|
9 |
|
public function dataHasBeenSet(): bool |
|
73
|
|
|
{ |
|
74
|
9 |
|
return $this->hasBeenSet; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Fill the "data" member with fake values |
|
79
|
|
|
* |
|
80
|
|
|
* @param integer $options Bitmask |
|
81
|
|
|
* @param integer $count In case of collection, it represents the number of resource object |
|
82
|
|
|
* or resource identifier to generate |
|
83
|
|
|
* |
|
84
|
|
|
* @return static |
|
85
|
|
|
* @throws JsonApiFakerException |
|
86
|
|
|
*/ |
|
87
|
30 |
|
public function fakeData(int $options = 0, int $count = 5) |
|
88
|
|
|
{ |
|
89
|
30 |
|
if ($options === 0) { |
|
90
|
3 |
|
$options = Options::FAKE_RESOURCE_OBJECT | Options::FAKE_COLLECTION; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
30 |
|
$canBeNull = (($options & Options::FAKE_CAN_BE_NULL) == Options::FAKE_CAN_BE_NULL); |
|
94
|
|
|
|
|
95
|
30 |
|
if ($canBeNull) { |
|
96
|
3 |
|
$faker = \Faker\Factory::create(); |
|
97
|
|
|
|
|
98
|
3 |
|
if ($faker->boolean()) { |
|
99
|
3 |
|
$this->setData(null); |
|
100
|
|
|
|
|
101
|
3 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
30 |
|
$dataFactory = $this->fakeCreateFactoryForData($options, $count); |
|
106
|
30 |
|
$this->setData($dataFactory); |
|
107
|
|
|
|
|
108
|
30 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get the factory used to fill the "data" member when creating fake values |
|
113
|
|
|
* |
|
114
|
|
|
* @param integer $options Bitmask |
|
115
|
|
|
* @param integer $count In case of collection, it represents the number of resource object |
|
116
|
|
|
* or resource identifier to generate |
|
117
|
|
|
* |
|
118
|
|
|
* @return ResourceIdentifierContract|ResourceObjectContract|CollectionContract |
|
119
|
|
|
*/ |
|
120
|
30 |
|
private function fakeCreateFactoryForData(int $options, int $count) |
|
121
|
|
|
{ |
|
122
|
30 |
|
$isCollection = (($options & Options::FAKE_COLLECTION) == Options::FAKE_COLLECTION); |
|
123
|
30 |
|
$isRI = (($options & Options::FAKE_RESOURCE_IDENTIFIER) == Options::FAKE_RESOURCE_IDENTIFIER); |
|
124
|
|
|
|
|
125
|
30 |
|
if ($isCollection) { |
|
126
|
24 |
|
return $this->generator |
|
127
|
24 |
|
->collection() |
|
128
|
24 |
|
->fake($options, $count); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
9 |
|
if ($isRI) { |
|
132
|
6 |
|
return $this->generator |
|
133
|
6 |
|
->resourceIdentifier() |
|
134
|
6 |
|
->fake(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
3 |
|
return $this->generator |
|
138
|
3 |
|
->resourceObject() |
|
139
|
3 |
|
->fake(); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|