Skip to content

Commit 35e74d0

Browse files
committed
Close #64
1 parent b3a8dd1 commit 35e74d0

File tree

10 files changed

+183
-89
lines changed

10 files changed

+183
-89
lines changed

src/Contracts/Schema/ResourceObjectInterface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,18 @@ public function getRelationshipsInclusionMeta();
8585
public function getSelfSubLink();
8686

8787
/**
88-
* If 'self' endpoint URL should be shown for resource in 'data' section.
88+
* Get links related to resource.
8989
*
90-
* @return bool
90+
* @return array
9191
*/
92-
public function isShowSelf();
92+
public function getResourceLinks();
9393

9494
/**
95-
* If 'self' endpoint URL should be shown for included resources.
95+
* Get links related to resource when it is in 'included' section.
9696
*
97-
* @return bool
97+
* @return array
9898
*/
99-
public function isShowSelfInIncluded();
99+
public function getIncludedResourceLinks();
100100

101101
/**
102102
* If resource attributes should be shown when the resource is within 'included'.

src/Contracts/Schema/SchemaProviderInterface.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,22 @@ public function createResourceObject($resource, $isOriginallyArrayed, $attribute
7878
public function getRelationshipObjectIterator($resource);
7979

8080
/**
81-
* If 'self' endpoint URL.
81+
* Get links related to resource.
8282
*
83-
* @return bool
83+
* @param mixed $resource
84+
*
85+
* @return array Array key is link name and value is LinkInterface.
8486
*/
85-
public function isShowSelf();
87+
public function getResourceLinks($resource);
8688

8789
/**
88-
* If 'self' endpoint URL should be shown for included resources.
90+
* Get links related to resource when it is in 'included' section.
8991
*
90-
* @return bool
92+
* @param mixed $resource
93+
*
94+
* @return array Array key is link name and value is LinkInterface.
9195
*/
92-
public function isShowSelfInIncluded();
96+
public function getIncludedResourceLinks($resource);
9397

