|
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
|
|
|
public function __construct($basePath = '/') |
|
21
|
|
|
{ |
|
22
|
|
|
$this->basePath = $basePath; |
|
|
|
|
|
|
23
|
|
|
} |
|
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
|
|
|
public function generatePublicAlias($subject, $currentAlias = '') |
|
35
|
|
|
{ |
|
36
|
|
|
if (is_object($subject)) { |
|
|
|
|
|
|
37
|
|
|
if ($subject instanceof Aliasable) { |
|
38
|
|
|
$subject = (string)$subject->getAliasTitle(); |
|
39
|
|
|
} elseif (method_exists($subject, 'getTitle')) { |
|
40
|
|
|
$subject = (string)$subject->getTitle(); |
|
41
|
|
|
} else { |
|
42
|
|
|
$subject = (string)$subject; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
if (!is_string($subject)) { |
|
|
|
|
|
|
46
|
|
|
throw new \InvalidArgumentException('Expected a string or object as subject, got ' . gettype($subject)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ($alias = $this->toAlias($subject)) { |
|
50
|
|
|
return $this->basePath . $alias; |
|
51
|
|
|
} |
|
52
|
|
|
return null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Systemizes the specified string. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $title |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function toAlias($title) |
|
63
|
|
|
{ |
|
64
|
|
|
return Str::systemize($title); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|