Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 0211982

Browse files
committed
Merge pull request #43 from symfony-cmf/content_method-fixes
Improved and fixed ContentMethod::normalizePathElements
2 parents 394908e + c3c56cf commit 0211982

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

AutoRoute/PathProvider/ContentDateTimeProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ public function providePath(RouteStack $routeStack)
5454
throw new \RuntimeException(sprintf('Method %s:%s must return an instance of DateTime.'));
5555
}
5656

57-
$dateString = $date->format($this->dateFormat);
58-
$pathElements = $this->normalizePathElements($dateString);
57+
$string = $date->format($this->dateFormat);
58+
$pathElements = $this->normalizePathElements($string, $object);
5959

6060
$routeStack->addPathElements($pathElements);
6161
}
6262

6363
/**
6464
* {@inheritDoc}
6565
*/
66-
public function normalizePathElements($elements)
66+
public function normalizePathElements($elements, $object)
6767
{
68-
return parent::normalizePathElements(explode('/', $elements));
68+
return parent::normalizePathElements(explode('/', $elements), $object);
6969
}
7070
}

AutoRoute/PathProvider/ContentMethodProvider.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,30 @@ public function providePath(RouteStack $routeStack)
6868
}
6969

7070
$pathElements = $object->$method();
71-
72-
$pathElements = $this->normalizePathElements($pathElements);
71+
$pathElements = $this->normalizePathElements($pathElements, $object);
7372

7473

7574
// @todo: Validate the validator service.
7675
$routeStack->addPathElements($pathElements);
7776
}
7877

79-
protected function normalizePathElements($pathElements)
78+
/**
79+
* Normalize the given $pathElements variable to an array of path elements,
80+
* accepting either an array or a string.
81+
*
82+
* A string will be converted to an array of elements delimiteed by the
83+
* path separator.
84+
*
85+
* If slugify is enabled, each path element will be slugified.
86+
*
87+
* @param mixed $pathElements Either an array or a string
88+
* @param object $object Used in the case of an exception
89+
*
90+
* @return array
91+
*/
92+
protected function normalizePathElements($pathElements, $object)
8093
{
81-
if (is_string($pathElements)) {
94+
if (is_string($pathElements) || (is_object($pathElements) && method_exists($pathElements, '__toString'))) {
8295
if (substr($pathElements, 0, 1) == '/') {
8396
throw new \RuntimeException('Path must not be absolute.');
8497
}
@@ -88,9 +101,9 @@ protected function normalizePathElements($pathElements)
88101

89102
if (!is_array($pathElements)) {
90103
throw new \RuntimeException(sprintf(
91-
'FromObjectMethodProvider wants %s:%s to return an array of route names.. got "%s"',
104+
'FromObjectMethodProvider wants %s::%s to return an array of route names or a string, got "%s"',
92105
get_class($object),
93-
$method,
106+
$this->method,
94107
gettype($pathElements)
95108
));
96109
}

Tests/Unit/AutoRoute/PathProvider/ContentMethodProviderTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ public function testProvideMethodWithAbsolute()
115115
$this->provider->init(array('method' => 'getAbsoluteSlug'));
116116
$this->provider->providePath($this->routeStack);
117117
}
118+
119+
public function testProvideMethodObjectToString()
120+
{
121+
$this->setupTest();
122+
123+
$this->provider->init(array('method' => 'getStringObjectSlug'));
124+
$this->provider->providePath($this->routeStack);
125+
}
126+
127+
/**
128+
* @expectedException \RunTimeException
129+
*/
130+
public function testProvideMethodWrongType()
131+
{
132+
$this->setupTest();
133+
134+
$this->provider->init(array('method' => 'getWrongTypeSlug'));
135+
$this->provider->providePath($this->routeStack);
136+
}
118137
}
119138

120139
class ContentMethodTestClass
@@ -134,4 +153,22 @@ public function getAbsoluteSlug()
134153
return '/this/is/absolute';
135154
}
136155

156+
public function getStringObjectSlug()
157+
{
158+
return new StringObject();
159+
}
160+
161+
public function getWrongTypeSlug()
162+
{
163+
return new \StdClass();
164+
}
165+
166+
}
167+
168+
class StringObject
169+
{
170+
public function __toString()
171+
{
172+
return 'this/is/from/an/object';
173+
}
137174
}

0 commit comments

Comments
 (0)