Completed
Push — develop ( de7659...415144 )
by Daniel
09:44
created

base_entity::_validate_attribute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 8
nc 3
nop 2
crap 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A base_entity::check_required() 0 10 3
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2015 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;
11
12
abstract class base_entity implements entity_interface
13
{
14
	/** @var array */
15
	protected $db_fields = array();
16
17
	/** @var array */
18
	protected $required_fields = array();
19
20
	/**
21
	 * Populate the entity with data
22
	 * @param array $data
23
	 */
24 160
	public function __construct(array $data)
25
	{
26 160
		foreach ($data as $name => $value)
27
		{
28 119
			$mutator = 'set_' . $name;
29 119
			$this->$mutator($value);
30 160
		}
31 160
	}
32
33
	/**
34
	 * @param $name
35
	 * @param $args
36
	 * @return $this
37
	 * @throws \blitze\sitemaker\exception\unexpected_value
38
	 */
39 147
	public function __call($name, $args)
40
	{
41 147
		if (preg_match('/^(get|set)_(\w+)/', strtolower($name), $match) && $attribute = $this->attribute_exists($match[2]))
42 147
		{
43 145
			if ('get' == $match[1])
44 145
			{
45 135
				return $this->$attribute;
46
			}
47
			else
48
			{
49 122
				$this->$attribute = $this->validate_attribute($match[2], $args[0]);
50 120
				return $this;
51
			}
52
		}
53
		else
54
		{
55 4
			throw new \blitze\sitemaker\exception\unexpected_value(array($name, 'UNDEFINED_METHOD'));
56
		}
57
	}
58
59
	/**
60
	* {@inheritdoc}
61
	*/
62 59
	public function to_array()
63
	{
64 59
		$attributes = $this->get_attributes();
65
66 59
		$data = array();
67 59
		foreach ($attributes as $attribute)
68
		{
69 59
			$accessor = 'get_' . $attribute;
70 59
			$data[$attribute] = $this->$accessor();
71 59
		}
72 59
		unset($data['db_fields'], $data['required_fields']);
73
74 59
		return $data;
75
	}
76
77
	/**
78
	* {@inheritdoc}
79
	*/
80 47
	public function to_db()
81
	{
82 47
		$this->check_required();
83
84 42
		$db_data = array();
85 42
		foreach ($this->db_fields as $attribute)
86
		{
87 42
			$type = $this->get_property_type($attribute);
88 42
			if (in_array($type, array('boolean', 'integer', 'string')))
89 42
			{
90 42
				$db_data[$attribute] = $this->$attribute;
91 42
			}
92 42
		}
93
94 42
		return $db_data;
95
	}
96
97
	/**
98
	 * @return array
99
	 */
100 147
	protected function get_attributes()
101
	{
102 147
		return array_keys(get_class_vars(get_class($this)));
103
	}
104
105
	/**
106
	 * @param string $name
107
	 * @return string|null
108
	 */
109 147
	protected function attribute_exists($name)
110
	{
111 147
		if (in_array(strtolower($name), $this->get_attributes()))
112 147
		{
113 145
			return strtolower($name);
114
		}
115 4
		return null;
116
	}
117
118
	/**
119
	 * @throws \blitze\sitemaker\exception\invalid_argument
120
	 */
121 47
	protected function check_required()
122
	{
123 47
		foreach ($this->required_fields as $field)
124
		{
125 47
			if (!$this->$field)
126 47
			{
127 5
				throw new \blitze\sitemaker\exception\invalid_argument(array($field, 'FIELD_MISSING'));
128
			}
129 44
		}
130 42
	}
131
132
	/**
133
	 * @param string $name
134
	 * @param mixed $value
135
	 * @return mixed
136
	 * @throws \blitze\sitemaker\exception\unexpected_value
137
	 */
138 122
	protected function validate_attribute($name, $value)
139
	{
140 122
		$type = $this->get_property_type($name);
141
142 122
		if (in_array($type, array('array', 'boolean', 'float', 'integer', 'string')))
143 122
		{
144 117
			settype($value, $type);
145 117
			return $value;
146
		}
147
148 36
		if ($type === false || !$value instanceof $type)
149 36
		{
150 2
			throw new \blitze\sitemaker\exception\unexpected_value(array($name, 'INVALID_TYPE'));
151
		}
152
153 34
		return $value;
154
	}
155
156
	/**
157
	 * @param string $name
158
	 * @return mixed
159
	 */
160 125
	protected function get_property_type($name)
161
	{
162 125
		$reflection = new \ReflectionObject($this);
163 125
		$reflectionProperty = $reflection->getProperty($name);
164 125
		$doc = $reflectionProperty->getDocComment();
165
166 125
		preg_match_all('#\/\*\*\s@var\s(.*?)\s\*\/#s', $doc, $annotations);
167
168 125
		return current($annotations[1]);
169
	}
170
}
171