Skip to content

Commit

Permalink
Add terminal path
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed May 27, 2014
1 parent 3567025 commit 477c6f5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 0.3.2

* Added support for OneToOne association (assuming you have a "User" entity with a OneToOne association to
a "Card" entity, you can now do a POST request like "/users/4/card")

## 0.3.1

* ZfrRest now returns input errors correctly if no data was given in the body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ public function matchSubPath(ResourceInterface $resource, $subPath, SubPathMatch
$reflectionProperty->setAccessible(true);

$associationData = $reflectionProperty->getValue($resource->getData());
$terminal = false;

if ($associationData === null && $classMetadata->isSingleValuedAssociation($associationName)) {
$resource = $associationResourceMetadata->createResource();

// We set this match as terminal, so that paths like "/user/4/twitter/tweets/123" don't end
// up creating a lot of "non-existant" resources, that would be very strange to handle
$terminal = true;
} else {
$resource = new Resource($associationData, $associationResourceMetadata);
}

return new SubPathMatch($resource, $associationPath, $previousMatch);
return new SubPathMatch($resource, $associationPath, $previousMatch, $terminal);
}
}
2 changes: 1 addition & 1 deletion src/ZfrRest/Router/Http/Matcher/BaseSubPathMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function matchSubPath(ResourceInterface $resource, $subPath, SubPathMatch
$subPath = trim($subPath, '/');

// We have traversed the whole path, return the last matched path!
if (empty($subPath)) {
if (empty($subPath) || ($previousMatch && $previousMatch->isTerminal())) {
return $previousMatch ?: new SubPathMatch($resource, $subPath);
}

Expand Down
25 changes: 23 additions & 2 deletions src/ZfrRest/Router/Http/Matcher/SubPathMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,27 @@ class SubPathMatch
*/
protected $previousMatch;

/**
* @var bool
*/
protected $terminal;

/**
* @param ResourceInterface $matchedResource
* @param string $matchedPath
* @param SubPathMatch|null $previousMatch
* @param bool $terminal
*/
public function __construct(ResourceInterface $matchedResource, $matchedPath, SubPathMatch $previousMatch = null)
{
public function __construct(
ResourceInterface $matchedResource,
$matchedPath,
SubPathMatch $previousMatch = null,
$terminal = false
) {
$this->matchedResource = $matchedResource;
$this->matchedPath = $matchedPath;
$this->previousMatch = $previousMatch;
$this->terminal = (bool) $terminal;
}

/**
Expand Down Expand Up @@ -85,4 +96,14 @@ public function getPreviousMatch()
{
return $this->previousMatch;
}

/**
* Get if this sub path match is a terminal path
*
* @return bool
*/
public function isTerminal()
{
return $this->terminal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public function testCanMatchAssociation($subPath, $associationPath, $propertyNam
$this->assertSame($associationMetadata, $result->getMatchedResource()->getMetadata());
$this->assertEquals($associationPath, $result->getMatchedPath());
$this->assertNull($result->getPreviousMatch());
$this->assertFalse($result->isTerminal());
}

public function testWontMatchWhenRoutableIsSetToFalse()
Expand Down Expand Up @@ -240,5 +241,6 @@ public function testCanCreateEmptyResourceForSingleValuedAssociation()

$this->assertInstanceOf('ZfrRest\Router\Http\Matcher\SubPathMatch', $result);
$this->assertInstanceOf('ZfrRest\Resource\ResourceInterface', $result->getMatchedResource());
$this->assertTrue($result->isTerminal());
}
}

0 comments on commit 477c6f5

Please sign in to comment.