@@ -32,14 +32,19 @@ final class DocumentLink implements DocumentLinkInterface
32
32
*/
33
33
protected $ manager ;
34
34
35
+ /**
36
+ * @var AccessInterface
37
+ */
38
+ protected $ parent ;
39
+
35
40
/**
36
41
* @param object $object The link object
37
42
*
38
43
* @return self
39
44
*
40
45
* @throws ValidationException
41
46
*/
42
- public function __construct ($ object , FactoryManagerInterface $ manager )
47
+ public function __construct ($ object , FactoryManagerInterface $ manager, AccessInterface $ parent )
43
48
{
44
49
if ( ! is_object ($ object ) )
45
50
{
@@ -48,80 +53,72 @@ public function __construct($object, FactoryManagerInterface $manager)
48
53
49
54
$ this ->manager = $ manager ;
50
55
56
+ $ this ->parent = $ parent ;
57
+
51
58
$ this ->container = new DataContainer ();
52
59
53
- if ( property_exists ($ object , 'self ' ) )
60
+ $ links = get_object_vars ($ object );
61
+
62
+ if ( array_key_exists ('self ' , $ links ) )
54
63
{
55
- if ( ! is_string ($ object -> self ) )
64
+ if ( ! is_string ($ links [ ' self ' ] ) )
56
65
{
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. ' );
58
67
}
59
68
60
- $ this ->container ->set ('self ' , $ object ->self );
69
+ $ this ->container ->set ('self ' , $ links ['self ' ]);
70
+
71
+ unset($ links ['self ' ]);
61
72
}
62
73
63
- if ( property_exists ( $ object , 'related ' ) )
74
+ if ( array_key_exists ( 'related ' , $ links ) )
64
75
{
65
- if ( ! is_string ($ object -> related ) )
76
+ if ( ! is_string ($ links [ ' related ' ]) and ! is_object ( $ links [ ' related ' ] ) )
66
77
{
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. ' );
68
79
}
69
80
70
- $ this ->container ->set ('related ' , $ object ->related );
71
- }
81
+ $ this ->setLink ('related ' , $ links ['related ' ]);
72
82
73
- // Pagination links
83
+ unset($ links ['related ' ]);
84
+ }
74
85
75
- if ( property_exists ($ object , 'first ' ) )
86
+ // Pagination links, if data in parent attributes exists
87
+ if ( $ parent ->has ('data ' ) )
76
88
{
77
- if ( ! is_string ( $ object -> first ) and ! is_null ( $ object -> first ) )
89
+ if ( array_key_exists ( ' first ' , $ links ) )
78
90
{
79
- throw new ValidationException ('property "first" has to be a string or null, " ' . gettype ($ object ->first ) . '" given. ' );
80
- }
91
+ $ this ->setPaginationLink ('first ' , $ links ['first ' ]);
81
92
82
- if ( ! is_null ($ object ->first ) )
83
- {
84
- $ this ->container ->set ('first ' , strval ($ object ->first ));
93
+ unset($ links ['first ' ]);
85
94
}
86
- }
87
95
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 ) )
91
97
{
92
- throw new ValidationException ('property "last" has to be a string or null, " ' . gettype ($ object ->last ) . '" given. ' );
93
- }
98
+ $ this ->setPaginationLink ('last ' , $ links ['last ' ]);
94
99
95
- if ( ! is_null ($ object ->last ) )
96
- {
97
- $ this ->container ->set ('last ' , strval ($ object ->last ));
100
+ unset($ links ['last ' ]);
98
101
}
99
- }
100
102
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 ) )
104
104
{
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 ' ]);
106
108
}
107
109
108
- if ( ! is_null ( $ object -> prev ) )
110
+ if ( array_key_exists ( ' next ' , $ links ) )
109
111
{
110
- $ this ->container ->set ('prev ' , strval ($ object ->prev ));
112
+ $ this ->setPaginationLink ('next ' , $ links ['next ' ]);
113
+
114
+ unset($ links ['next ' ]);
111
115
}
112
116
}
113
117
114
- if ( property_exists ($ object , 'next ' ) )
118
+ // custom links
119
+ foreach ($ links as $ name => $ value )
115
120
{
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 );
125
122
}
126
123
}
127
124
@@ -142,4 +139,56 @@ public function get($key)
142
139
throw new AccessException ('" ' . $ key . '" doesn \'t exist in this object. ' );
143
140
}
144
141
}
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
+ }
145
194
}
0 commit comments