block   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 58
dl 0
loc 176
ccs 33
cts 33
cp 1
rs 10
c 2
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A set_class() 0 4 2
A set_title() 0 4 1
A set_bid() 0 7 2
A set_permission() 0 17 3
A set_settings() 0 7 2
A get_permission() 0 6 1
A __clone() 0 3 1
A get_settings() 0 3 1
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\model\entity;
12
13
use blitze\sitemaker\model\base_entity;
14
15
/**
16
 * @method int get_bid()
17
 * @method object set_icon($icon)
18
 * @method string get_icon()
19
 * @method object set_name($name)
20
 * @method string get_name()
21
 * @method string get_title()
22
 * @method object set_route_id(integer $route_id)
23
 * @method integer get_route_id()
24
 * @method object set_position($position)
25
 * @method string get_position()
26
 * @method object set_weight(integer $weight)
27
 * @method integer get_weight()
28
 * @method object set_style(integer $style)
29
 * @method integer get_style()
30
 * @method object set_status(boolean $status)
31
 * @method boolean get_status()
32
 * @method object set_type(integer $type)
33
 * @method integer get_type()
34
 * @method object set_hide_title(boolean $hide_title)
35
 * @method boolean get_hide_title()
36
 * @method object set_hash($hash)
37
 * @method string get_hash()
38
 * @method object set_view($view)
39
 * @method boolean get_view()
40
 */
41
final class block extends base_entity
42
{
43
	/** @var integer */
44
	protected $bid;
45
46
	/** @var string */
47
	protected $icon = '';
48
49
	/** @var string */
50
	protected $name = '';
51
52
	/** @var string */
53
	protected $title = '';
54
55
	/** @var integer */
56
	protected $route_id = 0;
57
58
	/** @var string */
59
	protected $position = '';
60
61
	/** @var integer */
62
	protected $weight = 0;
63
64
	/** @var integer */
65
	protected $style = 0;
66
67
	/** @var string */
68
	protected $permission = '';
69
70
	/** @var string */
71
	protected $class = '';
72
73
	/** @var boolean */
74
	protected $status = true;
75
76
	/** @var integer */
77
	protected $type = 0;
78
79
	/** @var boolean */
80
	protected $hide_title = false;
81
82
	/** @var string */
83
	protected $hash = '';
84
85
	/** @var string */
86
	protected $settings = '';
87
88
	/** @var string */
89
	protected $view = '';
90
91
	/** @var array */
92
	protected $required_fields = array('name', 'route_id', 'position', 'style');
93
94
	/** @var array */
95
	protected $db_fields = array(
96
		'icon',
97
		'name',
98
		'title',
99
		'route_id',
100
		'position',
101
		'weight',
102
		'style',
103
		'permission',
104
		'class',
105
		'status',
106
		'type',
107
		'hide_title',
108
		'hash',
109
		'settings',
110
		'view',
111
	);
112
113
	/**
114
	 * Set block ID
115
	 * @param int $bid
116
	 * @return $this
117 60
	 */
118
	public function set_bid($bid)
119 60
	{
120 60
		if (!$this->bid)
121 60
		{
122 60
			$this->bid = (int) $bid;
123 60
		}
124
		return $this;
125
	}
126
127
	/**
128
	 * Set title
129
	 * @param string $title
130
	 * @return $this
131 55
	 */
132
	public function set_title($title)
133 55
	{
134 55
		$this->title = utf8_ucfirst(trim($title));
135
		return $this;
136
	}
137
138
	/**
139
	 * Set css class
140
	 * @param string $class
141
	 * @return $this
142 54
	 */
143
	public function set_class($class)
144 54
	{
145 54
		$this->class = ($class) ? ' ' . trim($class) : '';
146
		return $this;
147
	}
148
149
	/**
150
	 * Set permissions
151
	 * Going from ['type' => 1, 'groups' => [1,2,3]] to 1,2,3:1
152
	 * @param array|string $permission
153 55
	 * @return $this
154
	 */
155 55
	public function set_permission($permission)
156 55
	{
157
		if (is_array($permission))
158
		{
159
			if (sizeof($permission))
160
			{
161
				$permission = array_merge(['groups' => [], 'type' => 1], $permission);
162
				$permission['groups'] = join(',', array_filter($permission['groups']));
163 37
			}
164
165 37
			$this->permission = join(':', array_filter($permission));
166
		}
167
		else
168
		{
169
			$this->permission = $permission;
170
		}
171
		return $this;
172
	}
173 57
174
	/**
175 57
	 * Get permissions
176 57
	 * Going from 1,2,3:0 to ['type' => 0, 'groups' => [1,2,3]] or 1,2,3 to ['type' => 1, 'groups' => [1,2,3]]
177 53
	 * @return array
178 53
	 */
179 40
	public function get_permission()
180 40
	{
181 36
		[$groups, $type] = array_pad(explode(':', $this->permission), 2, 0);
182 36
		return [
183 36
			'type'		=> (int) $type,
184 57
			'groups'	=> array_map('intval', array_filter(explode(',', $groups))),
185
		];
186
	}
187
188
	/**
189
	 * Set settings
190
	 * @param array|string $settings
191 42
	 * @return $this
192
	 */
193 42
	public function set_settings($settings)
194
	{
195
		if ($this->set_array_field('settings', $settings))
196
		{
197
			$this->hash = md5($this->settings);
198
		}
199 6
		return $this;
200
	}
201 6
202 6
	/**
203
	 * Get settings
204
	 * @return array
205
	 */
206
	public function get_settings()
207
	{
208
		return $this->get_array_field('settings');
209
	}
210
211
	/**
212
	 *
213
	 */
214
	public function __clone()
215
	{
216
		$this->bid = null;
217
	}
218
}
219