Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 35a0ff1

Browse files
added new MetadataInterface
added missing param in doc-blocks fixed ExtractInstanceTrait::extractInstance signature (AbstractMetadata -> AbstractResourceMetadata) fixed few return types fixed useless string conversion in Link constructor changed generateSelfLink and generateLinkForPage signature in RouteBasedCollectionStrategy to be more specific changed UrlBasedCollectionStrategy::generateSelfLink signature to be more specific changed few lambda function to static wherever possible removed DepthAwareMetadataInterface
1 parent 7e03009 commit 35a0ff1

16 files changed

+43
-46
lines changed

src/HalResource.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function __construct(array $data = [], array $links = [], array $embedded
8383
$this->embedded[$name] = $resource;
8484
});
8585

86-
if (array_reduce($links, function ($containsNonLinkItem, $link) {
86+
if (array_reduce($links, static function ($containsNonLinkItem, $link) {
8787
return $containsNonLinkItem || ! $link instanceof LinkInterface;
8888
}, false)) {
8989
throw new InvalidArgumentException('Non-Link item provided in $links array');
@@ -392,7 +392,7 @@ private function isResourceCollection($value, string $name, string $context) : b
392392

393393
private function serializeLinks()
394394
{
395-
$relations = array_reduce($this->links, function (array $byRelation, LinkInterface $link) {
395+
$relations = array_reduce($this->links, static function (array $byRelation, LinkInterface $link) {
396396
$representation = array_merge($link->getAttributes(), [
397397
'href' => $link->getHref(),
398398
]);
@@ -401,7 +401,7 @@ private function serializeLinks()
401401
}
402402

403403
$linkRels = $link->getRels();
404-
array_walk($linkRels, function ($rel) use (&$byRelation, $representation) {
404+
array_walk($linkRels, static function ($rel) use (&$byRelation, $representation) {
405405
$forceCollection = array_key_exists(Link::AS_COLLECTION, $representation)
406406
? (bool) $representation[Link::AS_COLLECTION]
407407
: false;
@@ -431,7 +431,7 @@ private function serializeLinks()
431431
return $byRelation;
432432
}, []);
433433

434-
array_walk($relations, function ($links, $key) use (&$relations) {
434+
array_walk($relations, static function ($links, $key) use (&$relations) {
435435
if (isset($relations[$key][Link::AS_COLLECTION])) {
436436
// If forcing a collection, do nothing to the links, but DO
437437
// remove the marker indicating a collection should be
@@ -452,7 +452,7 @@ private function serializeEmbeddedResources()
452452
array_walk($this->embedded, function ($resource, $name) use (&$embedded) {
453453
$embedded[$name] = $resource instanceof self
454454
? $resource->toArray()
455-
: array_map(function ($item) {
455+
: array_map(static function ($item) {
456456
return $item->toArray();
457457
}, $resource);
458458
});

src/HalResponseFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(
4646
Renderer\XmlRenderer $xmlRenderer = null
4747
) {
4848
// Ensures type safety of the composed factory
49-
$this->responseFactory = function () use ($responseFactory) : ResponseInterface {
49+
$this->responseFactory = static function () use ($responseFactory) : ResponseInterface {
5050
return $responseFactory();
5151
};
5252
$this->jsonRenderer = $jsonRenderer ?: new Renderer\JsonRenderer();

src/HalResponseFactoryFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Psr\Container\ContainerInterface;
1111
use Psr\Http\Message\ResponseInterface;
12+
use RuntimeException;
1213

1314
/**
1415
* Create and return a HalResponseFactory instance.

src/Link.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Link implements EvolvableLinkInterface
4848

4949
/**
5050
* @param string|string[] $relation One or more relations represented by this link.
51-
* @param string|object $uri
51+
* @param string $uri
5252
* @param bool $isTemplated
5353
* @param array $attributes
5454
* @throws InvalidArgumentException if $relation is neither a string nor an array.
@@ -58,7 +58,7 @@ class Link implements EvolvableLinkInterface
5858
public function __construct($relation, string $uri = '', bool $isTemplated = false, array $attributes = [])
5959
{
6060
$this->relations = $this->validateRelation($relation);
61-
$this->uri = is_string($uri) ? $uri : (string) $uri;
61+
$this->uri = $uri;
6262
$this->isTemplated = $isTemplated;
6363
$this->attributes = $this->validateAttributes($attributes);
6464
}
@@ -153,7 +153,7 @@ public function withoutRel($rel)
153153
}
154154

155155
$new = clone $this;
156-
$new->relations = array_filter($this->relations, function ($value) use ($rel) {
156+
$new->relations = array_filter($this->relations, static function ($value) use ($rel) {
157157
return $rel !== $value;
158158
});
159159
return $new;
@@ -227,7 +227,7 @@ private function validateAttributeValue($value, string $context)
227227
));
228228
}
229229

230-
if (is_array($value) && array_reduce($value, function ($isInvalid, $value) {
230+
if (is_array($value) && array_reduce($value, static function ($isInvalid, $value) {
231231
return $isInvalid || ! is_string($value);
232232
}, false)) {
233233
throw new InvalidArgumentException(sprintf(
@@ -260,7 +260,7 @@ private function validateRelation($relation)
260260
));
261261
}
262262

263-
if (is_array($relation) && false === array_reduce($relation, function ($isString, $value) {
263+
if (is_array($relation) && false === array_reduce($relation, static function ($isString, $value) {
264264
return $isString === false || is_string($value) || empty($value);
265265
}, true)) {
266266
throw new InvalidArgumentException(

src/LinkCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getLinks()
3737
*/
3838
public function getLinksByRel($rel)
3939
{
40-
return array_filter($this->links, function (LinkInterface $link) use ($rel) {
40+
return array_filter($this->links, static function (LinkInterface $link) use ($rel) {
4141
$rels = $link->getRels();
4242
return in_array($rel, $rels, true);
4343
});
@@ -67,7 +67,7 @@ public function withoutLink(LinkInterface $link)
6767
}
6868

6969
$new = clone $this;
70-
$new->links = array_filter($this->links, function (LinkInterface $compare) use ($link) {
70+
$new->links = array_filter($this->links, static function (LinkInterface $compare) use ($link) {
7171
return $link !== $compare;
7272
});
7373
return $new;

src/Metadata/AbstractMetadata.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77

88
namespace Zend\Expressive\Hal\Metadata;
99

10-
use Zend\Expressive\Hal\LinkCollection;
11-
12-
abstract class AbstractMetadata
10+
abstract class AbstractMetadata implements MetadataInterface
1311
{
14-
use LinkCollection;
15-
1612
/**
1713
* @var string
1814
*/
@@ -22,4 +18,9 @@ public function getClass() : string
2218
{
2319
return $this->class;
2420
}
21+
22+
public function hasReachedMaxDepth(int $currentDepth): bool
23+
{
24+
return false;
25+
}
2526
}

src/Metadata/DepthAwareMetadataInterface.php renamed to src/Metadata/MetadataInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
namespace Zend\Expressive\Hal\Metadata;
55

6-
interface DepthAwareMetadataInterface
6+
interface MetadataInterface
77
{
88
/**
9-
* Returns configured max depth
9+
* Returns the configured metadata class name
1010
*
11-
* @return int
11+
* @return string
1212
*/
13-
public function getMaxDepth(): int;
13+
public function getClass() : string;
1414

1515
/**
1616
* Determines whenever the current depth level exceeds the allowed max depth

src/Metadata/MetadataMapFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private function populateMetadataMapFromConfig(
9191
* @throws Exception\InvalidConfigException if no matching `create*()`
9292
* method is found for the "__class__" entry.
9393
*/
94-
private function injectMetadata(MetadataMap $metadataMap, array $metadata, array $metadataFactories)
94+
private function injectMetadata(MetadataMap $metadataMap, array $metadata, array $metadataFactories): void
9595
{
9696
if (! isset($metadata['__class__'])) {
9797
throw Exception\InvalidConfigException::dueToMissingMetadataClass();

src/Metadata/RouteBasedResourceMetadata.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Zend\Expressive\Hal\Metadata;
99

10-
class RouteBasedResourceMetadata extends AbstractResourceMetadata implements DepthAwareMetadataInterface
10+
class RouteBasedResourceMetadata extends AbstractResourceMetadata
1111
{
1212
/** @var string */
1313
private $resourceIdentifier;
@@ -67,11 +67,6 @@ public function setRouteParams(array $routeParams) : void
6767
$this->routeParams = $routeParams;
6868
}
6969

70-
public function getMaxDepth(): int
71-
{
72-
return $this->maxDepth;
73-
}
74-
7570
public function hasReachedMaxDepth(int $currentDepth): bool
7671
{
7772
return $currentDepth > $this->maxDepth;

src/Renderer/XmlRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private function createResourceNode(DOMDocument $doc, array $resource, string $r
7878
return $this->createNodeTree($doc, $node, $resource);
7979
}
8080

81-
private function createLinkNode(DOMDocument $doc, string $rel, array $data)
81+
private function createLinkNode(DOMDocument $doc, string $rel, array $data): \DOMElement
8282
{
8383
$link = $doc->createElement('link');
8484
$link->setAttribute('rel', $rel);

0 commit comments

Comments
 (0)