|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiFaker\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use VGirol\JsonApiFaker\Members; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Factory for an entire document. |
|
11
|
|
|
*/ |
|
12
|
|
|
class DocumentFactory extends BaseFactory |
|
13
|
|
|
{ |
|
14
|
|
|
use HasData; |
|
|
|
|
|
|
15
|
|
|
use HasErrors; |
|
16
|
|
|
use HasLinks; |
|
17
|
|
|
use HasMeta; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The collection of included resources |
|
21
|
|
|
* |
|
22
|
|
|
* @var CollectionFactory |
|
23
|
|
|
*/ |
|
24
|
|
|
public $included; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The jsonapi object |
|
28
|
|
|
* |
|
29
|
|
|
* @var JsonapiFactory |
|
30
|
|
|
*/ |
|
31
|
|
|
public $jsonapi; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Sets the included collection. |
|
35
|
|
|
* |
|
36
|
|
|
* @param CollectionFactory $included |
|
37
|
|
|
* |
|
38
|
|
|
* @return static |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function setIncluded($included) |
|
41
|
|
|
{ |
|
42
|
2 |
|
$this->included = $included; |
|
43
|
|
|
|
|
44
|
2 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Sets the jsonapi object. |
|
49
|
|
|
* |
|
50
|
|
|
* @param JsonapiFactory $jsonapi |
|
51
|
|
|
* |
|
52
|
|
|
* @return static |
|
53
|
|
|
*/ |
|
54
|
4 |
|
public function setJsonapi($jsonapi) |
|
55
|
|
|
{ |
|
56
|
4 |
|
$this->jsonapi = $jsonapi; |
|
57
|
|
|
|
|
58
|
4 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritDoc |
|
63
|
|
|
* @return array<string,mixed> |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function toArray(): array |
|
66
|
|
|
{ |
|
67
|
2 |
|
$json = []; |
|
68
|
|
|
|
|
69
|
2 |
|
if (isset($this->meta)) { |
|
70
|
2 |
|
$json[Members::META] = $this->meta; |
|
71
|
|
|
} |
|
72
|
2 |
|
if (isset($this->links)) { |
|
73
|
2 |
|
$json[Members::LINKS] = $this->links; |
|
74
|
|
|
} |
|
75
|
2 |
|
if (isset($this->errors)) { |
|
76
|
1 |
|
$json[Members::ERRORS] = $this->errors; |
|
77
|
|
|
} |
|
78
|
2 |
|
if ($this->dataHasBeenSet()) { |
|
79
|
1 |
|
$json[Members::DATA] = is_null($this->data) ? null : $this->data->toArray(); |
|
80
|
|
|
} |
|
81
|
2 |
|
if (isset($this->included)) { |
|
82
|
1 |
|
$json[Members::INCLUDED] = $this->included->toArray(); |
|
83
|
|
|
} |
|
84
|
2 |
|
if (isset($this->jsonapi)) { |
|
85
|
2 |
|
$json[Members::JSONAPI] = $this->jsonapi->toArray(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
return $json; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Undocumented function |
|
93
|
|
|
* |
|
94
|
|
|
* @param integer $options |
|
95
|
|
|
* |
|
96
|
|
|
* @return static |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function fake($options = null, $count = null) |
|
99
|
|
|
{ |
|
100
|
1 |
|
if (is_null($options)) { |
|
101
|
1 |
|
$options = self::FAKE_SINGLE | self::FAKE_RESOURCE_OBJECT; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
$withErrors = (($options & self::FAKE_ERRORS) == self::FAKE_ERRORS); |
|
105
|
|
|
|
|
106
|
1 |
|
$this->fakeLinks() |
|
107
|
1 |
|
->fakeMeta() |
|
108
|
1 |
|
->setJsonapi(new JsonapiFactory) |
|
109
|
1 |
|
->jsonapi->fake(); |
|
110
|
|
|
|
|
111
|
1 |
|
return $withErrors ? $this->fakeErrors($count) : $this->fakeData($options, $count); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|