1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Hummingbird Anime Client |
4
|
|
|
* |
5
|
|
|
* An API client for Hummingbird to manage anime and manga watch lists |
6
|
|
|
* |
7
|
|
|
* PHP version 7 |
8
|
|
|
* |
9
|
|
|
* @package HummingbirdAnimeClient |
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
11
|
|
|
* @copyright 2015 - 2016 Timothy J. Warren |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
13
|
|
|
* @version 3.1 |
14
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Aviat\AnimeClient; |
18
|
|
|
|
19
|
|
|
use Aviat\Ion\Di\ContainerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Base for routing/url classes |
23
|
|
|
*/ |
24
|
|
|
class RoutingBase { |
25
|
|
|
|
26
|
|
|
use \Aviat\Ion\StringWrapper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Injection Container |
30
|
|
|
* @var ContainerInterface $container |
31
|
|
|
*/ |
32
|
|
|
protected $container; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Config Object |
36
|
|
|
* @var Config |
37
|
|
|
*/ |
38
|
|
|
protected $config; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Routing array |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $routes; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Route configuration options |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $route_config; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor |
54
|
|
|
* |
55
|
|
|
* @param ContainerInterface $container |
56
|
|
|
*/ |
57
|
|
|
public function __construct(ContainerInterface $container) |
58
|
|
|
{ |
59
|
|
|
$this->container = $container; |
60
|
|
|
$this->config = $container->get('config'); |
61
|
|
|
$base_routes = $this->config->get('routes'); |
62
|
|
|
$this->routes = $base_routes['routes']; |
63
|
|
|
$this->route_config = $base_routes['route_config']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Retreive the appropriate value for the routing key |
68
|
|
|
* |
69
|
|
|
* @param string $key |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function __get($key) |
73
|
|
|
{ |
74
|
|
|
$routing_config =& $this->route_config; |
75
|
|
|
|
76
|
|
|
if (array_key_exists($key, $routing_config)) |
77
|
|
|
{ |
78
|
|
|
return $routing_config[$key]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the current url path |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function path() |
88
|
|
|
{ |
89
|
|
|
$request = $this->container->get('request'); |
90
|
|
|
$path = $request->getUri()->getPath(); |
91
|
|
|
$cleaned_path = $this->string($path) |
92
|
|
|
->replace('%20', '') |
93
|
|
|
->trim() |
94
|
|
|
->trimRight('/') |
95
|
|
|
->ensureLeft('/'); |
96
|
|
|
|
97
|
|
|
return (string)$cleaned_path; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the url segments |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function segments() |
106
|
|
|
{ |
107
|
|
|
$path = $this->path(); |
108
|
|
|
return explode('/', $path); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get a segment of the current url |
113
|
|
|
* |
114
|
|
|
* @param int $num |
115
|
|
|
* @return string|null |
116
|
|
|
*/ |
117
|
|
|
public function get_segment($num) |
118
|
|
|
{ |
119
|
|
|
$segments = $this->segments(); |
120
|
|
|
return (array_key_exists($num, $segments)) ? $segments[$num] : NULL; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Retrieve the last url segment |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function last_segment() |
129
|
|
|
{ |
130
|
|
|
$segments = $this->segments(); |
131
|
|
|
return end($segments); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
// End of RoutingBase.php |