Completed
Push — master ( 9c73ab...3bd5c7 )
by Timothy
02:45
created

src/Aviat/AnimeClient/RoutingBase.php (3 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Hummingbird Anime Client
4
 *
5
 * An API client for Hummingbird to manage anime and manga watch lists
6
 *
7
 * @package     HummingbirdAnimeClient
8
 * @author      Timothy J. Warren
9
 * @copyright   Copyright (c) 2015 - 2016
10
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
11
 * @license     MIT
12
 */
13
namespace Aviat\AnimeClient;
14
15
use Aviat\Ion\Di\ContainerInterface;
16
17
/**
18
 * Base for routing/url classes
19
 */
20
class RoutingBase {
21
22
	use \Aviat\Ion\StringWrapper;
23
24
	/**
25
	 * Injection Container
26
	 * @var ContainerInterface $container
27
	 */
28
	protected $container;
29
30
	/**
31
	 * Config Object
32
	 * @var Config
33
	 */
34
	protected $config;
35
36
	/**
37
	 * Routing array
38
	 * @var array
39
	 */
40
	protected $routes;
41
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param ContainerInterface $container
46
	 */
47
	public function __construct(ContainerInterface $container)
48
	{
49
		$this->container = $container;
50
		$this->config = $container->get('config');
51
		$this->base_routes = $this->config->get('routes');
0 ignored issues
show
The property base_routes does not seem to exist. Did you mean routes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52
		$this->routes = $this->base_routes['routes'];
0 ignored issues
show
The property base_routes does not seem to exist. Did you mean routes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53
	}
54
55
	/**
56
	 * Retreive the appropriate value for the routing key
57
	 *
58
	 * @param string $key
59
	 * @return mixed
60
	 */
61
	public function __get($key)
62
	{
63
		$routing_config = $this->base_routes['route_config'];
0 ignored issues
show
The property base_routes does not seem to exist. Did you mean routes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
64
65
		if (array_key_exists($key, $routing_config))
66
		{
67
			return $routing_config[$key];
68
		}
69
	}
70
71
	/**
72
	 * Get the current url path
73
	 *
74
	 * @return string
75
	 */
76
	public function path()
77
	{
78
		$request = $this->container->get('request');
79
		$path = $request->url->get(PHP_URL_PATH);
80
		$cleaned_path = $this->string($path)
81
			->trim()
82
			->trimRight('/')
83
			->ensureLeft('/');
84
85
		return (string)$cleaned_path;
86
	}
87
88
	/**
89
	 * Get the url segments
90
	 *
91
	 * @return array
92
	 */
93
	public function segments()
94
	{
95
		$path = $this->path();
96
		return explode('/', $path);
97
	}
98
99
	/**
100
	 * Get a segment of the current url
101
	 *
102
	 * @param int $num
103
	 * @return string|null
104
	 */
105
	public function get_segment($num)
106
	{
107
		$segments = $this->segments();
108
		return (array_key_exists($num, $segments)) ? $segments[$num] : NULL;
109
	}
110
111
	/**
112
	 * Retrieve the last url segment
113
	 *
114
	 * @return string
115
	 */
116
	public function last_segment()
117
	{
118
		$segments = $this->segments();
119
		return end($segments);
120
	}
121
}
122
// End of RoutingBase.php