Skip to content

Commit 9d580b8

Browse files
committed
feat: add costume sort order, limit and offset
- 71 tests, 98 assertions)
1 parent 1b73981 commit 9d580b8

File tree

4 files changed

+155
-1
lines changed

4 files changed

+155
-1
lines changed

src/System/Database/MyModel/Model.php

+98
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class Model implements \ArrayAccess, \IteratorAggregate
4848
* @var Bind[] Binder for PDO bind */
4949
protected $binds = [];
5050

51+
// costume select -------------
52+
53+
protected int $limit_start = 0;
54+
protected int $limit_end = 0;
55+
protected int $offset = 0;
56+
protected string $sort_order = '';
57+
5158
// magic ----------------------
5259

5360
/**
@@ -249,6 +256,8 @@ public function read(): bool
249256
{
250257
$query = new Select($this->table_name, ['*'], $this->pdo);
251258

259+
$query->sortOrderRef($this->limit_start, $this->limit_end, $this->offset, $this->sort_order);
260+
252261
$all = $this->fetch($query);
253262

254263
if ([] === $all) {
@@ -389,6 +398,95 @@ public function toArray(): array
389398
return $this->getColumns();
390399
}
391400

401+
// costume select ------------
402+
403+
/**
404+
* Set data start for feact all data.
405+
*
406+
* @param int $limit_start limit start
407+
* @param int $limit_end limit end
408+
*
409+
* @return static
410+
*/
411+
public function limit(int $limit_start, int $limit_end)
412+
{
413+
$this->limitStart($limit_start);
414+
$this->limitEnd($limit_end);
415+
416+
return $this;
417+
}
418+
419+
/**
420+
* Set data start for feact all data.
421+
*
422+
* @param int $value limit start default is 0
423+
*
424+
* @return static
425+
*/
426+
public function limitStart(int $value)
427+
{
428+
$this->limit_start = $value < 0 ? 0 : $value;
429+
430+
return $this;
431+
}
432+
433+
/**
434+
* Set data end for feact all data
435+
* zero value meaning no data show.
436+
*
437+
* @param int $value limit start default
438+
*
439+
* @return static
440+
*/
441+
public function limitEnd(int $value)
442+
{
443+
$this->limit_end = $value < 0 ? 0 : $value;
444+
445+
return $this;
446+
}
447+
448+
/**
449+
* Set offest.
450+
*
451+
* @param int $value offet
452+
*
453+
* @return static
454+
*/
455+
public function offset(int $value)
456+
{
457+
$this->offset = $value < 0 ? 0 : $value;
458+
459+
return $this;
460+
}
461+
462+
/**
463+
* Set limit using limit and offset.
464+
*
465+
* @return static
466+
*/
467+
public function limitOffset(int $limit, int $offset)
468+
{
469+
return $this
470+
->limitStart($limit)
471+
->limitEnd(0)
472+
->offset($offset);
473+
}
474+
475+
/**
476+
* Set sort column and order
477+
* column name must register.
478+
*
479+
* @return static
480+
*/
481+
public function order(string $column_name, int $order_using = MyQuery::ORDER_ASC, ?string $belong_to = null)
482+
{
483+
$order = $order_using == 0 ? 'ASC' : 'DESC';
484+
$belong_to = $belong_to ?? $this->table_name;
485+
$this->sort_order = "ORDER BY `$belong_to`.`$column_name` $order";
486+
487+
return $this;
488+
}
489+
392490
// array access --------------
393491

394492
/**

src/System/Database/MyQuery/Select.php

+8
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,12 @@ private function getLimit(): string
201201

202202
return "LIMIT $this->_limit_start, $this->_limit_end";
203203
}
204+
205+
public function sortOrderRef(int $limit_start, int $limit_end, int $offset, string $sort_ordder): void
206+
{
207+
$this->_limit_start = $limit_start;
208+
$this->_limit_end = $limit_end;
209+
$this->_offset = $offset;
210+
$this->_sort_order = $sort_ordder;
211+
}
204212
}

tests/DataBase/Model/BaseModelTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function itCanCreateData()
105105
*/
106106
public function itCanReadData()
107107
{
108-
$user = new User($this->pdo, [[]], ['user' => ['taylor']]);
108+
$user = new User($this->pdo, []);
109109

110110
$this->assertTrue($user->read());
111111
}

tests/DataBase/Model/CostumeModelTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace System\Test\Database\Model;
66

77
use System\Database\MyModel\Model;
8+
use System\Database\MyQuery;
89
use System\Database\MyQuery\Insert;
910
use System\Test\Database\BaseConnection;
1011

@@ -112,6 +113,53 @@ public function itCanFilterModelChain(): void
112113
$this->assertGreaterThan(30, $profile->getter('gender'));
113114
}
114115
}
116+
117+
/**
118+
* @test
119+
*
120+
* @group database
121+
*/
122+
public function itCanlimitOrder(): void
123+
{
124+
$profiles = $this->profiles();
125+
$profiles->limitEnd(2);
126+
$profiles->read();
127+
128+
$this->assertEquals(2, $profiles->get()->count());
129+
}
130+
131+
/**
132+
* @test
133+
*
134+
* @group database
135+
*/
136+
public function itCanlimitOffset(): void
137+
{
138+
$profiles = $this->profiles();
139+
$profiles->limitOffset(1, 2);
140+
$profiles->read();
141+
142+
$this->assertEquals(1, $profiles->get()->count());
143+
}
144+
145+
/**
146+
* @test
147+
*
148+
* @group database
149+
*/
150+
public function itCanShortOrder(): void
151+
{
152+
$profiles = $this->profiles();
153+
154+
$profiles->order('user', MyQuery::ORDER_ASC);
155+
$profiles->read();
156+
$this->assertEquals([
157+
'user' => 'jesica',
158+
'name' => 'jesica w',
159+
'gender' => 'female',
160+
'age' => 38,
161+
], $profiles->first());
162+
}
115163
}
116164

117165
class Profile extends Model

0 commit comments

Comments
 (0)