Skip to content

Commit 7c60cd6

Browse files
committed
Merge pull request #23 from Art4/Bugfix_Links
Bugfix: links and pagination handling closes #23, closes #22, closes #21
2 parents 3a026c5 + ca3c453 commit 7c60cd6

22 files changed

+969
-229
lines changed

docs/objects-document-link.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ _[Symbols definition](objects-introduction.md#symbols)_
1515
| Key | Value | Note
1616
--- | ---- | ----- | ----
1717
? | self | `string` |
18-
? | related | `string` |
18+
? | related | - `string`<br />- [Link object](objects-link.md) |
1919
? | first | - `null`<br />- `string` |
2020
? | last | - `null`<br />- `string` |
2121
? | prev | - `null`<br />- `string` |
2222
? | next | - `null`<br />- `string` |
23+
? | meta | [Meta object](objects-meta.md) |
24+
* | `string` | - `string`<br />- [Link object](objects-link.md) |

docs/objects-error-link.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ _[Symbols definition](objects-introduction.md#symbols)_
1515
| Key | Value | Note
1616
--- | ---- | ----- | ----
1717
1 | about | - `string`<br />- [Link object](objects-link.md) |
18+
* | `string` | - `string`<br />- [Link object](objects-link.md) |

docs/objects-relationship-link.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ _[Symbols definition](objects-introduction.md#symbols)_
1515
| Key | Value | Note
1616
--- | ---- | ----- | ----
1717
# | self | `string` |
18-
# | related | `string` |
18+
# | related | - `string`<br />- [Link object](objects-link.md) |
1919
? | first | - `null`<br />- `string` | Only exists if the parent relationship object represents a to-many relationship |
2020
? | last | - `null`<br />- `string` | Only exists if the parent relationship object represents a to-many relationship |
2121
? | prev | - `null`<br />- `string` | Only exists if the parent relationship object represents a to-many relationship |
2222
? | next | - `null`<br />- `string` | Only exists if the parent relationship object represents a to-many relationship |
23+
? | meta | [Meta object](objects-meta.md) |
24+
* | `string` | - `string`<br />- [Link object](objects-link.md) |

src/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function __construct($object, FactoryManagerInterface $manager)
101101
{
102102
$this->container->set('links', $this->manager->getFactory()->make(
103103
'DocumentLink',
104-
[$object->links, $this->manager]
104+
[$object->links, $this->manager, $this]
105105
));
106106
}
107107

src/DocumentLink.php

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/ErrorLink.php

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,41 @@ public function __construct($object, FactoryManagerInterface $manager)
4545
throw new ValidationException('Link has to be an object, "' . gettype($object) . '" given.');
4646
}
4747

48-
if ( ! property_exists($object, 'about') )
48+
$links = get_object_vars($object);
49+
50+
if ( ! array_key_exists('about', $links) )
4951
{
5052
throw new ValidationException('ErrorLink MUST contain these properties: about');
5153
}
5254

53-
if ( ! is_string($object->about) and ! is_object($object->about) )
55+
if ( ! is_string($links['about']) and ! is_object($links['about']) )
5456
{
55-
throw new ValidationException('Link has to be an object or string, "' . gettype($object->about) . '" given.');
57+
throw new ValidationException('Link has to be an object or string, "' . gettype($links['about']) . '" given.');
5658
}
5759

5860
$this->manager = $manager;
5961

6062
$this->container = new DataContainer();
6163

62-
if ( is_string($object->about) )
64+
if ( is_string($links['about']) )
6365
{
64-
$this->container->set('about', strval($object->about));
65-
66-
return $this;
66+
$this->container->set('about', strval($links['about']));
67+
}
68+
else
69+
{
70+
$this->container->set('about', $this->manager->getFactory()->make(
71+
'Link',
72+
[$links['about'], $this->manager, $this]
73+
));
6774
}
6875

69-
$this->container->set('about', $this->manager->getFactory()->make(
70-
'Link',
71-
[$object->about, $this->manager]
72-
));
76+
unset($links['about']);
77+
78+
// custom links
79+
foreach ($links as $name => $value)
80+
{
81+
$this->setLink($name, $value);
82+
}
7383
}
7484

7585
/**
@@ -89,4 +99,33 @@ public function get($key)
8999
throw new AccessException('"' . $key . '" doesn\'t exist in this object.');
90100
}
91101
}
102+
/**
103+
* Set a link
104+
*
105+
* @param string $name The name of the link
106+
* @param string $link The link
107+
* @return self
108+
*/
109+
private function setLink($name, $link)
110+
{
111+
if ( ! is_string($link) and ! is_object($link) )
112+
{
113+
throw new ValidationException('Link attribute has to be an object or string, "' . gettype($link) . '" given.');
114+
}
115+
116+
if ( is_string($link) )
117+
{
118+
$this->container->set($name, strval($link));
119+
120+
return $this;
121+
}
122+
123+
// Now $link can only be an object
124+
$this->container->set($name, $this->manager->getFactory()->make(
125+
'Link',
126+
[$link, $this->manager, $this]
127+
));
128+
129+
return $this;
130+
}
92131
}

0 commit comments

Comments
 (0)