1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Twig; |
9
|
|
|
|
10
|
|
|
use Twig_Environment; |
11
|
|
|
|
12
|
|
|
class BaseUrlFunction implements StakxTwigFilter |
13
|
|
|
{ |
14
|
|
|
private $site; |
15
|
|
|
|
16
|
18 |
|
public function __invoke(Twig_Environment $env, $assetPath, $absolute = false) |
17
|
|
|
{ |
18
|
18 |
|
$globals = $env->getGlobals(); |
19
|
18 |
|
$this->site = $globals['site']; |
20
|
|
|
|
21
|
18 |
|
$url = $this->getUrl($absolute); |
22
|
18 |
|
$baseURL = $this->getBaseUrl(); |
23
|
18 |
|
$permalink = $this->guessAssetPath($assetPath); |
24
|
|
|
|
25
|
18 |
|
return $this->buildPermalink($url, $baseURL, $permalink); |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
public static function get() |
29
|
|
|
{ |
30
|
1 |
|
return new \Twig_SimpleFunction('url', new self(), array( |
|
|
|
|
31
|
1 |
|
'needs_environment' => true, |
32
|
1 |
|
)); |
33
|
|
|
} |
34
|
|
|
|
35
|
18 |
|
private function getUrl($absolute) |
36
|
|
|
{ |
37
|
18 |
|
$url = '/'; |
38
|
|
|
|
39
|
18 |
|
if (!$absolute) |
40
|
18 |
|
{ |
41
|
16 |
|
return $url; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
if (isset($this->site['url'])) |
45
|
2 |
|
{ |
46
|
2 |
|
$url = $this->site['url']; |
47
|
2 |
|
} |
48
|
|
|
|
49
|
2 |
|
return ltrim($url, '/'); |
50
|
|
|
} |
51
|
|
|
|
52
|
18 |
|
private function getBaseUrl() |
|
|
|
|
53
|
|
|
{ |
54
|
18 |
|
$base = ''; |
55
|
|
|
|
56
|
18 |
|
if (isset($this->site['baseurl'])) |
57
|
18 |
|
{ |
58
|
15 |
|
$base = $this->site['baseurl']; |
59
|
15 |
|
} |
60
|
3 |
|
elseif (isset($this->site['base'])) |
61
|
|
|
{ |
62
|
|
|
$base = $this->site['base']; |
63
|
|
|
} |
64
|
|
|
|
65
|
18 |
|
return $base; |
66
|
|
|
} |
67
|
|
|
|
68
|
18 |
|
private function guessAssetPath($assetPath) |
|
|
|
|
69
|
|
|
{ |
70
|
18 |
|
if (is_array($assetPath) || ($assetPath instanceof \ArrayAccess)) |
71
|
18 |
|
{ |
72
|
4 |
|
return (isset($assetPath['permalink'])) ? $assetPath['permalink'] : '/'; |
73
|
|
|
} |
74
|
14 |
|
elseif (is_null($assetPath)) |
75
|
|
|
{ |
76
|
1 |
|
return '/'; |
77
|
|
|
} |
78
|
|
|
|
79
|
13 |
|
return $assetPath; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @link https://stackoverflow.com/a/15575293 |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
18 |
View Code Duplication |
private function buildPermalink() |
|
|
|
|
87
|
|
|
{ |
88
|
18 |
|
$paths = array(); |
89
|
|
|
|
90
|
18 |
|
foreach (func_get_args() as $arg) |
91
|
|
|
{ |
92
|
18 |
|
if ($arg !== '') |
93
|
18 |
|
{ |
94
|
18 |
|
$paths[] = $arg; |
95
|
18 |
|
} |
96
|
18 |
|
} |
97
|
|
|
|
98
|
18 |
|
return preg_replace('#(?<!:)/+#','/', join('/', $paths)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.