Skip to content

Commit 639c65f

Browse files
committed
small refactoring
1 parent 76cdc42 commit 639c65f

16 files changed

+151
-162
lines changed

src/condition/Condition.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace WebComplete\core\condition;
44

5-
65
class Condition
76
{
8-
97
const EQUALS = 1;
108
const NOT_EQUALS = 2;
119
const LESS_THAN = 3;
@@ -18,28 +16,33 @@ class Condition
1816

1917
protected $conditions = [];
2018
protected $sort = [];
21-
protected $offset = null;
22-
protected $limit = null;
19+
protected $offsetValue;
20+
protected $limitValue;
2321

2422
/**
2523
* @param array $conditions
2624
* @param string|null $sortField
27-
* @param string|null $sortDir
25+
* @param int|null $sortDir
2826
* @param int|null $offset
2927
* @param int|null $limit
3028
*/
31-
public function __construct(array $conditions = [], string $sortField = null, string $sortDir = null, int $offset = null, int $limit = null)
32-
{
33-
if($conditions = $this->parseConditionsArg($conditions)) {
29+
public function __construct(
30+
array $conditions = [],
31+
string $sortField = null,
32+
int $sortDir = null,
33+
int $offset = null,
34+
int $limit = null
35+
) {
36+
if ($conditions = $this->parseConditionsArg($conditions)) {
3437
$this->conditions = $conditions;
3538
}
36-
if($sortField && $sortDir) {
39+
if ($sortField !== null && $sortDir !== null) {
3740
$this->addSort($sortField, $sortDir);
3841
}
39-
if($offset) {
42+
if ($offset !== null) {
4043
$this->offset($offset);
4144
}
42-
if($limit) {
45+
if ($limit !== null) {
4346
$this->limit($limit);
4447
}
4548
}
@@ -165,7 +168,7 @@ public function addSort(string $sortField, int $sortDir)
165168
*/
166169
public function offset(int $offset)
167170
{
168-
$this->offset = $offset;
171+
$this->offsetValue = $offset;
169172
return $this;
170173
}
171174

@@ -175,65 +178,63 @@ public function offset(int $offset)
175178
*/
176179
public function limit(int $limit)
177180
{
178-
$this->limit = $limit;
181+
$this->limitValue = $limit;
179182
return $this;
180183
}
181184

182185
/**
183186
* @return array
184187
*/
185-
public function getConditions()
188+
public function getConditions(): array
186189
{
187190
return $this->conditions;
188191
}
189192

190193
/**
191194
* @return array
192195
*/
193-
public function getSort()
196+
public function getSort(): array
194197
{
195198
return $this->sort;
196199
}
197200

198201
/**
199-
* @return null
202+
* @return null|int
200203
*/
201204
public function getOffset()
202205
{
203-
return $this->offset;
206+
return $this->offsetValue;
204207
}
205208

206209
/**
207-
* @return null
210+
* @return null|int
208211
*/
209212
public function getLimit()
210213
{
211-
return $this->limit;
214+
return $this->limitValue;
212215
}
213216

214217
/**
215218
* @param $conditions
216219
* @return array
217220
*/
218-
protected function parseConditionsArg($conditions)
221+
protected function parseConditionsArg($conditions): array
219222
{
220223
$result = [];
221-
if(is_array($conditions)) {
224+
if (\is_array($conditions)) {
222225
foreach ($conditions as $k => $v) {
223-
if(is_numeric($k) && is_array($v)) {
226+
if (\is_numeric($k) && \is_array($v)) {
224227
$result[] = $v;
225228
}
226-
else if(is_string($k) && $v) {
227-
if(is_scalar($v)) {
229+
if (\is_string($k) && $v) {
230+
if (\is_scalar($v)) {
228231
$result[] = [self::EQUALS, $k, $v];
229-
}
230-
else if(is_array($v)) {
232+
} elseif (\is_array($v)) {
231233
$result[] = [self::IN, $k, $v];
232234
}
233235
}
234236
}
235237
}
236238
return $result;
237239
}
238-
239-
}
240+
}

src/condition/ConditionDbParser.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44

55
use Doctrine\DBAL\Query\QueryBuilder;
66

7-
87
class ConditionDbParser
98
{
109

1110
public function parse(QueryBuilder $queryBuilder, Condition $condition = null)
1211
{
13-
if(!$condition) {
12+
if ($condition === null) {
1413
return;
1514
}
1615
foreach ($condition->getConditions() as $cond) {
17-
if(is_array($cond)) {
18-
$cond = array_values($cond);
19-
$type = array_shift($cond);
16+
if (\is_array($cond)) {
17+
$cond = \array_values($cond);
18+
$type = \array_shift($cond);
2019
switch ($type) {
2120
case Condition::EQUALS:
2221
$queryBuilder->andWhere("{$cond[0]} = " . $queryBuilder->createNamedParameter($cond[1]));
@@ -38,7 +37,7 @@ public function parse(QueryBuilder $queryBuilder, Condition $condition = null)
3837
break;
3938
case Condition::BETWEEN:
4039
$queryBuilder->andWhere("{$cond[0]} BETWEEN " . $queryBuilder->createNamedParameter($cond[1])
41-
. " AND " . $queryBuilder->createNamedParameter($cond[2]));
40+
. ' AND ' . $queryBuilder->createNamedParameter($cond[2]));
4241
break;
4342
case Condition::LIKE:
4443
$value = $cond[2] ? '%' : '';
@@ -47,15 +46,16 @@ public function parse(QueryBuilder $queryBuilder, Condition $condition = null)
4746
$queryBuilder->andWhere("{$cond[0]} LIKE " . $queryBuilder->createNamedParameter($value));
4847
break;
4948
case Condition::IN:
50-
if($cond[1]) {
49+
if ($cond[1]) {
5150
$isNumeric = $cond[2];
5251
$sql = '';
53-
foreach ($cond[1] as $v) {
52+
/** @var array $values */
53+
$values = $cond[1];
54+
foreach ($values as $v) {
5455
$sql .= ($isNumeric ? (float)$v : $queryBuilder->createNamedParameter($v)) . ',';
5556
}
56-
$queryBuilder->andWhere("{$cond[0]} IN (" . rtrim($sql, ',') . ")");
57-
}
58-
else {
57+
$queryBuilder->andWhere("{$cond[0]} IN (" . \rtrim($sql, ',') . ')');
58+
} else {
5959
$queryBuilder->andWhere('1 = 2'); // IN empty array is always false
6060
}
6161
break;
@@ -64,16 +64,15 @@ public function parse(QueryBuilder $queryBuilder, Condition $condition = null)
6464
}
6565

6666
foreach ($condition->getSort() as $field => $direction) {
67-
$queryBuilder->orderBy($field, $direction == SORT_DESC ? 'desc' : 'asc');
67+
$queryBuilder->orderBy($field, $direction === \SORT_DESC ? 'desc' : 'asc');
6868
}
6969

70-
if($offset = $condition->getOffset()) {
70+
if ($offset = (int)$condition->getOffset()) {
7171
$queryBuilder->setFirstResult($offset);
7272
}
7373

74-
if($limit = $condition->getLimit()) {
74+
if ($limit = (int)$condition->getLimit()) {
7575
$queryBuilder->setMaxResults($limit);
7676
}
7777
}
78-
79-
}
78+
}

src/entity/AbstractEntityEntityRepositoryDb.php

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
namespace WebComplete\core\entity;
44

55
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\Query\QueryBuilder;
67
use WebComplete\core\condition\ConditionDbParser;
78
use WebComplete\core\factory\ObjectFactory;
89
use WebComplete\core\utils\hydrator\HydratorInterface;
910
use WebComplete\core\condition\Condition;
1011

11-
1212
abstract class AbstractEntityEntityRepositoryDb extends AbstractEntityRepository
1313
{
14-
1514
const SERIALIZE_STRATEGY_JSON = 1;
1615
const SERIALIZE_STRATEGY_PHP = 2;
1716

@@ -27,13 +26,18 @@ abstract class AbstractEntityEntityRepositoryDb extends AbstractEntityRepository
2726
*/
2827
protected $conditionParser;
2928

30-
29+
/**
30+
* @param ObjectFactory $factory
31+
* @param HydratorInterface $hydrator
32+
* @param ConditionDbParser $conditionParser
33+
* @param Connection $db
34+
*/
3135
public function __construct(
3236
ObjectFactory $factory,
3337
HydratorInterface $hydrator,
3438
ConditionDbParser $conditionParser,
35-
Connection $db)
36-
{
39+
Connection $db
40+
) {
3741
parent::__construct($factory, $hydrator);
3842
$this->db = $db;
3943
$this->conditionParser = $conditionParser;
@@ -50,14 +54,15 @@ public function transaction(\Closure $closure)
5054

5155
/**
5256
* @param $id
53-
* @return AbstractEntity|object|null
57+
* @return AbstractEntity|null
5458
*/
5559
public function findById($id)
5660
{
5761
$result = null;
5862
$select = $this->selectQuery()->where('t1 = :id')->setParameter(':id', $id);
59-
if($row = $select->execute()->fetch()) {
63+
if ($row = $select->execute()->fetch()) {
6064
$this->unserializeFields($row);
65+
/** @var AbstractEntity $result */
6166
$result = $this->factory->createFromData($row);
6267
}
6368

@@ -66,14 +71,15 @@ public function findById($id)
6671

6772
/**
6873
* @param Condition $condition
69-
* @return AbstractEntity|object|null
74+
* @return AbstractEntity|null
7075
*/
7176
public function findOne(Condition $condition)
7277
{
7378
$result = null;
7479
$select = $this->selectQuery($condition);
75-
if($row = $select->execute()->fetch()) {
80+
if ($row = $select->execute()->fetch()) {
7681
$this->unserializeFields($row);
82+
/** @var AbstractEntity $result */
7783
$result = $this->factory->createFromData($row);
7884
}
7985

@@ -84,11 +90,11 @@ public function findOne(Condition $condition)
8490
* @param Condition $condition
8591
* @return AbstractEntity[]
8692
*/
87-
public function findAll(Condition $condition)
93+
public function findAll(Condition $condition): array
8894
{
8995
$result = [];
9096
$select = $this->selectQuery($condition);
91-
if($rows = $select->execute()->fetchAll(\PDO::FETCH_ASSOC)) {
97+
if ($rows = $select->execute()->fetchAll(\PDO::FETCH_ASSOC)) {
9298
foreach ($rows as $row) {
9399
$this->unserializeFields($row);
94100
/** @var AbstractEntity $entity */
@@ -104,7 +110,7 @@ public function findAll(Condition $condition)
104110
* @param Condition $condition
105111
* @return int
106112
*/
107-
public function count(Condition $condition)
113+
public function count(Condition $condition): int
108114
{
109115
$select = $this->selectQuery($condition);
110116
return $select->select(['id'])->execute()->rowCount();
@@ -118,18 +124,19 @@ public function save(AbstractEntity $item)
118124
$data = $this->hydrator->extract($item);
119125
$this->beforeDataSave($data);
120126
$this->serializeFields($data);
121-
if($id = $item->getId()) {
127+
if ($id = $item->getId()) {
122128
$this->db->update($this->table, $data, ['id' => $id]);
123-
}
124-
else {
129+
} else {
125130
unset($data['id']);
126131
$this->db->insert($this->table, $data);
127-
$item->setId($this->db->lastInsertId());
132+
$item->setId((int)$this->db->lastInsertId());
128133
}
129134
}
130135

131136
/**
132137
* @param $id
138+
*
139+
* @throws \Doctrine\DBAL\Exception\InvalidArgumentException
133140
*/
134141
public function delete($id)
135142
{
@@ -138,9 +145,9 @@ public function delete($id)
138145

139146
/**
140147
* @param Condition|null $condition
141-
* @return \Doctrine\DBAL\Query\QueryBuilder
148+
* @return QueryBuilder
142149
*/
143-
protected function selectQuery(Condition $condition = null)
150+
protected function selectQuery(Condition $condition = null): QueryBuilder
144151
{
145152
$queryBuilder = $this->db->createQueryBuilder();
146153
$queryBuilder->select(['t1.*'])->from($this->table, 't1');
@@ -162,10 +169,10 @@ protected function beforeDataSave(&$data)
162169
private function serializeFields(&$data)
163170
{
164171
foreach ($this->serializeFields as $field) {
165-
if(isset($data[$field])) {
166-
$data[$field] = $this->serializeStrategy == self::SERIALIZE_STRATEGY_JSON
167-
? json_encode($data[$field])
168-
: serialize($data[$field]);
172+
if (isset($data[$field])) {
173+
$data[$field] = $this->serializeStrategy === self::SERIALIZE_STRATEGY_JSON
174+
? \json_encode($data[$field])
175+
: \serialize($data[$field]);
169176
}
170177
}
171178
}
@@ -176,12 +183,11 @@ private function serializeFields(&$data)
176183
private function unserializeFields(&$row)
177184
{
178185
foreach ($this->serializeFields as $field) {
179-
if(isset($row[$field])) {
180-
$row[$field] = $this->serializeStrategy == self::SERIALIZE_STRATEGY_JSON
181-
? json_decode($row[$field], true)
182-
: unserialize($row[$field]);
186+
if (isset($row[$field])) {
187+
$row[$field] = $this->serializeStrategy === self::SERIALIZE_STRATEGY_JSON
188+
? \json_decode($row[$field], true)
189+
: \unserialize($row[$field], ['allowed_classes' => true]);
183190
}
184191
}
185192
}
186-
187-
}
193+
}

0 commit comments

Comments
 (0)