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

Commit c985716

Browse files
tobias-trozowskiweierophinney
authored andcommitted
added MetadataInterface
1 parent 43772a4 commit c985716

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

src/Metadata/AbstractMetadata.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use Zend\Expressive\Hal\LinkCollection;
1111

12-
abstract class AbstractMetadata
12+
abstract class AbstractMetadata implements MetadataInterface
1313
{
1414
use LinkCollection;
1515

@@ -22,4 +22,9 @@ public function getClass() : string
2222
{
2323
return $this->class;
2424
}
25+
26+
public function hasReachedMaxDepth(int $currentDepth): bool
27+
{
28+
return false;
29+
}
2530
}

src/Metadata/MetadataInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Zend\Expressive\Hal\Metadata;
5+
6+
interface MetadataInterface
7+
{
8+
/**
9+
* Returns the configured metadata class name
10+
*
11+
* @return string
12+
*/
13+
public function getClass() : string;
14+
15+
/**
16+
* Determines whenever the current depth level exceeds the allowed max depth
17+
*
18+
* @param int $currentDepth
19+
*
20+
* @return bool
21+
*/
22+
public function hasReachedMaxDepth(int $currentDepth): bool;
23+
}

src/Metadata/RouteBasedResourceMetadata.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public function setRouteParams(array $routeParams) : void
6767
$this->routeParams = $routeParams;
6868
}
6969

70-
public function getMaxDepth(): int
70+
public function hasReachedMaxDepth(int $currentDepth): bool
7171
{
72-
return $this->maxDepth;
72+
return $currentDepth > $this->maxDepth;
7373
}
7474
}

src/ResourceGenerator/ExtractInstanceTrait.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Psr\Http\Message\ServerRequestInterface;
1111
use Zend\Expressive\Hal\Metadata\AbstractCollectionMetadata;
1212
use Zend\Expressive\Hal\Metadata\AbstractMetadata;
13-
use Zend\Expressive\Hal\Metadata\RouteBasedResourceMetadata;
1413
use Zend\Expressive\Hal\ResourceGenerator;
1514
use Zend\Hydrator\ExtractionInterface;
1615

@@ -39,14 +38,8 @@ private function extractInstance(
3938

4039
$array = $extractor->extract($instance);
4140

42-
if ($metadata instanceof RouteBasedResourceMetadata) {
43-
$maxDepth = $metadata->getMaxDepth();
44-
if ($depth > $maxDepth) {
45-
$resourceIdentifier = $metadata->getResourceIdentifier();
46-
return [
47-
$resourceIdentifier => $array[$resourceIdentifier]
48-
];
49-
}
41+
if ($metadata->hasReachedMaxDepth($depth)) {
42+
return $array;
5043
}
5144

5245
// Extract nested resources if present in metadata map

src/ResourceGenerator/RouteBasedResourceStrategy.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public function createResource(
4747
$routeParams[$routeIdentifier] = $data[$resourceIdentifier];
4848
}
4949

50-
$maxDepth = $metadata->getMaxDepth();
51-
if ($depth > $maxDepth) {
50+
if ($metadata->hasReachedMaxDepth($depth)) {
5251
$data = [];
5352
}
5453

0 commit comments

Comments
 (0)