Completed
Push — master ( 9a0cd7...f57167 )
by Daniel
10:27
created

item::is_extension_route()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 6
nop 1
crap 4
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\model\menus\entity;
11
12
use blitze\sitemaker\model\base_entity;
13
14
/**
15
 * @method integer get_item_id()
16
 * @method object set_menu_id($menu_id)
17
 * @method integer get_menu_id()
18
 * @method object set_parent_id($parent_id)
19
 * @method integer get_parent_id()
20
 * @method string get_item_title()
21
 * @method string get_item_url()
22
 * @method string get_item_icon()
23
 * @method object set_item_target($item_target)
24
 * @method integer get_item_target()
25
 * @method object set_left_id($left_id)
26
 * @method integer get_left_id()
27
 * @method object set_right_id($right_id)
28
 * @method integer get_right_id()
29
 * @method object item_parents($item_parents)
30
 * @method integer get_item_parents()
31
 * @method object set_depth($depth)
32
 * @method integer get_depth()
33
 */
34
final class item extends base_entity
35
{
36
	/** @var integer */
37
	protected $item_id;
38
39
	/** @var integer */
40
	protected $menu_id = 0;
41
42
	/** @var integer */
43
	protected $parent_id = 0;
44
45
	/** @var string */
46
	protected $item_title = '';
47
48
	/** @var string */
49
	protected $item_url = '';
50
51
	/** @var string */
52
	protected $item_icon = '';
53
54
	/** @var integer */
55
	protected $item_target = 0;
56
57
	/** @var integer */
58
	protected $left_id = 0;
59
60
	/** @var integer */
61
	protected $right_id = 0;
62
63
	/** @var string */
64
	protected $item_parents = '';
65
66
	/** @var integer */
67
	protected $depth = 0;
68
69
	/** @var string */
70
	protected $full_url = '';
71
72
	/** @var string */
73
	protected $board_url;
74
75
	/** @var boolean */
76
	protected $mod_rewrite_enabled;
77
78
	/** @var array */
79
	protected $required_fields = array('menu_id');
80
81
	/** @var array */
82
	protected $db_fields = array(
83
		'item_id',
84
		'menu_id',
85
		'parent_id',
86
		'item_title',
87
		'item_url',
88
		'item_icon',
89
		'item_target',
90
		'left_id',
91
		'right_id',
92
		'item_parents',
93
		'depth',
94
	);
95
96
	/**
97
	 * Class constructor
98
	 * @param array $data
99
	 * @param bool  $mod_rewrite_enabled
100
	 */
101 62
	public function __construct(array $data, $mod_rewrite_enabled = false)
102
	{
103 62
		$this->board_url = generate_board_url();
104 62
		$this->mod_rewrite_enabled = $mod_rewrite_enabled;
105
106 62
		parent::__construct($data);
107 62
	}
108
109
	/**
110
	 * Set block ID
111
	 * @param int $item_id
112
	 * @return $this
113
	 */
114 44
	public function set_item_id($item_id)
115
	{
116 44
		if (!$this->item_id)
117 44
		{
118 44
			$this->item_id = (int) $item_id;
119 44
		}
120 44
		return $this;
121
	}
122
123
	/**
124
	 * @param string $item_title
125
	 * @return $this
126
	 */
127 44
	public function set_item_title($item_title)
128
	{
129 44
		$this->item_title = utf8_ucfirst(trim($item_title));
130 44
		return $this;
131
	}
132
133
	/**
134
	 * @param string $icon
135
	 * @return $this
136
	 */
137 42
	public function set_item_icon($icon)
138
	{
139 42
		$this->item_icon = ($icon) ? trim($icon) . ' ' : '';
140 42
		return $this;
141
	}
142
143
	/**
144
	 * @param string $item_url
145
	 * @return $this
146
	 */
147 49
	public function set_item_url($item_url)
148
	{
149 49
		$search = array('&amp;', $this->board_url);
150 49
		$replace = array('&', '');
151 49
		$this->item_url = ltrim(str_replace($search, $replace, $item_url), './');
152
153
		// to make things uniform and easy to switch between mod_rewrite_enabled or not without
154
		// having to edit menu items, we add app.php/ for all extension routes
155 49
		if (strpos($this->item_url, 'app.php') === false && $this->is_extension_route($this->item_url))
156 49
		{
157 31
			$this->item_url = 'app.php/' . $this->item_url;
158 31
		}
159
160 49
		return $this;
161
	}
162
163
	/**
164
	 * @return string
165
	 */
166 32
	public function get_full_url()
167
	{
168 32
		$item_url = $this->item_url;
169 32
		$host = parse_url($item_url, PHP_URL_HOST);
170
171 32
		if ($item_url && empty($host))
172 32
		{
173 30
			$item_url = $this->add_board_url($item_url);
174
175 30
			if ($this->mod_rewrite_enabled === true)
176 30
			{
177 2
				$item_url = str_replace('app.php/', '', $item_url);
178 2
			}
179 30
		}
180
181 32
		return $item_url;
182
	}
183
184
	/**
185
	 * Checks if a url is an extension route
186
	 *
187
	 * @param string $item_url
188
	 * @return true|false
189
	 */
190 49
	private function is_extension_route($item_url)
191
	{
192 49
		$host = parse_url($item_url, PHP_URL_HOST);
193 49
		$extension = pathinfo($item_url, PATHINFO_EXTENSION);
194
195 49
		return ($host || $extension || is_dir($item_url)) ? false : true;
0 ignored issues
show
Bug Best Practice introduced by
The expression $host of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
196
	}
197
198
	/**
199
	 * @param string $item_url
200
	 * @return string
201
	 */
202 30
	private function add_board_url($item_url)
203
	{
204 30
		return $this->board_url . ($item_url[0] === '/' ? '' : '/') . $item_url;
205
	}
206
}
207