|
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; |
|
13
|
|
|
|
|
14
|
|
|
use Neomerx\JsonApi\Document\Error; |
|
15
|
|
|
use Neomerx\JsonApi\Exceptions\JsonApiException; |
|
16
|
|
|
use Reva2\JsonApi\Contracts\Decoders\Mapping\ClassMetadataInterface; |
|
17
|
|
|
use Reva2\JsonApi\Contracts\Decoders\Mapping\PropertyMetadataInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Generic class metadata |
|
21
|
|
|
* |
|
22
|
|
|
* @package Reva2\JsonApi\Decoders\Mapping |
|
23
|
|
|
* @author Sergey Revenko <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class ClassMetadata extends GenericMetadata implements ClassMetadataInterface |
|
26
|
|
|
{ |
|
27
|
|
|
const INVALID_DISCRIMINATOR_VALUE = '19cf8396-0fe1-487f-bd32-d5d34e2b4f68'; |
|
28
|
|
|
|
|
29
|
|
|
/*** |
|
30
|
|
|
* @var PropertyMetadataInterface|null |
|
31
|
|
|
* @internal |
|
32
|
|
|
*/ |
|
33
|
|
|
public $discField; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
* @internal |
|
38
|
|
|
*/ |
|
39
|
|
|
public $discMap; |
|
40
|
10 |
|
|
|
41
|
|
|
/** |
|
42
|
10 |
|
* @var string |
|
43
|
|
|
* @internal |
|
44
|
|
|
*/ |
|
45
|
|
|
public $discError; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritdoc |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getDiscriminatorField() |
|
51
|
11 |
|
{ |
|
52
|
|
|
return $this->discField; |
|
53
|
11 |
|
} |
|
54
|
|
|
|
|
55
|
11 |
|
/** |
|
56
|
|
|
* Sets discriminator field |
|
57
|
|
|
* |
|
58
|
|
|
* @param PropertyMetadataInterface|null $field |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setDiscriminatorField(PropertyMetadataInterface $field = null) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->discField = $field; |
|
64
|
12 |
|
|
|
65
|
|
|
return $this; |
|
66
|
12 |
|
} |
|
67
|
|
|
|
|
68
|
12 |
|
/** |
|
69
|
|
|
* Sets discriminator map |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $map |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
8 |
|
public function setDiscriminatorMap(array $map) |
|
75
|
|
|
{ |
|
76
|
8 |
|
$this->discMap = $map; |
|
77
|
1 |
|
|
|
78
|
1 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
1 |
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return string |
|
83
|
8 |
|
*/ |
|
84
|
|
|
public function getDiscriminatorError() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->discError; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
/** |
|
90
|
|
|
* @param string $error |
|
91
|
|
|
* @return $this |
|
92
|
1 |
|
*/ |
|
93
|
|
|
public function setDiscriminatorError($error) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->discError = $error; |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritdoc |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getDiscriminatorClass($value) |
|
104
|
|
|
{ |
|
105
|
|
|
if (!array_key_exists($value, $this->discMap)) { |
|
106
|
|
|
$error = new Error( |
|
107
|
|
|
rand(), |
|
108
|
|
|
null, |
|
109
|
|
|
422, |
|
110
|
|
|
self::INVALID_DISCRIMINATOR_VALUE, |
|
111
|
|
|
str_replace('{{value}}', (string) $value, $this->discError) |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
throw new JsonApiException($error, 422); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $this->discMap[$value]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @inheritdoc |
|
122
|
|
|
*/ |
|
123
|
|
|
public function mergeMetadata($metadata) |
|
124
|
|
|
{ |
|
125
|
|
|
// Nothing to do here |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|