9498
/**
9599
* If resource attributes should be shown when the resource is within 'included'.

src/Document/Presenters/ElementPresenter.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function convertDataResourceToArray(ResourceObjectInterface $resource, $i
148148
{
149149
return $this->convertResourceToArray(
150150
$resource,
151-
$resource->isShowSelf(),
151+
$resource->getResourceLinks(),
152152
$resource->getPrimaryMeta(),
153153
$isShowAttributes
154154
);
@@ -165,7 +165,7 @@ public function convertIncludedResourceToArray(ResourceObjectInterface $resource
165165
{
166166
return $this->convertResourceToArray(
167167
$resource,
168-
$resource->isShowSelfInIncluded(),
168+
$resource->getIncludedResourceLinks(),
169169
$resource->getInclusionMeta(),
170170
$resource->isShowAttributesInIncluded()
171171
);
@@ -277,13 +277,13 @@ private function getRelationRepresentation(
277277
* Convert resource object to array.
278278
*
279279
* @param ResourceObjectInterface $resource
280-
* @param bool $isShowSelf
280+
* @param array $resourceLinks
281281
* @param mixed $meta
282282
* @param bool $isShowAttributes
283283
*
284284
* @return array
285285
*/
286-
private function convertResourceToArray(ResourceObjectInterface $resource, $isShowSelf, $meta, $isShowAttributes)
286+
private function convertResourceToArray(ResourceObjectInterface $resource, $resourceLinks, $meta, $isShowAttributes)
287287
{
288288
$representation = [
289289
Document::KEYWORD_TYPE => $resource->getType(),
@@ -304,9 +304,12 @@ private function convertResourceToArray(ResourceObjectInterface $resource, $isSh
304304
// links and meta which is not visually beautiful
305305
$representation[Document::KEYWORD_RELATIONSHIPS] = null;
306306

307-
if ($isShowSelf === true) {
308-
$representation[Document::KEYWORD_LINKS][Document::KEYWORD_SELF] =
309-
$this->getLinkRepresentation($this->document->getUrlPrefix(), $resource->getSelfSubLink());
307+
if (empty($resourceLinks) === false) {
308+
foreach ($resourceLinks as $linkName => $link) {
309+
/** @var LinkInterface $link */
310+
$representation[Document::KEYWORD_LINKS][$linkName] =
311+
$this->getLinkRepresentation($this->document->getUrlPrefix(), $link);
312+
}
310313
}
311314

312315
if ($meta !== null) {

src/Schema/ResourceIdentifierSchemaAdapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ public function getRelationshipObjectIterator($resource)
9797
/**
9898
* @inheritdoc
9999
*/
100-
public function isShowSelf()
100+
public function getResourceLinks($resource)
101101
{
102-
return false;
102+
return [];
103103
}
104104

105105
/**
106106
* @inheritdoc
107107
*/
108-
public function isShowSelfInIncluded()
108+
public function getIncludedResourceLinks($resource)
109109
{
110-
return false;
110+
return [];
111111
}
112112

113113
/**

src/Schema/ResourceObject.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,17 @@ public function getSelfSubLink()
186186
/**
187187
* @inheritdoc
188188
*/
189-
public function isShowSelf()
189+
public function getResourceLinks()
190190
{
191-
return $this->schema->isShowSelf();
191+
return $this->schema->getResourceLinks($this->resource);
192192
}
193193

194194
/**
195195
* @inheritdoc
196196
*/
197-
public function isShowSelfInIncluded()
197+
public function getIncludedResourceLinks()
198198
{
199-
return $this->schema->isShowSelfInIncluded();
199+
return $this->schema->getIncludedResourceLinks($this->resource);
200200
}
201201

202202
/**

src/Schema/SchemaProvider.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@ abstract class SchemaProvider implements SchemaProviderInterface
6666
protected $selfSubUrl;
6767

6868
/**
69+
* @deprecated Override getResourceLinks instead
70+
*
6971
* @var bool
7072
*/
7173
protected $isShowSelf = true;
7274

7375
/**
76+
* @deprecated Override getIncludedResourceLinks instead
77+
*
7478
* @var bool
7579
*/
7680
protected $isShowSelfInIncluded = false;
@@ -83,7 +87,7 @@ abstract class SchemaProvider implements SchemaProviderInterface
8387
/**
8488
* @var bool
8589
*/
86-
protected $isShowRelShipsInIncluded = false;
90+
protected $isShowRelShipsInIncluded = true;
8791

8892
/**
8993
* @var SchemaFactoryInterface
@@ -166,22 +170,6 @@ public function getRelationshipsInclusionMeta($resource)
166170
return null;
167171
}
168172

169-
/**
170-
* @inheritdoc
171-
*/
172-
public function isShowSelf()
173-
{
174-
return $this->isShowSelf;
175-
}
176-
177-
/**
178-
* @inheritdoc
179-
*/
180-
public function isShowSelfInIncluded()
181-
{
182-
return $this->isShowSelfInIncluded;
183-
}
184-
185173
/**
186174
* @inheritdoc
187175
*/
@@ -237,6 +225,34 @@ public function getIncludePaths()
237225
return [];
238226
}
239227

228+
/**
229+
* @inheritdoc
230+
*/
231+
public function getResourceLinks($resource)
232+
{
233+
$links = [];
234+
235+
if ($this->isShowSelf === true) {
236+
$links[LinkInterface::SELF] = $this->getSelfSubLink($resource);
237+
}
238+
239+
return $links;
240+
}
241+
242+
/**
243+
* @inheritdoc
244+
*/
245+
public function getIncludedResourceLinks($resource)
246+
{
247+
$links = [];
248+
249+
if ($this->isShowSelfInIncluded === true) {
250+
$links[LinkInterface::SELF] = $this->getSelfSubLink($resource);
251+
}
252+
253+
return $links;
254+
}
255+
240256
/**
241257
* @param string $relationshipName
242258
* @param array $description

tests/Data/DevSchemaProvider.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
use \Closure;
1920
use \Neomerx\JsonApi\Schema\SchemaProvider;
2021

2122
/**
@@ -50,6 +51,11 @@ abstract class DevSchemaProvider extends SchemaProvider
5051
*/
5152
private $relationshipsMeta;
5253

54+
/**
55+
* @var Closure
56+
*/
57+
private $resourceLinksClosure = null;
58+
5359
/**
5460
* @inheritdoc
5561
*/
@@ -66,6 +72,18 @@ public function getRelationshipsInclusionMeta($resource)
6672
return $this->relationshipsMeta ?: parent::getRelationshipsInclusionMeta($resource);
6773
}
6874

75+
/**
76+
* @inheritdoc
77+
*/
78+
public function getResourceLinks($resource)
79+
{
80+
if (($linksClosure = $this->resourceLinksClosure) === null) {
81+
return parent::getResourceLinks($resource);
82+
} else {
83+
return $linksClosure($resource);
84+
}
85+
}
86+
6987
/**
7088
* @param array $relationshipMeta
7189
*/
@@ -74,6 +92,14 @@ public function setRelationshipsMeta($relationshipMeta)
7492
$this->relationshipsMeta = $relationshipMeta;
7593
}
7694

95+
/**
96+
* @param Closure $linksClosure
97+
*/
98+
public function setResourceLinksClosure(Closure $linksClosure)
99+
{
100+
$this->resourceLinksClosure = $linksClosure;
101+
}
102+
77103
/**
78104
* Add to 'add to link' list.
79105
*

0 commit comments

Comments
 (0)