1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Factory; |
4
|
|
|
|
5
|
|
|
use Brendt\Stitcher\Adapter\Adapter; |
6
|
|
|
use Brendt\Stitcher\Exception\UnknownAdapterException; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
8
|
|
|
|
9
|
|
|
class AdapterFactory |
10
|
|
|
{ |
11
|
|
|
const COLLECTION_ADAPTER = 'collection'; |
12
|
|
|
const PAGINATION_ADAPTER = 'pagination'; |
13
|
|
|
const ORDER_ADAPTER = 'order'; |
14
|
|
|
const FILTER_ADAPTER = 'filter'; |
15
|
|
|
const LIMIT_ADAPTER = 'limit'; |
16
|
|
|
|
17
|
|
|
private $container; |
18
|
|
|
private $adapters = []; |
19
|
|
|
|
20
|
57 |
|
public function __construct(ContainerInterface $container) |
21
|
|
|
{ |
22
|
57 |
|
$this->container = $container; |
23
|
|
|
|
24
|
|
|
$this->addAdapter(self::COLLECTION_ADAPTER, function () { |
25
|
13 |
|
return $this->container->get('adapter.collection'); |
26
|
57 |
|
}); |
27
|
|
|
|
28
|
|
|
$this->addAdapter(self::PAGINATION_ADAPTER, function () { |
29
|
|
|
return $this->container->get('adapter.pagination'); |
30
|
57 |
|
}); |
31
|
|
|
|
32
|
|
|
$this->addAdapter(self::ORDER_ADAPTER, function () { |
33
|
1 |
|
return $this->container->get('adapter.order'); |
34
|
57 |
|
}); |
35
|
|
|
|
36
|
|
|
$this->addAdapter(self::FILTER_ADAPTER, function () { |
37
|
1 |
|
return $this->container->get('adapter.filter'); |
38
|
57 |
|
}); |
39
|
|
|
|
40
|
57 |
|
$this->addAdapter(self::LIMIT_ADAPTER, function () { |
41
|
|
|
return $this->container->get('adapter.limit'); |
42
|
57 |
|
}); |
43
|
57 |
|
} |
44
|
|
|
|
45
|
57 |
|
public function addAdapter(string $adapterName, callable $filter) |
46
|
|
|
{ |
47
|
57 |
|
$this->adapters[$adapterName] = $filter; |
48
|
57 |
|
} |
49
|
|
|
|
50
|
14 |
|
public function getByType($type) : Adapter |
51
|
|
|
{ |
52
|
14 |
|
if (!isset($this->adapters[$type])) { |
53
|
|
|
throw new UnknownAdapterException(); |
54
|
|
|
} |
55
|
|
|
|
56
|
14 |
|
return $this->adapters[$type]($this->container); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|