This repository was archived by the owner on Jan 29, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +33
-11
lines changed Expand file tree Collapse file tree 4 files changed +33
-11
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+ declare (strict_types=1 );
3
+
4
+ namespace Zend \Expressive \Hal \Metadata ;
5
+
6
+ interface DepthAwareMetadataInterface
7
+ {
8
+ /**
9
+ * Returns configured max depth
10
+ *
11
+ * @return int
12
+ */
13
+ public function getMaxDepth (): int ;
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
+ }
Original file line number Diff line number Diff line change 7
7
8
8
namespace Zend \Expressive \Hal \Metadata ;
9
9
10
- class RouteBasedResourceMetadata extends AbstractResourceMetadata
10
+ class RouteBasedResourceMetadata extends AbstractResourceMetadata implements DepthAwareMetadataInterface
11
11
{
12
12
/** @var string */
13
13
private $ resourceIdentifier ;
@@ -71,4 +71,9 @@ public function getMaxDepth(): int
71
71
{
72
72
return $ this ->maxDepth ;
73
73
}
74
+
75
+ public function hasReachedMaxDepth (int $ currentDepth ): bool
76
+ {
77
+ return $ currentDepth > $ this ->maxDepth ;
78
+ }
74
79
}
Original file line number Diff line number Diff line change 10
10
use Psr \Http \Message \ServerRequestInterface ;
11
11
use Zend \Expressive \Hal \Metadata \AbstractCollectionMetadata ;
12
12
use Zend \Expressive \Hal \Metadata \AbstractMetadata ;
13
+ use Zend \Expressive \Hal \Metadata \DepthAwareMetadataInterface ;
13
14
use Zend \Expressive \Hal \Metadata \RouteBasedResourceMetadata ;
14
15
use Zend \Expressive \Hal \ResourceGenerator ;
15
16
use Zend \Hydrator \ExtractionInterface ;
@@ -39,14 +40,8 @@ private function extractInstance(
39
40
40
41
$ array = $ extractor ->extract ($ instance );
41
42
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
- }
43
+ if ($ metadata instanceof DepthAwareMetadataInterface && $ metadata ->hasReachedMaxDepth ($ depth )) {
44
+ return $ array ;
50
45
}
51
46
52
47
// Extract nested resources if present in metadata map
Original file line number Diff line number Diff line change @@ -47,8 +47,7 @@ public function createResource(
47
47
$ routeParams [$ routeIdentifier ] = $ data [$ resourceIdentifier ];
48
48
}
49
49
50
- $ maxDepth = $ metadata ->getMaxDepth ();
51
- if ($ depth > $ maxDepth ) {
50
+ if ($ metadata instanceof Metadata \DepthAwareMetadataInterface && $ metadata ->hasReachedMaxDepth ($ depth )) {
52
51
$ data = [];
53
52
}
54
53
You can’t perform that action at this time.
0 commit comments