1 | <?php |
||
27 | class ResourceFactory |
||
28 | { |
||
29 | /** |
||
30 | * Mappings of supported data types with corresponding make methods. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | const MAKE_METHODS = [ |
||
35 | Builder::class => 'makeFromBuilder', |
||
36 | Collection::class => 'makeFromCollection', |
||
37 | Pivot::class => 'makeFromPivot', |
||
38 | Model::class => 'makeFromModel', |
||
39 | Paginator::class => 'makeFromPaginator', |
||
40 | Relation::class => 'makeFromRelation' |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Build a resource instance from the given data. |
||
45 | * |
||
46 | * @param mixed|null $data |
||
47 | * @return \League\Fractal\Resource\ResourceInterface |
||
48 | */ |
||
49 | public function make($data = null) |
||
61 | |||
62 | /** |
||
63 | * Resolve which make method to call from the given date type. |
||
64 | * |
||
65 | * @param mixed $data |
||
66 | * @return string |
||
67 | * @throws \InvalidArgumentException |
||
68 | */ |
||
69 | protected function getMakeMethod($data):string |
||
79 | |||
80 | /** |
||
81 | * Make resource from an Eloquent model. |
||
82 | * |
||
83 | * @param \Illuminate\Database\Eloquent\Model $model |
||
84 | * @return \League\Fractal\Resource\ResourceInterface |
||
85 | */ |
||
86 | protected function makeFromModel(Model $model):ResourceInterface |
||
90 | |||
91 | /** |
||
92 | * Make resource from a collection of Eloquent models. |
||
93 | * |
||
94 | * @param array $array |
||
95 | * @return \League\Fractal\Resource\ResourceInterface |
||
96 | */ |
||
97 | protected function makeFromArray(array $array):ResourceInterface |
||
101 | |||
102 | /** |
||
103 | * Make resource from a collection. |
||
104 | * |
||
105 | * @param \Illuminate\Support\Collection $collection |
||
106 | * @return \League\Fractal\Resource\ResourceInterface |
||
107 | */ |
||
108 | protected function makeFromCollection(Collection $collection):ResourceInterface |
||
112 | |||
113 | /** |
||
114 | * Make resource from an Eloquent query builder. |
||
115 | * |
||
116 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
117 | * @return \League\Fractal\Resource\ResourceInterface |
||
118 | */ |
||
119 | protected function makeFromBuilder(Builder $query):ResourceInterface |
||
123 | |||
124 | /** |
||
125 | * Make resource from an Eloquent paginator. |
||
126 | * |
||
127 | * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator |
||
128 | * @return \League\Fractal\Resource\ResourceInterface |
||
129 | */ |
||
130 | protected function makeFromPaginator(Paginator $paginator):ResourceInterface |
||
140 | |||
141 | /** |
||
142 | * Make resource from an Eloquent pivot table. |
||
143 | * |
||
144 | * @param \Illuminate\Database\Eloquent\Relations\Pivot $pivot |
||
145 | * @return \League\Fractal\Resource\ResourceInterface |
||
146 | */ |
||
147 | protected function makeFromPivot(Pivot $pivot):ResourceInterface |
||
151 | |||
152 | /** |
||
153 | * Make resource from an Eloquent query builder. |
||
154 | * |
||
155 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
156 | * @return \League\Fractal\Resource\ResourceInterface |
||
157 | */ |
||
158 | protected function makeFromRelation(Relation $relation):ResourceInterface |
||
162 | } |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.