|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the reva2/jsonapi. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Sergey Revenko <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Reva2\JsonApi\Decoders\Mapping\Loader; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
15
|
|
|
use Reva2\JsonApi\Annotations\Attribute; |
|
16
|
|
|
use Reva2\JsonApi\Annotations\Document as ApiDocument; |
|
17
|
|
|
use Reva2\JsonApi\Annotations\Relationship; |
|
18
|
|
|
use Reva2\JsonApi\Annotations\Resource as ApiResource; |
|
19
|
|
|
use Reva2\JsonApi\Contracts\Decoders\Mapping\Loader\LoaderInterface; |
|
20
|
|
|
use Reva2\JsonApi\Decoders\Mapping\AttributeMetadata; |
|
21
|
|
|
use Reva2\JsonApi\Decoders\Mapping\DocumentMetadata; |
|
22
|
|
|
use Reva2\JsonApi\Decoders\Mapping\GenericMetadata; |
|
23
|
|
|
use Reva2\JsonApi\Decoders\Mapping\ResourceMetadata; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Loads JSON API metadata using a Doctrine annotations |
|
27
|
|
|
* |
|
28
|
|
|
* @package Reva2\JsonApi\Decoders\Mapping\Loader |
|
29
|
|
|
* @author Sergey Revenko <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class AnnotationLoader implements LoaderInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var Reader |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $reader; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor |
|
40
|
|
|
* |
|
41
|
|
|
* @param Reader $reader |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(Reader $reader) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->reader = $reader; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
*/ |
|
51
|
|
|
public function loadClassMetadata(\ReflectionClass $class) |
|
52
|
|
|
{ |
|
53
|
|
|
if (null !== ($resource = $this->reader->getClassAnnotation($class, ApiResource::class))) { |
|
54
|
|
|
return $this->loadResourceMetadata($resource, $class); |
|
|
|
|
|
|
55
|
|
|
} elseif (null !== ($document = $this->reader->getClassAnnotation($class, ApiDocument::class))) { |
|
56
|
|
|
return $this->loadDocumentMetadata($document, $class); |
|
|
|
|
|
|
57
|
|
|
} else { |
|
58
|
|
|
return new GenericMetadata($class->getName()); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Parse JSON API resource metadata |
|
64
|
|
|
* |
|
65
|
|
|
* @param ApiResource $resource |
|
66
|
|
|
* @param \ReflectionClass $class |
|
67
|
|
|
* @return ResourceMetadata |
|
68
|
|
|
*/ |
|
69
|
|
|
private function loadResourceMetadata(ApiResource $resource, \ReflectionClass $class) |
|
70
|
|
|
{ |
|
71
|
|
|
$metadata = new ResourceMetadata($resource->name, $class->getName()); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$properties = $class->getProperties(); |
|
74
|
|
|
foreach ($properties as $property) { |
|
75
|
|
|
foreach ($this->reader->getPropertyAnnotations($property) as $annotation) { |
|
76
|
|
|
if ($annotation instanceof Attribute) { |
|
77
|
|
|
$metadata->addAttribute($this->parseAttribute($annotation, $property)); |
|
|
|
|
|
|
78
|
|
|
} elseif ($annotation instanceof Relationship) { |
|
79
|
|
|
$metadata->addRelationship($this->parseRelationship($annotation, $property)); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $metadata; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Parse JSON API document metadata |
|
89
|
|
|
* |
|
90
|
|
|
* @param ApiDocument $document |
|
91
|
|
|
* @param \ReflectionClass $class |
|
92
|
|
|
* @return DocumentMetadata |
|
93
|
|
|
*/ |
|
94
|
|
|
private function loadDocumentMetadata(ApiDocument $document, \ReflectionClass $class) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$metadata = new DocumentMetadata($class->getName()); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
return $metadata; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function parseAttribute(Attribute $attribute, \ReflectionProperty $property) |
|
102
|
|
|
{ |
|
103
|
|
|
$metadata = new AttributeMetadata($property->getName(), $property->class); |
|
104
|
|
|
if ($attribute->type) { |
|
105
|
|
|
$this->parseAttributeType($attribute->type, $metadata); |
|
|
|
|
|
|
106
|
|
|
} else { |
|
107
|
|
|
$this->parsePropertyDataType($property, $metadata); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.