Skip to content

Commit 45fe740

Browse files
dsavinahomersimpsons
authored andcommitted
Replace Porpaginas with self-owned code
- Interface signatures now compatible with PHP 8.1 (covariance issue)
1 parent 67e4ee7 commit 45fe740

9 files changed

+111
-51
lines changed

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"doctrine/dbal": "^2.9.2",
2525
"psr/log": "^1 || ^2 || ^3",
2626
"doctrine/inflector": "^1.4.3 || ^2",
27-
"beberlei/porpaginas": "~1.0",
2827
"mouf/classname-mapper": "~1.0",
2928
"doctrine/cache": "^1.6",
3029
"greenlion/php-sql-parser": "^4.3.0",

src/AlterableResultIterator.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44

55
namespace TheCodingMachine\TDBM;
66

7-
use Porpaginas\Arrays\ArrayPage;
8-
use Porpaginas\Iterator;
9-
use Porpaginas\Result;
10-
117
/**
128
* This class acts as a wrapper around a result iterator.
139
* It can be used to add or remove results from a ResultIterator (or any kind a traversable collection).
1410
*
1511
* Note: in the case of TDBM, this is useful to manage many to one relationships
1612
*/
17-
class AlterableResultIterator implements Result, \ArrayAccess, \JsonSerializable
13+
class AlterableResultIterator implements ResultInterface, \ArrayAccess, \JsonSerializable
1814
{
1915
/**
2016
* @var \Traversable|null
@@ -202,16 +198,10 @@ public function offsetUnset($offset)
202198
throw new TDBMInvalidOperationException('You can unset values in a TDBM result set, even in an alterable one. Use the delete method instead.');
203199
}
204200

205-
/**
206-
* @param int $offset
207-
* @param int $limit
208-
*
209-
* @return \Porpaginas\Page
210-
*/
211-
public function take($offset, $limit)
201+
public function take(int $offset, int $limit): PageInterface
212202
{
213203
// TODO: replace this with a class implementing the map method.
214-
return new ArrayPage(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray()));
204+
return new PageArray(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray()));
215205
}
216206

217207
/**

src/PageArray.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\TDBM;
6+
7+
use ArrayIterator;
8+
use Traversable;
9+
10+
class PageArray implements PageInterface
11+
{
12+
private array $items;
13+
private int $offset;
14+
private int $limit;
15+
private int $totalCount;
16+
17+
/**
18+
* @param array $items
19+
*/
20+
public function __construct(array $items, int $offset, int $limit, int $totalCount)
21+
{
22+
$this->items = $items;
23+
$this->offset = $offset;
24+
$this->limit = $limit;
25+
$this->totalCount = $totalCount;
26+
}
27+
28+
public function count(): int
29+
{
30+
return count($this->items);
31+
}
32+
33+
public function totalCount(): int
34+
{
35+
return $this->totalCount;
36+
}
37+
38+
public function getCurrentOffset(): int
39+
{
40+
return $this->offset;
41+
}
42+
43+
public function getCurrentPage(): int
44+
{
45+
return (int) floor($this->offset / $this->limit) + 1;
46+
}
47+
48+
public function getCurrentLimit(): int
49+
{
50+
return $this->limit;
51+
}
52+
53+
public function getIterator(): Traversable
54+
{
55+
return new ArrayIterator($this->items);
56+
}
57+
}

src/PageInterface.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\TDBM;
6+
7+
use Countable;
8+
use IteratorAggregate;
9+
10+
interface PageInterface extends Countable, IteratorAggregate
11+
{
12+
/**
13+
* Returns the total number of results in the paginated collection.
14+
*/
15+
public function totalCount(): int;
16+
17+
public function getCurrentOffset(): int;
18+
19+
public function getCurrentPage(): int;
20+
21+
public function getCurrentLimit(): int;
22+
}

src/PageIterator.php

+6-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Doctrine\DBAL\Statement;
88
use Mouf\Database\MagicQuery;
9-
use Porpaginas\Page;
109
use Psr\Log\LoggerInterface;
1110
use Psr\Log\NullLogger;
1211

@@ -31,7 +30,7 @@
3130
/**
3231
* Iterator used to retrieve results.
3332
*/
34-
class PageIterator implements Page, \ArrayAccess, \JsonSerializable
33+
class PageIterator implements PageInterface, \ArrayAccess, \JsonSerializable
3534
{
3635
/** @var Statement */
3736
protected $statement;
@@ -133,46 +132,33 @@ public function getIterator()
133132
return $this->innerResultIterator;
134133
}
135134

136-
/**
137-
* @return int
138-
*/
139-
public function getCurrentOffset()
135+
public function getCurrentOffset(): int
140136
{
141137
return $this->offset;
142138
}
143139

144-
/**
145-
* @return int
146-
*/
147-
public function getCurrentPage()
140+
public function getCurrentPage(): int
148141
{
149142
return (int) floor($this->offset / $this->limit) + 1;
150143
}
151144

152-
/**
153-
* @return int
154-
*/
155-
public function getCurrentLimit()
145+
public function getCurrentLimit(): int
156146
{
157147
return $this->limit;
158148
}
159149

160150
/**
161151
* Return the number of results on the current page of the {@link Result}.
162-
*
163-
* @return int
164152
*/
165-
public function count()
153+
public function count(): int
166154
{
167155
return $this->getIterator()->count();
168156
}
169157

