Skip to content

Commit

Permalink
Do not alter original path
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Dec 19, 2013
1 parent 917b2cd commit ff423d9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 54 deletions.
103 changes: 52 additions & 51 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,55 +1,56 @@
{
"name": "zfr/zfr-rest",
"description": "Zend Framework 2 REST Module.",
"type": "library",
"license": "MIT",
"keywords": [
"zf2",
"zend framework",
"rest",
"restful",
"api"
],
"homepage": "https://github.com/zf-fr/zfr-rest",
"authors": [
{
"name": "Michaël Gallego",
"email": "mic.gallego@gmail.com",
"homepage": "http://www.michaelgallego.fr/"
},
{
"name": "Marco Pivetta",
"email": "ocramius@gmail.com",
"homepage": "http://marco-pivetta.com/"
}
],
"minimum-stability": "beta",
"require": {
"php": ">=5.4",
"jms/metadata": "~1.5",
"doctrine/common": "~2.4",
"doctrine/doctrine-module": "~0.8",
"zendframework/zend-cache": "~2.2",
"zendframework/zend-http": "~2.2",
"zendframework/zend-inputfilter": "~2.2",
"zendframework/zend-mvc": "~2.2",
"zendframework/zend-modulemanager": "~2.2",
"zendframework/zend-paginator": "~2.2",
"zendframework/zend-servicemanager": "~2.2",
"zendframework/zend-view": "~2.2",
"zendframework/zend-stdlib": "~2.2"
"name": "zfr/zfr-rest",
"description": "Zend Framework 2 REST Module.",
"type": "library",
"license": "MIT",
"keywords": [
"zf2",
"zend framework",
"rest",
"restful",
"api"
],
"homepage": "https://github.com/zf-fr/zfr-rest",
"authors": [
{
"name": "Michaël Gallego",
"email": "mic.gallego@gmail.com",
"homepage": "http://www.michaelgallego.fr/"
},
"require-dev": {
"zendframework/zend-config": "~2.2",
"zendframework/zend-serializer": "~2.2",
"doctrine/doctrine-orm-module": "~0.8",
"phpunit/phpunit": "~3.7",
"squizlabs/php_codesniffer": "1.4.*",
"satooshi/php-coveralls": "~0.6"
},
"autoload": {
"psr-0": {
"ZfrRest\\": "src/"
}
{
"name": "Marco Pivetta",
"email": "ocramius@gmail.com",
"homepage": "http://marco-pivetta.com/"
}
],
"minimum-stability": "beta",
"require": {
"php": ">=5.4",
"jms/metadata": "~1.5",
"doctrine/common": "~2.4",
"doctrine/doctrine-module": "~0.8",
"zendframework/zend-cache": "~2.2",
"zendframework/zend-http": "~2.2",
"zendframework/zend-inputfilter": "~2.2",
"zendframework/zend-filter": "~2.2",
"zendframework/zend-mvc": "~2.2",
"zendframework/zend-modulemanager": "~2.2",
"zendframework/zend-paginator": "~2.2",
"zendframework/zend-servicemanager": "~2.2",
"zendframework/zend-view": "~2.2",
"zendframework/zend-stdlib": "~2.2"
},
"require-dev": {
"zendframework/zend-config": "~2.2",
"zendframework/zend-serializer": "~2.2",
"doctrine/doctrine-orm-module": "~0.8",
"phpunit/phpunit": "~3.7",
"squizlabs/php_codesniffer": "1.4.*",
"satooshi/php-coveralls": "~0.6"
},
"autoload": {
"psr-0": {
"ZfrRest\\": "src/"
}
}
}
14 changes: 12 additions & 2 deletions src/ZfrRest/Router/Http/Matcher/AssociationSubPathMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace ZfrRest\Router\Http\Matcher;

use Metadata\MetadataFactory;
use Zend\Filter\Word\DashToCamelCase;
use ZfrRest\Resource\Resource;
use ZfrRest\Resource\ResourceInterface;

Expand All @@ -40,12 +41,18 @@ class AssociationSubPathMatcher implements SubPathMatcherInterface
*/
protected $metadataFactory;

/**
* @var DashToCamelCase
*/
protected $inflector;

/**
* @param MetadataFactory $metadataFactory
*/
public function __construct(MetadataFactory $metadataFactory)
{
$this->metadataFactory = $metadataFactory;
$this->inflector = new DashToCamelCase();
}

/**
Expand All @@ -57,14 +64,17 @@ public function matchSubPath(ResourceInterface $resource, $subPath, SubPathMatch
$pathChunks = explode('/', $subPath);
$associationName = array_shift($pathChunks);

// Most of the time, the convention in URI is too have dash-separated paths, so we inflect it
$inflectedAssociationName = lcfirst($this->inflector->filter($associationName));

$resourceMetadata = $resource->getMetadata();

if (!$resourceMetadata->hasAssociation($associationName)) {
if (!$resourceMetadata->hasAssociation($inflectedAssociationName)) {
return null;
}

$classMetadata = $resourceMetadata->getClassMetadata();
$associationTargetClass = $classMetadata->getAssociationTargetClass($associationName);
$associationTargetClass = $classMetadata->getAssociationTargetClass($inflectedAssociationName);
$associationMetadata = $this->metadataFactory->getMetadataForClass($associationTargetClass)
->getOutsideClassMetadata();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testCanMatchAssociation($subPath)

$classHierarchy = $this->getMock('Metadata\ClassHierarchyMetadata');
$classHierarchy->expects($this->once())->method('getOutsideClassMetadata')->will($this->returnValue($associationMetadata));

$this->metadataFactory->expects($this->once())
->method('getMetadataForClass')
->with('ZfrRestTest\Asset\Router\AssociationMatcherEntity')
Expand All @@ -136,4 +136,18 @@ public function testCanMatchAssociation($subPath)
$this->assertEquals($associationName, $result->getMatchedPath());
$this->assertNull($result->getPreviousMatch());
}

public function testCanInflectDashSeparatedPaths()
{
$resource = $this->getMock('ZfrRest\Resource\ResourceInterface');
$metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface');

$resource->expects($this->once())->method('getMetadata')->will($this->returnValue($metadata));
$metadata->expects($this->once())
->method('hasAssociation')
->with('fooBar')
->will($this->returnValue(false));

$this->assertNull($this->associationMatcher->matchSubPath($resource, 'foo-bar'));
}
}

0 comments on commit ff423d9

Please sign in to comment.