1 <?php
 2 
 3 namespace App\Domain\Service;
 4 
 5 
 6 //
 7 // To achieve pagination when listing indexes,
 8 // one needs to get more than just a collection of objects to the view
 9 //
10 // The page number and "per page" values seem they should be transfered via the
11 // "input" properties... but what about the total?
12 //
13 // Part of Output?
14 // Extra?
15 // Message?
16 //
17 class IndexItems
18 {
19     protected $gateway;
20 
21     protected $payload;
22 
23     public function __construct(
24         Gatweay $gateway,
25         Payload $payload
26     ) {
27         $this->gateway = $gateway;
28         $this->payload = $payload;
29     }
30 
31     public function __invoke($page = 1, $paging = 10)
32     {
33         $payload = clone $this->payload;
34 
35         $items = $this->gateway->list($page, $paging);
36 
37         // This seems correct
38         $payload->setInput(['page' => $page, 'paging' => $paging]);
39 
40 
41         // but what about the total?
42         $total = $this->gateway->getTotal();
43 
44         // Everything in output?
45         $payload->setOutput(
46             [
47                 'items' => $items,
48                 'total' => $total
49             ]
50         );
51 
52         // or meta-like thing in extras?
53         $payload->setOutput($items);
54         $payload->setExtras(['total' => $total]);
55 
56         return $payload;
57     }
58 }