R3VoLuT1OneR /
jsonapi-resource
| 1 | <?php namespace JSONAPI\Resource\Metadata; |
||
| 2 | |||
| 3 | use \JSONAPI\Resource\Attributes\Resource as ResourceAttr; |
||
| 4 | use JSONAPI\Resource\Metadata\Exceptions\IdFetcherMissingException; |
||
| 5 | |||
| 6 | use ReflectionClass; |
||
| 7 | |||
| 8 | class Resource |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @param ReflectionClass<object> $ref |
||
| 12 | */ |
||
| 13 | public function __construct( |
||
| 14 | protected ResourceAttr $attribute, |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 15 | protected ReflectionClass $ref |
||
| 16 | ) {} |
||
| 17 | |||
| 18 | public function getType(): string |
||
| 19 | { |
||
| 20 | if (null === ($type = $this->attribute->getType())) { |
||
| 21 | return lcfirst(str_replace('_', '', $this->ref->getName())); |
||
| 22 | } |
||
| 23 | |||
| 24 | return $type; |
||
| 25 | } |
||
| 26 | |||
| 27 | public function getId(object $resource): string |
||
| 28 | { |
||
| 29 | $idFetcher = $this->attribute->getIdFetcher(); |
||
| 30 | |||
| 31 | if (method_exists($resource, $idFetcher)) { |
||
| 32 | return (string) $resource->{$idFetcher}(); |
||
| 33 | } |
||
| 34 | |||
| 35 | if (property_exists($resource, $idFetcher)) { |
||
| 36 | return (string) $resource->{$idFetcher}; |
||
| 37 | } |
||
| 38 | |||
| 39 | throw new IdFetcherMissingException(); |
||
| 40 | } |
||
| 41 | } |