170158
/**
171159
* Return the number of ALL results in the paginatable of {@link Result}.
172-
*
173-
* @return int
174160
*/
175-
public function totalCount()
161+
public function totalCount(): int
176162
{
177163
return $this->parentResult->count();
178164
}

src/ResultInterface.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\TDBM;
6+
7+
use Countable;
8+
use IteratorAggregate;
9+
10+
interface ResultInterface extends Countable, IteratorAggregate
11+
{
12+
public function take(int $offset, int $limit): PageInterface;
13+
}

src/ResultIterator.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Doctrine\DBAL\Statement;
1111
use Mouf\Database\MagicQuery;
1212
use TheCodingMachine\TDBM\QueryFactory\QueryFactory;
13-
use Porpaginas\Result;
1413
use Psr\Log\LoggerInterface;
1514
use TheCodingMachine\TDBM\Utils\DbalUtils;
1615
use Traversable;
@@ -41,7 +40,7 @@
4140
/**
4241
* Iterator used to retrieve results.
4342
*/
44-
class ResultIterator implements Result, \ArrayAccess, \JsonSerializable
43+
class ResultIterator implements ResultInterface, \ArrayAccess, \JsonSerializable
4544
{
4645
/** @var Statement */
4746
protected $statement;
@@ -176,13 +175,7 @@ public function getIterator()
176175
return $this->innerResultIterator;
177176
}
178177

179-
/**
180-
* @param int $offset
181-
* @param int $limit
182-
*
183-
* @return PageIterator
184-
*/
185-
public function take($offset, $limit)
178+
public function take(int $offset, int $limit): PageIterator
186179
{
187180
if ($this->totalCount === 0) {
188181
return PageIterator::createEmpyIterator($this);

tests/Dao/TestCountryDao.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace TheCodingMachine\TDBM\Dao;
66

7-
use Porpaginas\Result;
7+
use TheCodingMachine\TDBM\ResultInterface;
88
use TheCodingMachine\TDBM\Test\Dao\Bean\CountryBean;
99
use TheCodingMachine\TDBM\Test\Dao\Generated\CountryBaseDao;
1010

@@ -14,7 +14,7 @@
1414
class TestCountryDao extends CountryBaseDao
1515
{
1616
/**
17-
* @return CountryBean[]|Result
17+
* @return CountryBean[]|ResultInterface
1818
*/
1919
public function getCountriesByUserCount()
2020
{
@@ -30,7 +30,7 @@ public function getCountriesByUserCount()
3030
}
3131

3232
/**
33-
* @return CountryBean[]|Result
33+
* @return CountryBean[]|ResultInterface
3434
*/
3535
public function getCountriesUsingUnion()
3636
{
@@ -48,7 +48,7 @@ public function getCountriesUsingUnion()
4848
}
4949

5050
/**
51-
* @return CountryBean[]|Result
51+
* @return CountryBean[]|ResultInterface
5252
*/
5353
public function getCountriesUsingSimpleQuery()
5454
{
@@ -62,7 +62,7 @@ public function getCountriesUsingSimpleQuery()
6262
}
6363

6464
/**
65-
* @return CountryBean[]|Result
65+
* @return CountryBean[]|ResultInterface
6666
*/
6767
public function getCountriesUsingDistinctQuery()
6868
{

tests/TDBMServiceTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public function testRawSqlFilterCountriesByUserCount(): void
248248
GROUP BY country.id
249249
HAVING COUNT(users.id) > 1;
250250
SQL;
251-
/** @var Test\Dao\Bean\CountryBean[]|\Porpaginas\Result $beans */
251+
/** @var Test\Dao\Bean\CountryBean[]|ResultInterface $beans */
252252
$beans = $this->tdbmService->findObjectsFromRawSql('country', $sql, [], null, Test\Dao\Bean\CountryBean::class, null, CountryResultIterator::class);
253253

254254
$count = 0;
@@ -271,7 +271,7 @@ public function testRawSqlOrderCountriesByUserCount(): void
271271
ORDER BY COUNT(users.id);
272272
SQL;
273273

274-
/** @var Test\Dao\Bean\CountryBean[]|\Porpaginas\Result $beans */
274+
/** @var Test\Dao\Bean\CountryBean[]|ResultInterface $beans */
275275
$beans = $this->tdbmService->findObjectsFromRawSql('country', $sql, [], null, Test\Dao\Bean\CountryBean::class, null, CountryResultIterator::class);
276276

277277
$count = 0;
@@ -300,7 +300,7 @@ public function testRawSqlOrderUsersByCustomRoleOrder(): void
300300
ORDER BY MAX(IF(roles.name = 'Admins', 3, IF(roles.name = 'Writers', 2, IF(roles.name = 'Singers', 1, 0)))) DESC
301301
SQL;
302302

303-
/** @var Test\Dao\Bean\UserBean[]|\Porpaginas\Result $beans */
303+
/** @var Test\Dao\Bean\UserBean[]|ResultInterface $beans */
304304
$beans = $this->tdbmService->findObjectsFromRawSql('contact', $sql, [], null, Test\Dao\Bean\UserBean::class, null, UserResultIterator::class);
305305

306306
function getCustomOrder(Test\Dao\Bean\UserBean $contact)

0 commit comments

Comments
 (0)