55
66use JsonApiPhp \JsonApi \Document \LinksTrait ;
77use JsonApiPhp \JsonApi \Document \Meta ;
8+ use JsonApiPhp \JsonApi \Document \ReservedName ;
89
910class ResourceObject implements \JsonSerializable
1011{
@@ -22,7 +23,7 @@ class ResourceObject implements \JsonSerializable
2223
2324 public function __construct (string $ type , string $ id = null )
2425 {
25- $ this ->type = $ type ;
26+ $ this ->type = new ResourceType ( $ type) ;
2627 $ this ->id = $ id ;
2728 }
2829
@@ -33,12 +34,7 @@ public function setMeta(Meta $meta)
3334
3435 public function setAttribute (string $ name , $ value )
3536 {
36- if ($ this ->isReservedName ($ name )) {
37- throw new \InvalidArgumentException ("Can not use a reserved name ' $ name' " );
38- }
39- if (!$ this ->isValidMemberName ($ name )) {
40- throw new \OutOfBoundsException ("Not a valid attribute name ' $ name' " );
41- }
37+ $ name = (string ) new ReservedName ($ name );
4238 if (isset ($ this ->relationships [$ name ])) {
4339 throw new \LogicException ("Field ' $ name' already exists in relationships " );
4440 }
@@ -47,12 +43,7 @@ public function setAttribute(string $name, $value)
4743
4844 public function setRelationship (string $ name , Relationship $ relationship )
4945 {
50- if ($ this ->isReservedName ($ name )) {
51- throw new \InvalidArgumentException ("Can not use a reserved name ' $ name' " );
52- }
53- if (!$ this ->isValidMemberName ($ name )) {
54- throw new \OutOfBoundsException ("Not a valid attribute name ' $ name' " );
55- }
46+ $ name = (string ) new ReservedName ($ name );
5647 if (isset ($ this ->attributes [$ name ])) {
5748 throw new \LogicException ("Field ' $ name' already exists in attributes " );
5849 }
@@ -61,7 +52,7 @@ public function setRelationship(string $name, Relationship $relationship)
6152
6253 public function toIdentifier (): ResourceIdentifier
6354 {
64- return new ResourceIdentifier ($ this ->type , $ this ->id );
55+ return new ResourceIdentifier (( string ) $ this ->type , $ this ->id );
6556 }
6657
6758 public function jsonSerialize ()
@@ -92,14 +83,4 @@ public function identifies(ResourceObject $resource): bool
9283 }
9384 return false ;
9485 }
95-
96- private function isReservedName (string $ name ): bool
97- {
98- return in_array ($ name , ['id ' , 'type ' ]);
99- }
100-
101- private function isValidMemberName (string $ name ): bool
102- {
103- return preg_match ('/^(?=[^-_ ])[a-zA-Z0-9\x{0080}-\x{FFFF}-_ ]*(?<=[^-_ ])$/u ' , $ name ) === 1 ;
104- }
10586}
0 commit comments