Skip to content

Commit 91cf00a

Browse files
committed
Added $page->itemsCount. Move set of $page->hasPrev to separate pipe.
1 parent 309e20a commit 91cf00a

File tree

14 files changed

+164
-25
lines changed

14 files changed

+164
-25
lines changed

examples/count.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
var_dump($page->hasNext);
3838
var_dump($page->hasPrev);
3939
var_dump($page->items->count());
40+
var_dump($page->itemsCount);
4041
foreach ($page->items as $user) {
4142
var_dump($user);
4243
}

examples/id.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
var_dump($page->hasNext);
3333
var_dump($page->hasPrev);
3434
var_dump($page->items->count());
35+
var_dump($page->itemsCount);
3536
foreach ($page->items as $user) {
3637
var_dump($user);
3738
}

examples/offset.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
var_dump($page->hasNext);
2929
var_dump($page->hasPrev);
3030
var_dump($page->items->count());
31+
var_dump($page->itemsCount);
32+
3133
foreach ($page->items as $user) {
3234
var_dump($user);
3335
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace spec\Makedo\Paginator\Page\Builder;
4+
5+
use Makedo\Paginator\Loader\Result;
6+
use Makedo\Paginator\Page\Builder\CountItems;
7+
use Makedo\Paginator\Page\Builder\Pipe;
8+
use Makedo\Paginator\Page\Page;
9+
use PhpSpec\ObjectBehavior;
10+
11+
class CountItemsSpec extends ObjectBehavior
12+
{
13+
const PER_PAGE = 3;
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType(CountItems::class);
18+
$this->shouldImplement(Pipe::class);
19+
}
20+
21+
function it_sets_items_count(Page $page)
22+
{
23+
$page = $page->getWrappedObject();
24+
$page->items = Result::fromIterable([1,2,3]);
25+
$page->perPage = 10;
26+
27+
$page = $this->build($page);
28+
29+
$page->itemsCount->shouldBe(self::PER_PAGE);
30+
}
31+
32+
function it_sets_items_count_as_per_page(Page $page)
33+
{
34+
$page = $page->getWrappedObject();
35+
36+
$page->items = Result::fromIterable([1,2,3,4]);
37+
$page->perPage = self::PER_PAGE;
38+
39+
$page = $this->build($page);
40+
41+
$page->itemsCount->shouldBe(self::PER_PAGE);
42+
}
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace spec\Makedo\Paginator\Page\Builder;
4+
5+
use Makedo\Paginator\Page\Builder\HasPrev;
6+
use Makedo\Paginator\Page\Builder\Pipe;
7+
use Makedo\Paginator\Page\Page;
8+
use Makedo\Paginator\Strategy\Skip\Skip;
9+
use PhpSpec\ObjectBehavior;
10+
11+
class HasPrevSpec extends ObjectBehavior
12+
{
13+
function let(Skip $skip)
14+
{
15+
$this->beConstructedWith($skip);
16+
}
17+
18+
function it_is_initializable()
19+
{
20+
$this->shouldHaveType(HasPrev::class);
21+
$this->shouldImplement(Pipe::class);
22+
}
23+
24+
function it_sets_has_prev_as_has_skip(Skip $skip, Page $page)
25+
{
26+
$skip->hasSkip()->willReturn(true);
27+
28+
$page = $page->getWrappedObject();
29+
30+
$page = $this->build($page);
31+
32+
$page->hasPrev->shouldBe(true);
33+
}
34+
35+
}

spec/Paginator/Page/Builder/LoadItemsSpec.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ public function it_fills_page_with_items(
4242
->shouldBeCalled()
4343
;
4444

45-
$hasSkip = false;
46-
$skipStrategy->hasSkip()
47-
->willReturn($hasSkip)
48-
->shouldBeCalled()
49-
;
50-
5145
$items = [1,2,3,4,5];
5246
$loader->load($limit, $skip)
5347
->willReturn($items)
@@ -57,6 +51,5 @@ public function it_fills_page_with_items(
5751
$page = $this->build($page);
5852

5953
$page->items->shouldBeLike(Result::fromIterable($items));
60-
$page->hasPrev->shouldBe($hasSkip);
6154
}
6255
}

spec/Paginator/PaginatorBuilderSpec.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ function it_builds_paginator_with_offset_skip_strategy(Loader $loader)
4646
$page->currentPage->shouldBe($currentPage);
4747
$page->perPage->shouldBe(self::PER_PAGE);
4848
$page->items->shouldBe($result);
49+
$page->itemsCount->shouldBe(self::PER_PAGE);
50+
$page->items->count()->shouldBe($result->count());
4951
$page->hasPrev->shouldBe(true);
5052
$page->hasNext->shouldBe(true);
5153
}
@@ -105,6 +107,8 @@ function it_builds_paginator_with_offset_skip_strategy_and_count(
105107
$page->currentPage->shouldBe($currentPage);
106108
$page->perPage->shouldBe(self::PER_PAGE);
107109
$page->items->shouldBe($result);
110+
$page->itemsCount->shouldBe(self::PER_PAGE);
111+
$page->items->count()->shouldBe($result->count());
108112
$page->hasPrev->shouldBe(true);
109113
$page->hasNext->shouldBe(true);
110114

@@ -140,6 +144,8 @@ function it_builds_paginator_with_skip_by_id_and_count(
140144

141145
$page->currentPage->shouldBe(null);
142146
$page->perPage->shouldBe(self::PER_PAGE);
147+
$page->itemsCount->shouldBe(self::PER_PAGE);
148+
$page->items->count()->shouldBe($result->count());
143149
$page->items->shouldBe($result);
144150
$page->hasPrev->shouldBe(true);
145151
$page->hasNext->shouldBe(true);
@@ -156,7 +162,7 @@ function it_builds_paginator_with_skip_by_id_and_count_and_current_page(
156162
$id = 55;
157163
$limit = self::PER_PAGE;
158164

159-
$result = Result::fromArray([1,2,3,4], self::PER_PAGE);
165+
$result = Result::fromArray([1,2,3,4,5], self::PER_PAGE);
160166
$loader->load($limit, $id)
161167
->willReturn($result)
162168
->shouldBeCalled()
@@ -179,6 +185,8 @@ function it_builds_paginator_with_skip_by_id_and_count_and_current_page(
179185
$page->currentPage->shouldBe($currentPage);
180186
$page->perPage->shouldBe(self::PER_PAGE);
181187
$page->items->shouldBe($result);
188+
$page->itemsCount->shouldBe(self::PER_PAGE);
189+
$page->items->count()->shouldBe($result->count());
182190
$page->hasPrev->shouldBe(true);
183191
$page->hasNext->shouldBe(true);
184192

src/Paginator/Loader/Result.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@ final class Result implements IteratorAggregate, Countable
1414
/**
1515
* @var Iterator
1616
*/
17-
private $iterator;
17+
private $items;
18+
19+
/**
20+
* @var int|null
21+
*/
22+
private $limit;
1823

1924
/**
2025
* @var int
2126
*/
2227
private $count;
2328

24-
public function __construct(Iterator $items, ?int $count = null, ?int $limit = null)
29+
public function __construct(Iterator $items, ?int $limit = null, ?int $count = null)
2530
{
26-
$this->iterator = $limit ? new LimitIterator($items, 0, $limit) : $items;
31+
$this->items = $items;
32+
$this->limit = $limit;
2733
$this->count = $count;
2834
}
2935

@@ -42,26 +48,28 @@ public static function fromIterable(iterable $items, ?int $limit = null): self
4248

4349
public static function fromArray(array $items, ?int $limit = null): self
4450
{
45-
return new self(
46-
new ArrayIterator($items),
47-
count($items),
48-
$limit
49-
);
51+
return new self(new ArrayIterator($items), $limit);
5052
}
5153

5254
public static function fromIterator(Iterator $items, ?int $limit = null): self
5355
{
54-
$count = self::countItems($items);
55-
return new self($items, $count, $limit);
56+
return new self($items, $limit);
5657
}
5758

5859
public function getIterator(): Iterator
5960
{
60-
return $this->iterator;
61+
return $this->limit
62+
? new LimitIterator($this->items, 0, $this->limit)
63+
: $this->items
64+
;
6165
}
6266

6367
public function count(): int
6468
{
69+
if (null === $this->count) {
70+
$this->count = self::countItems($this->items);
71+
}
72+
6573
return $this->count;
6674
}
6775

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
4+
namespace Makedo\Paginator\Page\Builder;
5+
6+
use Makedo\Paginator\Page\Page;
7+
8+
class CountItems implements Pipe
9+
{
10+
public function build(Page $page): Page
11+
{
12+
$count = $page->items->count();
13+
$page->itemsCount = $count > $page->perPage ? $count - 1 : $count;
14+
15+
return $page;
16+
}
17+
}

src/Paginator/Page/Builder/HasNextByItemsCount.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ public function build(Page $page): Page
1111
$page->hasNext = $page->items->count() > $page->perPage;
1212
return $page;
1313
}
14-
1514
}

0 commit comments

Comments
 (0)