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
|
21 |
|
public function getIlluminateCollection() |
27
|
|
|
{ |
28
|
21 |
|
return $this->collection; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set the collection. |
33
|
|
|
* |
34
|
|
|
* @param Collection|array|null $provided An array of objects implementing ResourceObjectContract |
35
|
|
|
* or ResourceIdentifierContract |
36
|
|
|
* @param string|null $resourceType |
37
|
|
|
* |
38
|
|
|
* @return static |
39
|
|
|
* @throws JsonApiFakerException |
40
|
|
|
*/ |
41
|
45 |
|
public function setCollection($provided, $resourceType = null) |
42
|
|
|
{ |
43
|
45 |
|
$collection = null; |
44
|
45 |
|
$array = null; |
45
|
|
|
|
46
|
45 |
|
if (is_a($provided, Collection::class)) { |
47
|
33 |
|
if ($resourceType === null) { |
48
|
3 |
|
throw new JsonApiFakerException(Messages::ERROR_TYPE_NOT_NULL); |
49
|
|
|
} |
50
|
|
|
|
51
|
30 |
|
$collection = $provided; |
52
|
30 |
|
$array = $this->transform($collection, $resourceType); |
53
|
|
|
} |
54
|
|
|
|
55
|
36 |
|
if (is_array($provided)) { |
56
|
12 |
|
$array = $provided; |
57
|
12 |
|
$collection = collect($provided)->map( |
58
|
|
|
/** |
59
|
|
|
* @param ResourceObjectContract|ResourceIdentifierContract $item |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
62
|
|
|
* @throws JsonApiFakerException |
63
|
|
|
*/ |
64
|
12 |
|
function ($item) { |
65
|
9 |
|
$isValid = is_a($item, ResourceObjectContract::class) |
66
|
9 |
|
|| is_a($item, ResourceIdentifierContract::class); |
67
|
9 |
|
if (!$isValid) { |
68
|
3 |
|
throw new JsonApiFakerException(Messages::ERROR_NOT_FACTORY_INSTANCE); |
69
|
|
|
} |
70
|
6 |
|
if ($item->getModel() == null) { |
71
|
3 |
|
throw new JsonApiFakerException(Messages::ERROR_MODEL_NOT_SET); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return $item->getModel(); |
75
|
12 |
|
} |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
30 |
|
$this->collection = $collection; |
80
|
30 |
|
parent::setCollection($array); |
81
|
|
|
|
82
|
30 |
|
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
|
|
|
|