Skip to content
This repository was archived by the owner on Jul 28, 2023. It is now read-only.

Commit 7b7492d

Browse files
committed
refactor(cycle): refactor CycleQuery
1 parent 18326cf commit 7b7492d

File tree

2 files changed

+75
-25
lines changed

2 files changed

+75
-25
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
2222
- Nothing
2323
-->
2424

25+
## [1.4.1] - 2021-02-25
26+
27+
### Fixed
28+
29+
- Refactor CycleQuery.
30+
2531
## [1.4.0] - 2021-02-25
2632

2733
### Added

src/Bridge/CycleOrm/Query/CycleQuery.php

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use spaceonfire\Collection\CollectionInterface;
1010
use spaceonfire\Collection\TypedCollection;
1111
use spaceonfire\Criteria\Bridge\SpiralPagination\PaginableCriteria;
12+
use spaceonfire\Criteria\Criteria;
1213
use spaceonfire\Criteria\CriteriaInterface;
1314
use spaceonfire\DataSource\EntityInterface;
1415
use spaceonfire\DataSource\MapperInterface;
@@ -21,11 +22,27 @@ class CycleQuery implements QueryInterface
2122
* @var Select
2223
*/
2324
protected $select;
25+
2426
/**
2527
* @var MapperInterface
2628
*/
2729
protected $mapper;
2830

31+
/**
32+
* @var CriteriaInterface|null
33+
*/
34+
protected $criteria;
35+
36+
/**
37+
* @var int|null
38+
*/
39+
protected $limit;
40+
41+
/**
42+
* @var int|null
43+
*/
44+
protected $offset;
45+
2946
/**
3047
* CycleQuery constructor.
3148
* @param Select $select
@@ -40,18 +57,30 @@ public function __construct(Select $select, MapperInterface $mapper)
4057
/**
4158
* @inheritDoc
4259
*/
43-
public function limit(int $limit): QueryInterface
60+
public function limit(?int $limit): QueryInterface
61+
{
62+
$this->limit = $limit;
63+
64+
return $this;
65+
}
66+
67+
/**
68+
* @inheritDoc
69+
*/
70+
public function offset(?int $offset): QueryInterface
4471
{
45-
$this->select->limit($limit);
72+
$this->offset = $offset;
73+
4674
return $this;
4775
}
4876

4977
/**
5078
* @inheritDoc
5179
*/
52-
public function offset(int $offset): QueryInterface
80+
public function matching(CriteriaInterface $criteria): QueryInterface
5381
{
54-
$this->select->offset($offset);
82+
$this->criteria = $criteria;
83+
5584
return $this;
5685
}
5786

@@ -60,7 +89,7 @@ public function offset(int $offset): QueryInterface
6089
*/
6190
public function fetchOne(): ?EntityInterface
6291
{
63-
$entity = $this->select->fetchOne();
92+
$entity = $this->makeSelect()->fetchOne();
6493

6594
if ($entity === null) {
6695
return null;
@@ -80,54 +109,69 @@ public function fetchOne(): ?EntityInterface
80109
*/
81110
public function fetchAll(): CollectionInterface
82111
{
83-
$items = $this->select->fetchAll();
112+
$items = $this->makeSelect()->fetchAll();
84113

85114
return new TypedCollection($items, EntityInterface::class);
86115
}
87116

88117
/**
89118
* @inheritDoc
90119
*/
91-
public function matching(CriteriaInterface $criteria): QueryInterface
120+
public function count(?string $column = null): int
121+
{
122+
$select = clone $this->select;
123+
124+
if (null !== $this->criteria) {
125+
$criteria = clone $this->criteria;
126+
$criteria->limit(null);
127+
$criteria->offset(null);
128+
129+
$select = $this->applyCriteria($select, $criteria);
130+
}
131+
132+
return $select->count($column);
133+
}
134+
135+
private function makeSelect(): Select
136+
{
137+
$select = clone $this->select;
138+
139+
$criteria = null === $this->criteria ? new Criteria() : clone $this->criteria;
140+
$criteria->limit($this->limit ?? $criteria->getLimit());
141+
$criteria->offset($this->offset ?? $criteria->getOffset());
142+
143+
return $this->applyCriteria($select, $criteria);
144+
}
145+
146+
private function applyCriteria(Select $select, CriteriaInterface $criteria): Select
92147
{
93148
if ($expression = $criteria->getWhere()) {
94149
$scope = (new CycleQueryExpressionVisitor($this->mapper))->dispatch($expression);
95-
$this->select->andWhere($scope);
150+
$select->andWhere($scope);
96151
}
97152

98153
foreach ($criteria->getOrderBy() as $key => $order) {
99-
$this->select->orderBy(
154+
$select->orderBy(
100155
$this->mapper->convertNameToStorage($key),
101156
$order === SORT_ASC ? SelectQuery::SORT_ASC : SelectQuery::SORT_DESC
102157
);
103158
}
104159

105160
if ($criteria instanceof PaginableCriteria) {
106-
$criteria->paginate($this);
161+
$criteria->paginate($select);
107162
} else {
108163
if ($criteria->getOffset()) {
109-
$this->offset($criteria->getOffset());
164+
$select->offset($criteria->getOffset());
110165
}
111166

112167
if ($criteria->getLimit() !== null) {
113-
$this->limit($criteria->getLimit());
168+
$select->limit($criteria->getLimit());
114169
}
115170
}
116171

117172
// TODO: support load options
118-
$this->select->load($criteria->getInclude());
119-
120-
return $this;
121-
}
173+
$select->load($criteria->getInclude());
122174

123-
/**
124-
* @inheritDoc
125-
*/
126-
public function count(?string $column = null): int
127-
{
128-
$select = clone $this->select;
129-
$select->limit(-1);
130-
$select->offset(0);
131-
return $select->count($column);
175+
return $select;
132176
}
133177
}

0 commit comments

Comments
 (0)