| Total Complexity | 9 |
| Total Lines | 49 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 15 | abstract class Transformer extends JsonResource |
||
| 16 | { |
||
| 17 | public $relations = [ |
||
| 18 | |||
| 19 | ]; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Resolve the resource to an array. |
||
| 23 | * |
||
| 24 | * @param \Illuminate\Http\Request|null $request |
||
| 25 | * @return array |
||
| 26 | */ |
||
| 27 | public function resolve($request = null) |
||
| 28 | { |
||
| 29 | return array_merge(parent::resolve($request),$this->filter($this->includeRelations())); |
||
| 30 | } |
||
| 31 | |||
| 32 | protected function includeRelations() |
||
| 33 | { |
||
| 34 | $relations = []; |
||
| 35 | foreach ($this->relations as $relation) { |
||
| 36 | if (is_string($relation) && method_exists(static::class, 'transform' . ucfirst(strtolower($relation)))) { |
||
| 37 | $method = 'transform' . ucfirst(strtolower($relation)); |
||
| 38 | $data = $this->$method($this->resource); |
||
| 39 | if ($data instanceof JsonResource) { |
||
| 40 | $data->jsonSerialize(); |
||
| 41 | } |
||
| 42 | $relations[strtolower($relation)] = $data; |
||
| 43 | } else { |
||
| 44 | throw new \Exception("invalid relation or not relation_transform_method given in " . get_short_class_name(static::class)); |
||
| 45 | } |
||
| 46 | |||
| 47 | } |
||
| 48 | return $relations; |
||
| 49 | } |
||
| 50 | |||
| 51 | public static function resource($model) |
||
| 52 | { |
||
| 53 | return new static($model); |
||
| 54 | } |
||
| 55 | |||
| 56 | public static function collection($resource) |
||
| 59 | } |
||
| 60 | |||
| 61 | public function serialize() |
||
| 64 | } |
||
| 65 | } |
||
| 66 |