-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cars.php
51 lines (41 loc) · 923 Bytes
/
Cars.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
namespace App\ValueObject;
use App\Elasticsearch\ValueObject\Response;
use App\Elasticsearch\ValueObject\Result;
final class Cars implements \IteratorAggregate, \Countable, PagesTotalAwareInterface
{
private int $total;
private array $cars;
public function __construct(int $total, Car ...$cars)
{
$this->total = $total;
$this->cars = $cars;
}
public static function fromResponse(Response $response): self
{
$cars = $response->results()->map(static function (Result $result) {
return Car::fromResult($result);
});
return new self(
$response->total(),
...$cars
);
}
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->cars);
}
public function count(): int
{
return count($this->cars);
}
public function total(): int
{
return $this->total;
}
public function empty(): bool
{
return 0 === $this->count();
}
}