File tree 3 files changed +47
-23
lines changed
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
23
23
24
24
public function setAttribute (string $ name , $ value )
25
25
{
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 ' );
28
28
}
29
29
if (isset ($ this ->relationships [$ name ])) {
30
30
throw new \LogicException ("Field $ name already exists in relationships " );
@@ -34,6 +34,9 @@ public function setAttribute(string $name, $value)
34
34
35
35
public function setRelationship (string $ name , Relationship $ relationship )
36
36
{
37
+ if ($ this ->isReservedName ($ name )) {
38
+ throw new \InvalidArgumentException ('Can not use a reserved name ' );
39
+ }
37
40
if (isset ($ this ->attributes [$ name ])) {
38
41
throw new \LogicException ("Field $ name already exists in attributes " );
39
42
}
@@ -74,4 +77,13 @@ public function identifies(ResourceInterface $resource): bool
74
77
}
75
78
return false ;
76
79
}
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
+ }
77
89
}
Original file line number Diff line number Diff line change 27
27
*
28
28
* @see http://jsonapi.org/format/#document-resource-object-fields
29
29
*/
30
- class ResourceFireldsTest extends TestCase
30
+ class ResourceFieldsTest extends TestCase
31
31
{
32
32
/**
33
33
* @expectedException \LogicException
@@ -50,4 +50,36 @@ public function testCanNotSetAttributeIfRelationshipExists()
50
50
$ res ->setRelationship ('foo ' , Relationship::fromMeta (new ArrayMeta (['a ' => 'b ' ])));
51
51
$ res ->setAttribute ('foo ' , 'bar ' );
52
52
}
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
+ }
53
85
}
Original file line number Diff line number Diff line change @@ -92,24 +92,4 @@ public function resourceProvider()
92
92
],
93
93
];
94
94
}
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
- }
115
95
}
You can’t perform that action at this time.
0 commit comments