Completed
Push — master ( e252bb...9a0cd7 )
by Daniel
09:22
created

item::set_item_title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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
		$this->item_url = str_replace($this->board_url . '/', '', $item_url);
150
151
		// to make things uniform and easy to switch between mod_rewrite_enabled or not without
152
		// having to edit menu items, we add app.php/ for all extension routes
153 49
		if (strpos($item_url, 'app.php') === false && $this->is_extension_route($item_url))
154 49
		{
155 31
			$this->item_url = ltrim($this->item_url, './');
156 31
			$this->item_url = '/app.php/' . $this->item_url;
157 31
		}
158
159 49
		return $this;
160
	}
161
162
	/**
163
	 * @return string
164
	 */
165 32
	public function get_full_url()
166
	{
167 32
		$item_url = $this->item_url;
168 32
		$host = parse_url($item_url, PHP_URL_HOST);
169
170 32
		if ($item_url && empty($host))
171 32
		{
172 30
			$item_url = $this->add_board_url($item_url);
173
174 30
			if ($this->mod_rewrite_enabled === true)
175 30
			{
176 2
				$item_url = str_replace('app.php/', '', $item_url);
177 2
			}
178 30
		}
179
180 32
		return $item_url;
181
	}
182
183
	/**
184
	 * Checks if a url is an extension route
185
	 *
186
	 * @param string $item_url
187
	 * @return true|false
188
	 */
189 49
	private function is_extension_route($item_url)
190
	{
191 49
		$host = parse_url($item_url, PHP_URL_HOST);
192 49
		$extension = pathinfo($item_url, PATHINFO_EXTENSION);
193 49
		$is_absolute_path = phpbb_is_absolute($item_url);
194
195 49
		return ($host || $extension || is_dir($item_url) || $is_absolute_path) ? 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 30
	private function add_board_url($item_url)
199
	{
200 30
		return $this->board_url . ($item_url[0] === '/' ? '' : '/') . $item_url;
201
	}
202
}
203