|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Knuckles\Scribe\Docblock2Attributes\TagParsers; |
| 4 | + |
| 5 | +use Illuminate\Support\Arr; |
| 6 | +use Knuckles\Scribe\Extracting\ParamHelpers; |
| 7 | +use Knuckles\Scribe\Tools\AnnotationParser as a; |
| 8 | +use Knuckles\Scribe\Tools\Utils; |
| 9 | +use Mpociot\Reflection\DocBlock\Tag; |
| 10 | + |
| 11 | +class ApiResourceTagParser |
| 12 | +{ |
| 13 | + use ParamHelpers; |
| 14 | + |
| 15 | + public function __construct(protected string $tagContent, protected array $allTags, protected Tag $tag) |
| 16 | + { |
| 17 | + } |
| 18 | + |
| 19 | + public function parse() |
| 20 | + { |
| 21 | + [$statusCode, $description, $apiResourceClass, $isCollection] = $this->getStatusCodeAndApiResourceClass(); |
| 22 | + [$modelClass, $factoryStates, $relations, $pagination] = $this->getClassToBeTransformedAndAttributes(); |
| 23 | + $additionalData = $this->getAdditionalData(); |
| 24 | + |
| 25 | + if (empty($pagination)) { |
| 26 | + [$simplePaginate, $paginate] = [null, null]; |
| 27 | + } else { |
| 28 | + if (($pagination[1] ?? null) === 'simple') { |
| 29 | + [$simplePaginate, $paginate] = [$pagination[0], null]; |
| 30 | + } else { |
| 31 | + [$paginate, $simplePaginate] = [$pagination[0], null]; |
| 32 | + |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return [ |
| 37 | + [ |
| 38 | + 'type' => 'apiresource', |
| 39 | + 'data' => [ |
| 40 | + 'status' => (int)$statusCode, |
| 41 | + 'description' => $description, |
| 42 | + 'name' => $apiResourceClass, |
| 43 | + 'model' => $modelClass, |
| 44 | + 'collection' => $isCollection, |
| 45 | + 'factoryStates' => $factoryStates, |
| 46 | + 'with' => $relations, |
| 47 | + 'additionalData' => $additionalData, |
| 48 | + 'paginate' => $paginate, |
| 49 | + 'simplePaginate' => $simplePaginate, |
| 50 | + ], |
| 51 | + ], |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + private function getStatusCodeAndApiResourceClass(): array |
| 56 | + { |
| 57 | + preg_match('/^(\d{3})?\s?([\s\S]*)$/', $this->tagContent, $result); |
| 58 | + |
| 59 | + $status = $result[1] ?: 200; |
| 60 | + $content = $result[2]; |
| 61 | + |
| 62 | + ['attributes' => $attributes, 'content' => $content] = a::parseIntoContentAndAttributes($content, ['status', 'scenario']); |
| 63 | + |
| 64 | + $status = $attributes['status'] ?: $status; |
| 65 | + $apiResourceClass = $content; |
| 66 | + $description = ($status && $attributes['scenario']) ? "$status, {$attributes['scenario']}" : ""; |
| 67 | + |
| 68 | + $isCollection = strtolower($this->tag->getName()) == 'apiresourcecollection'; |
| 69 | + return [(int)$status, $description, $apiResourceClass, $isCollection]; |
| 70 | + } |
| 71 | + |
| 72 | + private function getClassToBeTransformedAndAttributes(): array |
| 73 | + { |
| 74 | + $modelTag = Arr::first(Utils::filterDocBlockTags($this->allTags, 'apiresourcemodel')); |
| 75 | + |
| 76 | + $modelClass = null; |
| 77 | + $states = []; |
| 78 | + $relations = []; |
| 79 | + $pagination = []; |
| 80 | + |
| 81 | + if ($modelTag) { |
| 82 | + ['content' => $modelClass, 'attributes' => $attributes] = a::parseIntoContentAndAttributes($modelTag->getContent(), ['states', 'with', 'paginate']); |
| 83 | + $states = $attributes['states'] ? explode(',', $attributes['states']) : []; |
| 84 | + $relations = $attributes['with'] ? explode(',', $attributes['with']) : []; |
| 85 | + $pagination = $attributes['paginate'] ? explode(',', $attributes['paginate']) : []; |
| 86 | + } |
| 87 | + |
| 88 | + return [$modelClass, $states, $relations, $pagination]; |
| 89 | + } |
| 90 | + |
| 91 | + private function getAdditionalData(): array |
| 92 | + { |
| 93 | + $tag = Arr::first(Utils::filterDocBlockTags($this->allTags, 'apiresourceadditional')); |
| 94 | + return $tag ? a::parseIntoAttributes($tag->getContent()) : []; |
| 95 | + } |
| 96 | +} |
0 commit comments