Skip to content

Commit d409754

Browse files
authored
Merge pull request #490 from onursimsek/feature/modifier-with-sub-relation
Allow sub relations with modifier
2 parents e03925f + b323c88 commit d409754

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/Manager.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public function parseIncludes($includes)
164164
{
165165
// Wipe these before we go again
166166
$this->requestedIncludes = $this->includeParams = [];
167+
$subRelations = '';
167168

168169
if (is_string($includes)) {
169170
$includes = explode(',', $includes);
@@ -177,6 +178,7 @@ public function parseIncludes($includes)
177178

178179
foreach ($includes as $include) {
179180
list($includeName, $allModifiersStr) = array_pad(explode(':', $include, 2), 2, null);
181+
list($allModifiersStr, $subRelations) = array_pad(explode('.', $allModifiersStr, 2), 2, null);
180182

181183
// Trim it down to a cool level of recursion
182184
$includeName = $this->trimToAcceptableRecursionLevel($includeName);
@@ -212,6 +214,10 @@ public function parseIncludes($includes)
212214
}
213215

214216
$this->includeParams[$includeName] = $modifierArr;
217+
218+
if ($subRelations) {
219+
$this->requestedIncludes[] = $this->trimToAcceptableRecursionLevel($includeName . '.' . $subRelations);
220+
}
215221
}
216222

217223
// This should be optional and public someday, but without it includes would never show up

test/ManagerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public function testParseIncludes()
5656
$manager->parseIncludes(['foo', 'foo', 'bar']);
5757
$this->assertSame(['foo', 'bar'], $manager->getRequestedIncludes());
5858

59+
$manager->parseIncludes(['foo.bar', 'foo:limit(10|1).bar']);
60+
$this->assertSame(['foo', 'foo.bar'], $manager->getRequestedIncludes());
61+
$this->assertSame(['10', '1'], $manager->getIncludeParams('foo')->get('limit'));
62+
5963
// Do requests for `baz.bart` also request `baz`?
6064
$manager->parseIncludes(['foo.bar']);
6165
$this->assertSame(['foo', 'foo.bar'], $manager->getRequestedIncludes());
@@ -74,6 +78,17 @@ public function testParseIncludes()
7478
$this->assertSame([''], $params['anotherparam']);
7579

7680
$this->assertNull($params['totallymadeup']);
81+
82+
// Relation with params and sub relation
83+
$manager->parseIncludes('foo:limit(5|1):order(name).bar,baz');
84+
85+
$params = $manager->getIncludeParams('foo');
86+
87+
$this->assertInstanceOf('League\Fractal\ParamBag', $params);
88+
89+
$this->assertSame(['5', '1'], $params['limit']);
90+
$this->assertSame(['name'], $params['order']);
91+
$this->assertSame(['foo', 'foo.bar', 'baz'], $manager->getRequestedIncludes());
7792
}
7893

7994
public function testParseExcludeSelfie()
@@ -151,6 +166,24 @@ public function testRecursionLimiting()
151166
$manager->getRequestedIncludes()
152167
);
153168

169+
$manager->parseIncludes('a:limit(5|1).b.c.d.e.f.g.h.i.j.NEVER');
170+
171+
$this->assertSame(
172+
[
173+
'a',
174+
'a.b',
175+
'a.b.c',
176+
'a.b.c.d',
177+
'a.b.c.d.e',
178+
'a.b.c.d.e.f',
179+
'a.b.c.d.e.f.g',
180+
'a.b.c.d.e.f.g.h',
181+
'a.b.c.d.e.f.g.h.i',
182+
'a.b.c.d.e.f.g.h.i.j',
183+
],
184+
$manager->getRequestedIncludes()
185+
);
186+
154187
// Try setting to 3 and see what happens
155188
$manager->setRecursionLimit(3);
156189
$manager->parseIncludes('a.b.c.NEVER');
@@ -163,6 +196,17 @@ public function testRecursionLimiting()
163196
],
164197
$manager->getRequestedIncludes()
165198
);
199+
200+
$manager->parseIncludes('a:limit(5|1).b.c.NEVER');
201+
202+
$this->assertSame(
203+
[
204+
'a',
205+
'a.b',
206+
'a.b.c',
207+
],
208+
$manager->getRequestedIncludes()
209+
);
166210
}
167211

168212
public function testCreateDataWithCallback()

0 commit comments

Comments
 (0)