Completed
Push — develop ( 8387b5...1b51bd )
by Daniel
11:34
created

item   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 1
dl 0
loc 157
c 0
b 0
f 0
ccs 40
cts 40
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A set_item_id() 0 8 2
A set_item_title() 0 5 1
A set_item_icon() 0 5 2
A set_item_url() 0 14 3
A get_full_url() 0 15 4
A is_local() 0 5 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\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', 'item_title');
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 71
	public function __construct(array $data, $mod_rewrite_enabled = false)
102
	{
103 71
		$this->board_url = generate_board_url();
104 71
		$this->mod_rewrite_enabled = $mod_rewrite_enabled;
105
106 71
		parent::__construct($data);
107 71
	}
108
109
	/**
110
	 * Set block ID
111
	 * @param int $item_id
112
	 * @return $this
113
	 */
114 47
	public function set_item_id($item_id)
115
	{
116 47
		if (!$this->item_id)
117 47
		{
118 47
			$this->item_id = (int) $item_id;
119 47
		}
120 47
		return $this;
121
	}
122
123
	/**
124
	 * @param string $item_title
125
	 * @return $this
126
	 */
127 47
	public function set_item_title($item_title)
128
	{
129 47
		$this->item_title = utf8_ucfirst(trim($item_title));
130 47
		return $this;
131
	}
132
133
	/**
134
	 * @param string $icon
135
	 * @return $this
136
	 */
137 45
	public function set_item_icon($icon)
138
	{
139 45
		$this->item_icon = ($icon) ? trim($icon) . ' ' : '';
140 45
		return $this;
141
	}
142
143
	/**
144
	 * @param string $item_url
145
	 * @return $this
146
	 */
147 58
	public function set_item_url($item_url)
148
	{
149 58
		$search = array('&amp;', $this->board_url);
150 58
		$replace = array('&', '');
151 58
		$this->item_url = str_replace($search, $replace, $item_url);
152
153
		// add leading / for local paths, except leading hashtags
154 58
		if ($this->is_local($this->item_url) && $this->item_url[0] !== '#')
155 58
		{
156 30
			$this->item_url = '/' . ltrim($this->item_url, './');
157 30
		}
158
159 58
		return $this;
160
	}
161
162
	/**
163
	 * @return string
164
	 */
165 41
	public function get_full_url()
166
	{
167 41
		$item_url = $this->item_url;
168
169 41
		if ($this->is_local($item_url) && $item_url[0] === '/')
170 41
		{
171 29
			$item_url = $this->board_url . $item_url;
172 29
			if ($this->mod_rewrite_enabled)
173 29
			{
174 4
				$item_url = str_replace('app.php/', '', $item_url);
175 4
			}
176 29
		}
177
178 41
		return $item_url;
179
	}
180
181
	/**
182
	 * @param string $item_url
183
	 * @return true|false
184
	 */
185 58
	private function is_local($item_url)
186
	{
187 58
		$host = parse_url($item_url, PHP_URL_HOST);
188 58
		return (!$item_url || !empty($host) || substr($item_url, 0, 2) === '//') ? false: true;
189
	}
190
}
191