1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiFaker\Factory; |
6
|
|
|
|
7
|
|
|
use VGirol\JsonApiFaker\Contract\FactoryContract; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Add "data" member to a factory. |
11
|
|
|
*/ |
12
|
|
|
trait HasData |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The "data" member |
17
|
|
|
* |
18
|
|
|
* @var ResourceIdentifierFactory|ResourceObjectFactory|CollectionFactory|null |
19
|
|
|
*/ |
20
|
|
|
public $data; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Flag to indicate that "data" member has been set. |
24
|
|
|
* This can distinguish "data" member set to null to unset "data" member. |
25
|
|
|
* |
26
|
|
|
* @var boolean |
27
|
|
|
*/ |
28
|
|
|
private $hasBeenSet = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set "data" member |
32
|
|
|
* |
33
|
|
|
* @param ResourceIdentifierFactory|ResourceObjectFactory|CollectionFactory|null $data |
34
|
|
|
* @return static |
35
|
|
|
*/ |
36
|
15 |
|
public function setData($data) |
37
|
|
|
{ |
38
|
15 |
|
$this->data = $data; |
39
|
15 |
|
$this->hasBeenSet = true; |
40
|
|
|
|
41
|
15 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Check if the "data" member has been set. |
46
|
|
|
* |
47
|
|
|
* @return boolean |
48
|
|
|
*/ |
49
|
3 |
|
public function dataHasBeenSet(): bool |
50
|
|
|
{ |
51
|
3 |
|
return $this->hasBeenSet; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Fill the "data" member with fake values |
56
|
|
|
* |
57
|
|
|
* @param integer $options Bitmask |
58
|
|
|
* @param integer $count In case of collection, it represents the number of resource object |
59
|
|
|
* or resource identifier to generate |
60
|
|
|
* |
61
|
|
|
* @return static |
62
|
|
|
*/ |
63
|
9 |
|
public function fakeData($options = null, $count = 3) |
64
|
|
|
{ |
65
|
9 |
|
if ($options === null) { |
66
|
1 |
|
$options = Options::FAKE_RESOURCE_OBJECT | Options::FAKE_COLLECTION; |
67
|
1 |
|
$count = 5; |
68
|
|
|
} |
69
|
|
|
|
70
|
9 |
|
$canBeNull = (($options & Options::FAKE_CAN_BE_NULL) == Options::FAKE_CAN_BE_NULL); |
71
|
|
|
|
72
|
9 |
|
if ($canBeNull) { |
73
|
1 |
|
$faker = \Faker\Factory::create(); |
74
|
|
|
|
75
|
1 |
|
if ($faker->boolean()) { |
76
|
1 |
|
$this->setData(null); |
77
|
|
|
|
78
|
1 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
9 |
|
$dataFactory = $this->fakeCreateFactoryForData($options, $count); |
83
|
9 |
|
$this->setData($dataFactory); |
84
|
|
|
|
85
|
9 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the factory used to fill the "data" member when creating fake values |
90
|
|
|
* |
91
|
|
|
* @param integer $options Bitmask |
92
|
|
|
* @param integer $count In case of collection, it represents the number of resource object |
93
|
|
|
* or resource identifier to generate |
94
|
|
|
* |
95
|
|
|
* @return FactoryContract |
96
|
|
|
*/ |
97
|
9 |
|
private function fakeCreateFactoryForData($options, $count) |
98
|
|
|
{ |
99
|
9 |
|
$isCollection = (($options & Options::FAKE_COLLECTION) == Options::FAKE_COLLECTION); |
100
|
9 |
|
$isRI = (($options & Options::FAKE_RESOURCE_IDENTIFIER) == Options::FAKE_RESOURCE_IDENTIFIER); |
101
|
|
|
|
102
|
9 |
|
if ($isCollection) { |
103
|
7 |
|
return (new CollectionFactory)->fake($options, $count); |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
if ($isRI) { |
107
|
2 |
|
return (new ResourceIdentifierFactory)->fake(); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return (new ResourceObjectFactory)->fake(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|