6
6
namespace tuyakhov \jsonapi ;
7
7
8
8
use yii \base \Component ;
9
+ use yii \base \InvalidValueException ;
9
10
use yii \base \Model ;
10
11
use yii \data \DataProviderInterface ;
11
12
use yii \data \Pagination ;
@@ -86,16 +87,14 @@ public function serialize($data)
86
87
* @param ResourceInterface $model
87
88
* @return array
88
89
*/
89
- protected function serializeModel ($ model )
90
+ protected function serializeModel (ResourceInterface $ model )
90
91
{
91
92
$ fields = $ this ->getRequestedFields ();
92
93
93
94
$ attributes = isset ($ fields [$ model ->getType ()]) ? $ fields [$ model ->getType ()] : [];
94
- $ data = [
95
- 'id ' => $ model ->getId (),
96
- 'type ' => $ model ->getType (),
95
+ $ data = array_merge ($ this ->serializeIdentifier ($ model ), [
97
96
'attributes ' => $ model ->getResourceAttributes ($ attributes ),
98
- ];
97
+ ]) ;
99
98
100
99
$ relationships = $ model ->getResourceRelationships ();
101
100
if (!empty ($ relationships )) {
@@ -104,11 +103,11 @@ protected function serializeModel($model)
104
103
if (is_array ($ items )) {
105
104
foreach ($ items as $ item ) {
106
105
if ($ item instanceof ResourceIdentifierInterface) {
107
- $ relationship [] = [ ' id ' => $ item -> getId (), ' type ' => $ item-> getType ()] ;
106
+ $ relationship [] = $ this -> serializeIdentifier ( $ item) ;
108
107
}
109
108
}
110
109
} elseif ($ items instanceof ResourceIdentifierInterface) {
111
- $ relationship = [ ' id ' => $ items -> getId (), ' type ' => $ items-> getType ()] ;
110
+ $ relationship = $ this -> serializeIdentifier ( $ items) ;
112
111
}
113
112
114
113
if (!empty ($ relationship )) {
@@ -134,7 +133,7 @@ protected function serializeModel($model)
134
133
* @param ResourceInterface $resource
135
134
* @return array
136
135
*/
137
- protected function serializeResource ($ resource )
136
+ protected function serializeResource (ResourceInterface $ resource )
138
137
{
139
138
if ($ this ->request ->getIsHead ()) {
140
139
return null ;
@@ -150,6 +149,26 @@ protected function serializeResource($resource)
150
149
}
151
150
}
152
151
152
+ /**
153
+ * Serialize resource identifier object and make type juggling
154
+ * @link http://jsonapi.org/format/#document-resource-object-identification
155
+ * @param ResourceIdentifierInterface $identifier
156
+ * @return array
157
+ */
158
+ protected function serializeIdentifier (ResourceIdentifierInterface $ identifier )
159
+ {
160
+ $ result = [];
161
+ foreach (['id ' , 'type ' ] as $ key ) {
162
+ $ getter = 'get ' . ucfirst ($ key );
163
+ $ value = $ identifier ->$ getter ();
164
+ if ($ value === null || is_array ($ value ) || (is_object ($ value ) && !method_exists ($ value , '__toString ' ))) {
165
+ throw new InvalidValueException ("The value {$ key } of resource object " . get_class ($ identifier ) . ' MUST be a string. ' );
166
+ }
167
+ $ result [$ key ] = (string ) $ value ;
168
+ }
169
+ return $ result ;
170
+ }
171
+
153
172
/**
154
173
* @param ResourceInterface $resource
155
174
* @return array
0 commit comments