Callback.php
1.8 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace ACP\ListScreenRepository;
use AC\ListScreen;
use AC\ListScreenCollection;
use AC\ListScreenRepository;
use AC\ListScreenRepository\Filter;
use AC\Type\ListScreenId;
use ACP\Exception\DecoderNotFoundException;
use ACP\Storage\AbstractDecoderFactory;
use ACP\Storage\Decoder\ListScreenDecoder;
use Closure;
final class Callback implements ListScreenRepository
{
use ListScreenRepository\ListScreenRepositoryTrait;
private $decoder_factory;
/**
* @var Closure
*/
private $callback;
public function __construct(AbstractDecoderFactory $decoder_factory, callable $callback)
{
$this->decoder_factory = $decoder_factory;
$this->callback = Closure::fromCallable($callback);
}
protected function find_from_source(ListScreenId $id): ?ListScreen
{
$list_screens = (new Filter\ListId($id))->filter(
$this->find_all()
);
return $list_screens->get_first() ?: null;
}
protected function find_all_from_source(): ListScreenCollection
{
$collection = new ListScreenCollection();
$callback = $this->callback;
foreach ($callback() as $encoded_list_screen) {
try {
$decoder = $this->decoder_factory->create($encoded_list_screen);
} catch (DecoderNotFoundException $e) {
continue;
}
if ( ! $decoder instanceof ListScreenDecoder || ! $decoder->has_list_screen()) {
continue;
}
$collection->add($decoder->get_list_screen());
}
return $collection;
}
protected function find_all_by_key_from_source(string $key): ListScreenCollection
{
return (new Filter\ListKey($key))->filter(
$this->find_all()
);
}
}