@@ -32,14 +32,19 @@ final class DocumentLink implements DocumentLinkInterface
3232 */
3333 protected $ manager ;
3434
35+ /**
36+ * @var AccessInterface
37+ */
38+ protected $ parent ;
39+
3540 /**
3641 * @param object $object The link object
3742 *
3843 * @return self
3944 *
4045 * @throws ValidationException
4146 */
42- public function __construct ($ object , FactoryManagerInterface $ manager )
47+ public function __construct ($ object , FactoryManagerInterface $ manager, AccessInterface $ parent )
4348 {
4449 if ( ! is_object ($ object ) )
4550 {
@@ -48,80 +53,72 @@ public function __construct($object, FactoryManagerInterface $manager)
4853
4954 $ this ->manager = $ manager ;
5055
56+ $ this ->parent = $ parent ;
57+
5158 $ this ->container = new DataContainer ();
5259
53- if ( property_exists ($ object , 'self ' ) )
60+ $ links = get_object_vars ($ object );
61+
62+ if ( array_key_exists ('self ' , $ links ) )
5463 {
55- if ( ! is_string ($ object -> self ) )
64+ if ( ! is_string ($ links [ ' self ' ] ) )
5665 {
57- throw new ValidationException ('property "self" has to be a string, " ' . gettype ($ object -> self ) . '" given. ' );
66+ throw new ValidationException ('property "self" has to be a string, " ' . gettype ($ links [ ' self ' ] ) . '" given. ' );
5867 }
5968
60- $ this ->container ->set ('self ' , $ object ->self );
69+ $ this ->container ->set ('self ' , $ links ['self ' ]);
70+
71+ unset($ links ['self ' ]);
6172 }
6273
63- if ( property_exists ( $ object , 'related ' ) )
74+ if ( array_key_exists ( 'related ' , $ links ) )
6475 {
65- if ( ! is_string ($ object -> related ) )
76+ if ( ! is_string ($ links [ ' related ' ]) and ! is_object ( $ links [ ' related ' ] ) )
6677 {
67- throw new ValidationException ('property "related" has to be a string, " ' . gettype ($ object -> related ) . '" given. ' );
78+ throw new ValidationException ('property "related" has to be a string or object , " ' . gettype ($ links [ ' related ' ] ) . '" given. ' );
6879 }
6980
70- $ this ->container ->set ('related ' , $ object ->related );
71- }
81+ $ this ->setLink ('related ' , $ links ['related ' ]);
7282
73- // Pagination links
83+ unset($ links ['related ' ]);
84+ }
7485
75- if ( property_exists ($ object , 'first ' ) )
86+ // Pagination links, if data in parent attributes exists
87+ if ( $ parent ->has ('data ' ) )
7688 {
77- if ( ! is_string ( $ object -> first ) and ! is_null ( $ object -> first ) )
89+ if ( array_key_exists ( ' first ' , $ links ) )
7890 {
79- throw new ValidationException ('property "first" has to be a string or null, " ' . gettype ($ object ->first ) . '" given. ' );
80- }
91+ $ this ->setPaginationLink ('first ' , $ links ['first ' ]);
8192
82- if ( ! is_null ($ object ->first ) )
83- {
84- $ this ->container ->set ('first ' , strval ($ object ->first ));
93+ unset($ links ['first ' ]);
8594 }
86- }
8795
88- if ( property_exists ($ object , 'last ' ) )
89- {
90- if ( ! is_string ($ object ->last ) and ! is_null ($ object ->last ) )
96+ if ( array_key_exists ('last ' , $ links ) )
9197 {
92- throw new ValidationException ('property "last" has to be a string or null, " ' . gettype ($ object ->last ) . '" given. ' );
93- }
98+ $ this ->setPaginationLink ('last ' , $ links ['last ' ]);
9499
95- if ( ! is_null ($ object ->last ) )
96- {
97- $ this ->container ->set ('last ' , strval ($ object ->last ));
100+ unset($ links ['last ' ]);
98101 }
99- }
100102
101- if ( property_exists ($ object , 'prev ' ) )
102- {
103- if ( ! is_string ($ object ->prev ) and ! is_null ($ object ->prev ) )
103+ if ( array_key_exists ('prev ' , $ links ) )
104104 {
105- throw new ValidationException ('property "prev" has to be a string or null, " ' . gettype ($ object ->prev ) . '" given. ' );
105+ $ this ->setPaginationLink ('prev ' , $ links ['prev ' ]);
106+
107+ unset($ links ['prev ' ]);
106108 }
107109
108- if ( ! is_null ( $ object -> prev ) )
110+ if ( array_key_exists ( ' next ' , $ links ) )
109111 {
110- $ this ->container ->set ('prev ' , strval ($ object ->prev ));
112+ $ this ->setPaginationLink ('next ' , $ links ['next ' ]);
113+
114+ unset($ links ['next ' ]);
111115 }
112116 }
113117
114- if ( property_exists ($ object , 'next ' ) )
118+ // custom links
119+ foreach ($ links as $ name => $ value )
115120 {
116- if ( ! is_string ($ object ->next ) and ! is_null ($ object ->next ) )
117- {
118- throw new ValidationException ('property "next" has to be a string or null, " ' . gettype ($ object ->next ) . '" given. ' );
119- }
120-
121- if ( ! is_null ($ object ->next ) )
122- {
123- $ this ->container ->set ('next ' , strval ($ object ->next ));
124- }
121+ $ this ->setLink ($ name , $ value );
125122 }
126123 }
127124
@@ -142,4 +139,56 @@ public function get($key)
142139 throw new AccessException ('" ' . $ key . '" doesn \'t exist in this object. ' );
143140 }
144141 }
142+
143+ /**
144+ * Set a pagination link
145+ *
146+ * @param string $name The name of the link
147+ * @param string $value The link
148+ * @return self
149+ */
150+ private function setPaginationLink ($ name , $ value )
151+ {
152+ if ( ! is_string ($ value ) and ! is_null ($ value ) )
153+ {
154+ throw new ValidationException ('property " ' . $ name . '" has to be a string or null, " ' . gettype ($ value ) . '" given. ' );
155+ }
156+
157+ if ( ! is_null ($ value ) )
158+ {
159+ $ this ->container ->set ($ name , strval ($ value ));
160+ }
161+
162+ return $ this ;
163+ }
164+
165+ /**
166+ * Set a link
167+ *
168+ * @param string $name The name of the link
169+ * @param string $link The link
170+ * @return self
171+ */
172+ private function setLink ($ name , $ link )
173+ {
174+ if ( ! is_string ($ link ) and ! is_object ($ link ) )
175+ {
176+ throw new ValidationException ('Link attribute has to be an object or string, " ' . gettype ($ link ) . '" given. ' );
177+ }
178+
179+ if ( is_string ($ link ) )
180+ {
181+ $ this ->container ->set ($ name , strval ($ link ));
182+
183+ return $ this ;
184+ }
185+
186+ // Now $link can only be an object
187+ $ this ->container ->set ($ name , $ this ->manager ->getFactory ()->make (
188+ 'Link ' ,
189+ [$ link , $ this ->manager , $ this ]
190+ ));
191+
192+ return $ this ;
193+ }
145194}
0 commit comments