1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains functionality linking database objects' aliases with Symfony2's URL routing component |
4
|
|
|
* |
5
|
|
|
* @package BZiON\Models |
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A Model that has a URL |
11
|
|
|
* @package BZiON\Models |
12
|
|
|
*/ |
13
|
|
|
abstract class UrlModel extends PermissionModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Get the name of the route that shows the object |
17
|
|
|
* @param string $action The route's suffix |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public static function getRouteName($action = 'show') |
21
|
|
|
{ |
22
|
|
|
return self::getParamName() . "_$action"; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get the name of the object's parameter in the route |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public static function getParamName() |
30
|
|
|
{ |
31
|
|
|
return static::getType(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Gets a human-readable format of the model's type |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public static function getTypeForHumans() |
39
|
|
|
{ |
40
|
|
|
return static::getParamName(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get an object's url |
45
|
|
|
* |
46
|
|
|
* @param string $action The action to perform (e.g `show`, `list` or `delete`) |
47
|
|
|
* @param bool $absolute Whether to return an absolute URL |
48
|
|
|
* @param array $params Extra parameters to pass to the URL generator |
49
|
|
|
* |
50
|
|
|
* @return string A link |
51
|
|
|
*/ |
52
|
|
|
public function getURL($action = 'show', $absolute = false, $params = array()) |
53
|
|
|
{ |
54
|
|
|
return static::getPermaLink($action, $absolute, $params); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get an object's permanent url |
59
|
|
|
* |
60
|
|
|
* @param string $action The action to perform (e.g `show`, `list` or `delete`) |
61
|
|
|
* @param bool $absolute Whether to return an absolute URL |
62
|
|
|
* @param array $params Extra parameters to pass to the URL generator |
63
|
|
|
* |
64
|
|
|
* @return string A permanent link |
65
|
|
|
*/ |
66
|
|
|
public function getPermaLink($action = 'show', $absolute = false, $params = array()) |
67
|
|
|
{ |
68
|
|
|
return $this->getLink($this->getId(), $action, $absolute, $params); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Generate a link for a route related to this object |
73
|
|
|
* |
74
|
|
|
* @param mixed $identifier A parameter representing the model (e.g an ID or alias) |
75
|
|
|
* @param string $action The action to perform |
76
|
|
|
* @param bool $absolute Whether to return an absolute URL |
77
|
|
|
* @param array $params Extra parameters to pass to the URL generator |
78
|
|
|
* |
79
|
|
|
* @return string A link |
80
|
|
|
*/ |
81
|
|
|
protected function getLink($identifier, $action, $absolute, $params) |
82
|
|
|
{ |
83
|
|
|
return Service::getGenerator()->generate( |
84
|
|
|
static::getRouteName($action), |
85
|
|
|
array_merge(array(static::getParamName() => $identifier), $params), |
86
|
|
|
$absolute |
|
|
|
|
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: