|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace VGirol\JsonApiFaker\Laravel\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use VGirol\JsonApiFaker\Exception\JsonApiFakerException; |
|
7
|
|
|
use VGirol\JsonApiFaker\Factory\CollectionFactory as BaseFactory; |
|
8
|
|
|
use VGirol\JsonApiFaker\Laravel\Contract\CollectionContract; |
|
9
|
|
|
use VGirol\JsonApiFaker\Laravel\Contract\ResourceIdentifierContract; |
|
10
|
|
|
use VGirol\JsonApiFaker\Laravel\Contract\ResourceObjectContract; |
|
11
|
|
|
use VGirol\JsonApiFaker\Laravel\Messages; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Factory for collection of resource object (@see ResourceObjectFactory) |
|
15
|
|
|
* or resource identifier (@see ResourceIdentifierFactory). |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class CollectionFactory extends BaseFactory implements CollectionContract |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* A collection of models. |
|
21
|
|
|
* |
|
22
|
|
|
* @var Collection|null |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $collection; |
|
25
|
|
|
|
|
26
|
|
|
public function getIlluminateCollection() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->collection; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set the collection. |
|
33
|
15 |
|
* |
|
34
|
|
|
* @param Collection|array|null $provided An array of objects implementing ResourceObjectContract |
|
35
|
15 |
|
* or ResourceIdentifierContract |
|
36
|
15 |
|
* @param string|null $resourceType |
|
37
|
|
|
* |
|
38
|
15 |
|
* @return static |
|
39
|
11 |
|
* @throws JsonApiFakerException |
|
40
|
1 |
|
*/ |
|
41
|
|
|
public function setCollection($provided, $resourceType = null) |
|
42
|
|
|
{ |
|
43
|
10 |
|
$collection = null; |
|
44
|
10 |
|
$array = null; |
|
45
|
|
|
|
|
46
|
|
|
if (is_a($provided, Collection::class)) { |
|
|
|
|
|
|
47
|
12 |
|
if ($resourceType === null) { |
|
48
|
4 |
|
throw new JsonApiFakerException(Messages::ERROR_TYPE_NOT_NULL); |
|
49
|
4 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
$collection = $provided; |
|
52
|
|
|
$array = $this->transform($collection, $resourceType); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (is_array($provided)) { |
|
56
|
|
|
$array = $provided; |
|
57
|
|
|
$collection = collect($provided)->map( |
|
58
|
3 |
|
/** |
|
59
|
1 |
|
* @param ResourceObjectContract|ResourceIdentifierContract $item |
|
60
|
|
|
* |
|
61
|
2 |
|
* @return \Illuminate\Database\Eloquent\Model |
|
62
|
1 |
|
* @throws JsonApiFakerException |
|
63
|
|
|
*/ |
|
64
|
|
|
function ($item) { |
|
65
|
1 |
|
$isValid = is_a($item, ResourceObjectContract::class) |
|
66
|
4 |
|
|| is_a($item, ResourceIdentifierContract::class); |
|
67
|
|
|
if (!$isValid) { |
|
68
|
|
|
throw new JsonApiFakerException(Messages::ERROR_NOT_FACTORY_INSTANCE); |
|
69
|
|
|
} |
|
70
|
10 |
|
if ($item->getModel() == null) { |
|
71
|
10 |
|
throw new JsonApiFakerException(Messages::ERROR_MODEL_NOT_SET); |
|
72
|
|
|
} |
|
73
|
10 |
|
|
|
74
|
|
|
return $item->getModel(); |
|
75
|
|
|
} |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->collection = $collection; |
|
80
|
|
|
parent::setCollection($array); |
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns a collection of resource identifier or resource object factories. |
|
87
|
|
|
* |
|
88
|
|
|
* @param Collection $collection |
|
89
|
|
|
* @param string $resourceType |
|
90
|
|
|
* |
|
91
|
|
|
* @return array<ResourceObjectContract>|array<ResourceIdentifierContract> |
|
92
|
|
|
*/ |
|
93
|
|
|
abstract protected function transform($collection, $resourceType): array; |
|
94
|
|
|
} |
|
95
|
|
|
|