Completed
Push — development ( 1c3d48...61edbf )
by Alexander
03:31
created

ActiveRecord   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 20
c 7
b 2
f 0
lcom 1
cbo 4
dl 0
loc 184
ccs 56
cts 56
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getMap() 0 4 1
A save() 0 4 2
A delete() 0 6 1
A setBuilder() 0 4 1
A query() 0 18 4
A update() 0 6 1
A getById() 0 6 1
A getAll() 0 20 4
A find() 0 6 1
A insert() 0 8 1
A getResult() 0 12 2
1
<?php
2
/**
3
 * ActiveRecord model
4
 *
5
 * @file      ActiveRecord.php
6
 *
7
 * PHP version 5.6+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2016 Alexander Yancharuk
11
 * @date      Втр Апр 24 21:53:04 2012
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Model;
17
18
use StdClass;
19
use Veles\DataBase\Db;
20
use Veles\DataBase\DbFilter;
21
use Veles\DataBase\DbPaginator;
22
use Veles\Traits\DynamicPropHandler;
23
24
/**
25
 * Model class using ActiveRecord pattern
26
 *
27
 * @author Alexander Yancharuk <alex at itvault dot info>
28
 */
29
class ActiveRecord extends StdClass
30
{
31
	/**
32
	 * @const string|null Table name
33
	 */
34
	const TBL_NAME = null;
35
	/** @var QueryBuilder */
36
	protected $builder;
37
	/** @var array Data type map */
38
	protected $map = [];
39
40
	use DynamicPropHandler;
41
42
	/**
43
	 * Update data in database
44
	 *
45
	 * @return bool
46
	 */
47 1
	protected function update()
48
	{
49 1
		$sql = $this->builder->update($this);
50
51 1
		return Db::query($sql);
52
	}
53
54
	/**
55
	 * Insert data in database
56
	 *
57
	 * @return bool
58
	 */
59 1
	protected function insert()
60
	{
61 1
		$sql      = $this->builder->insert($this);
62 1
		$result   = Db::query($sql);
63 1
		$this->id = Db::getLastInsertId();
64
65 1
		return $result;
66
	}
67
68 4
	protected function getResult($sql)
69
	{
70 4
		$result = Db::row($sql);
71
72 4
		if (empty($result)) {
73 2
			return false;
74
		}
75
76 2
		$this->setProperties($result);
77
78 2
		return true;
79
	}
80
81 1
	public function __construct()
82
	{
83 1
		$this->builder = new QueryBuilder;
84 1
	}
85
86
	/**
87
	 * Get data type map
88
	 *
89
	 * @return array
90
	 */
91 1
	public function getMap()
92
	{
93 1
		return $this->map;
94
	}
95
96
	/**
97
	 * Get data by ID
98
	 *
99
	 * @param int $identifier Model ID
100
	 *
101
	 * @return bool
102
	 */
103 4
	public function getById($identifier)
104
	{
105 4
		$sql = $this->builder->getById($this, $identifier);
106
107 4
		return $this->getResult($sql);
108
	}
109
110
	/**
111
	 * Get object list by filter
112
	 *
113
	 * @param bool|DbFilter    $filter Filter object
114
	 * @param bool|DbPaginator $pager  Pagination object
115
	 *
116
	 * @return bool|array
117
	 */
118 1
	public function getAll($filter = false, $pager = false)
119
	{
120 1
		$sql = $this->builder->find($this, $filter);
121
122 1
		if ($pager instanceof DbPaginator) {
123 1
			$sql = $this->builder->setPage($sql, $pager);
124 1
		}
125
126 1
		$result = Db::rows($sql);
127
128 1
		if (empty($result)) {
129 1
			return false;
130
		}
131
132 1
		if ($pager instanceof DbPaginator) {
133 1
			$pager->calcMaxPages();
134 1
		}
135
136 1
		return $result;
137
	}
138
139
	/**
140
	 * Save data
141
	 *
142
	 * @return bool|int
143
	 */
144 1
	public function save()
145
	{
146 1
		return isset($this->id) ? $this->update() : $this->insert();
147
	}
148
149
	/**
150
	 * Delete data
151
	 *
152
	 * @param array|bool $ids Array of ID for delete
153
	 *
154
	 * @return bool
155
	 */
156 1
	public function delete($ids = false)
157
	{
158 1
		$sql = $this->builder->delete($this, $ids);
159
160 1
		return Db::query($sql);
161
	}
162
163
	/**
164
	 * Get unique object
165
	 *
166
	 * @param bool|DbFilter $filter Filter object
167
	 *
168
	 * @return bool
169
	 */
170 2
	public function find($filter = false)
171
	{
172 2
		$sql = $this->builder->find($this, $filter);
173
174 2
		return $this->getResult($sql);
175
	}
176
177
	/**
178
	 * @param QueryBuilder $builder
179
	 */
180 1
	public function setBuilder(QueryBuilder $builder)
181
	{
182 1
		$this->builder = $builder;
183 1
	}
184
185
	/**
186
	 * Query with pagination
187
	 *
188
	 * @param string           $sql   Query
189
	 * @param bool|DbPaginator $pager Pagination object
190
	 *
191
	 * @todo Add Pager type hints
192
	 * @return array|bool
193
	 */
194 4
	public function query($sql, $pager = false)
195
	{
196 4
		if ($pager instanceof DbPaginator) {
197 2
			$sql = $this->builder->setPage($sql, $pager);
198 2
		}
199
200 4
		$result = Db::rows($sql);
201
202 4
		if (empty($result)) {
203 1
			return false;
204
		}
205
206 3
		if ($pager instanceof DbPaginator) {
207 2
			$pager->calcMaxPages();
208 2
		}
209
210 3
		return $result;
211
	}
212
}
213