1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Gerard van Helden <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
namespace Zicht\Bundle\UrlBundle\Aliasing; |
7
|
|
|
|
8
|
|
|
use Zicht\Util\Str; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* An aliasing strategy which simply puts the systemized title at the specified base path (default is root) |
12
|
|
|
*/ |
13
|
|
|
class DefaultAliasingStrategy implements AliasingStrategy |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Construct with the specified base path |
17
|
|
|
* |
18
|
|
|
* @param string $basePath |
19
|
|
|
*/ |
20
|
6 |
|
public function __construct($basePath = '/') |
21
|
|
|
{ |
22
|
6 |
|
$this->basePath = $basePath; |
|
|
|
|
23
|
6 |
|
} |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns the calculated public alias for the specified object. |
28
|
|
|
* |
29
|
|
|
* @param string $subject |
30
|
|
|
* @param string $currentAlias |
31
|
|
|
* @return null|string |
32
|
|
|
* @throws \InvalidArgumentException |
33
|
|
|
*/ |
34
|
6 |
|
public function generatePublicAlias($subject, $currentAlias = '') |
35
|
|
|
{ |
36
|
6 |
|
if (is_object($subject)) { |
|
|
|
|
37
|
4 |
|
if ($subject instanceof Aliasable) { |
38
|
1 |
|
$subject = (string)$subject->getAliasTitle(); |
39
|
3 |
|
} elseif (method_exists($subject, 'getTitle')) { |
40
|
1 |
|
$subject = (string)$subject->getTitle(); |
41
|
|
|
} else { |
42
|
2 |
|
$subject = (string)$subject; |
43
|
|
|
} |
44
|
|
|
} |
45
|
6 |
|
if (!is_string($subject)) { |
|
|
|
|
46
|
1 |
|
throw new \InvalidArgumentException("Expected a string or object as subject, got " . gettype($subject)); |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
if ($alias = $this->toAlias($subject)) { |
50
|
4 |
|
return $this->basePath . $alias; |
51
|
|
|
} |
52
|
1 |
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Systemizes the specified string. |
58
|
|
|
* |
59
|
|
|
* @param string $title |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
5 |
|
protected function toAlias($title) |
63
|
|
|
{ |
64
|
5 |
|
return Str::systemize($title); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|