Skip to content

Commit

Permalink
Merge pull request #490 from onursimsek/feature/modifier-with-sub-rel…
Browse files Browse the repository at this point in the history
…ation

Allow sub relations with modifier
  • Loading branch information
matthewtrask authored Jan 24, 2020
2 parents e03925f + b323c88 commit d409754
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public function parseIncludes($includes)
{
// Wipe these before we go again
$this->requestedIncludes = $this->includeParams = [];
$subRelations = '';

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

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

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

$this->includeParams[$includeName] = $modifierArr;

if ($subRelations) {
$this->requestedIncludes[] = $this->trimToAcceptableRecursionLevel($includeName . '.' . $subRelations);
}
}

// This should be optional and public someday, but without it includes would never show up
Expand Down
44 changes: 44 additions & 0 deletions test/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public function testParseIncludes()
$manager->parseIncludes(['foo', 'foo', 'bar']);
$this->assertSame(['foo', 'bar'], $manager->getRequestedIncludes());

$manager->parseIncludes(['foo.bar', 'foo:limit(10|1).bar']);
$this->assertSame(['foo', 'foo.bar'], $manager->getRequestedIncludes());
$this->assertSame(['10', '1'], $manager->getIncludeParams('foo')->get('limit'));

// Do requests for `baz.bart` also request `baz`?
$manager->parseIncludes(['foo.bar']);
$this->assertSame(['foo', 'foo.bar'], $manager->getRequestedIncludes());
Expand All @@ -74,6 +78,17 @@ public function testParseIncludes()
$this->assertSame([''], $params['anotherparam']);

$this->assertNull($params['totallymadeup']);

// Relation with params and sub relation
$manager->parseIncludes('foo:limit(5|1):order(name).bar,baz');

$params = $manager->getIncludeParams('foo');

$this->assertInstanceOf('League\Fractal\ParamBag', $params);

$this->assertSame(['5', '1'], $params['limit']);
$this->assertSame(['name'], $params['order']);
$this->assertSame(['foo', 'foo.bar', 'baz'], $manager->getRequestedIncludes());
}

public function testParseExcludeSelfie()
Expand Down Expand Up @@ -151,6 +166,24 @@ public function testRecursionLimiting()
$manager->getRequestedIncludes()
);

$manager->parseIncludes('a:limit(5|1).b.c.d.e.f.g.h.i.j.NEVER');

$this->assertSame(
[
'a',
'a.b',
'a.b.c',
'a.b.c.d',
'a.b.c.d.e',
'a.b.c.d.e.f',
'a.b.c.d.e.f.g',
'a.b.c.d.e.f.g.h',
'a.b.c.d.e.f.g.h.i',
'a.b.c.d.e.f.g.h.i.j',
],
$manager->getRequestedIncludes()
);

// Try setting to 3 and see what happens
$manager->setRecursionLimit(3);
$manager->parseIncludes('a.b.c.NEVER');
Expand All @@ -163,6 +196,17 @@ public function testRecursionLimiting()
],
$manager->getRequestedIncludes()
);

$manager->parseIncludes('a:limit(5|1).b.c.NEVER');

$this->assertSame(
[
'a',
'a.b',
'a.b.c',
],
$manager->getRequestedIncludes()
);
}

public function testCreateDataWithCallback()
Expand Down

0 comments on commit d409754

Please sign in to comment.