File tree Expand file tree Collapse file tree 3 files changed +47
-23
lines changed Expand file tree Collapse file tree 3 files changed +47
-23
lines changed Original file line number Diff line number Diff line change @@ -23,8 +23,8 @@ class ResourceObject extends ResourceIdentifier
2323
2424 public function setAttribute (string $ name , $ value )
2525 {
26- if (in_array ($ name, [ ' id ' , ' type ' ] )) {
27- throw new \InvalidArgumentException ('Invalid attribute name ' );
26+ if ($ this -> isReservedName ($ name )) {
27+ throw new \InvalidArgumentException ('Can not use a reserved name ' );
2828 }
2929 if (isset ($ this ->relationships [$ name ])) {
3030 throw new \LogicException ("Field $ name already exists in relationships " );
@@ -34,6 +34,9 @@ public function setAttribute(string $name, $value)
3434
3535 public function setRelationship (string $ name , Relationship $ relationship )
3636 {
37+ if ($ this ->isReservedName ($ name )) {
38+ throw new \InvalidArgumentException ('Can not use a reserved name ' );
39+ }
3740 if (isset ($ this ->attributes [$ name ])) {
3841 throw new \LogicException ("Field $ name already exists in attributes " );
3942 }
@@ -74,4 +77,13 @@ public function identifies(ResourceInterface $resource): bool
7477 }
7578 return false ;
7679 }
80+
81+ /**
82+ * @param string $name
83+ * @return bool
84+ */
85+ private function isReservedName (string $ name ): bool
86+ {
87+ return in_array ($ name , ['id ' , 'type ' ]);
88+ }
7789}
Original file line number Diff line number Diff line change 2727 *
2828 * @see http://jsonapi.org/format/#document-resource-object-fields
2929 */
30- class ResourceFireldsTest extends TestCase
30+ class ResourceFieldsTest extends TestCase
3131{
3232 /**
3333 * @expectedException \LogicException
@@ -50,4 +50,36 @@ public function testCanNotSetAttributeIfRelationshipExists()
5050 $ res ->setRelationship ('foo ' , Relationship::fromMeta (new ArrayMeta (['a ' => 'b ' ])));
5151 $ res ->setAttribute ('foo ' , 'bar ' );
5252 }
53+
54+ /**
55+ * @param string $name
56+ * @expectedException \InvalidArgumentException
57+ * @expectedExceptionMessage Can not use a reserved name
58+ * @dataProvider invalidAttributeNames
59+ */
60+ public function testAttributeCanNotHaveReservedNames (string $ name )
61+ {
62+ $ res = new ResourceObject ('books ' , 'abc ' );
63+ $ res ->setAttribute ($ name , 1 );
64+ }
65+
66+ /**
67+ * @param string $name
68+ * @expectedException \InvalidArgumentException
69+ * @expectedExceptionMessage Can not use a reserved name
70+ * @dataProvider invalidAttributeNames
71+ */
72+ public function testRelationshipCanNotHaveReservedNames (string $ name )
73+ {
74+ $ res = new ResourceObject ('books ' , 'abc ' );
75+ $ res ->setRelationship ($ name , Relationship::fromMeta (new ArrayMeta (['a ' => 'b ' ])));
76+ }
77+
78+ public function invalidAttributeNames (): array
79+ {
80+ return [
81+ ['id ' ],
82+ ['type ' ],
83+ ];
84+ }
5385}
Original file line number Diff line number Diff line change @@ -92,24 +92,4 @@ public function resourceProvider()
9292 ],
9393 ];
9494 }
95-
96- /**
97- * @param string $name
98- * @expectedException \InvalidArgumentException
99- * @expectedExceptionMessage Invalid attribute name
100- * @dataProvider invalidAttributeNames
101- */
102- public function testAttributeCanNotHaveReservedNames (string $ name )
103- {
104- $ r = new ResourceObject ('books ' , 'abc ' );
105- $ r ->setAttribute ($ name , 1 );
106- }
107-
108- public function invalidAttributeNames (): array
109- {
110- return [
111- ['id ' ],
112- ['type ' ],
113- ];
114- }
11595}
You can’t perform that action at this time.
0 commit comments