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
|
|
|
/** |
18
|
|
|
* @var ContainerInterface |
19
|
|
|
*/ |
20
|
|
|
private $container; |
21
|
|
|
|
22
|
|
|
private $adapters = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* AdapterFactory constructor. |
26
|
|
|
* |
27
|
|
|
* @param ContainerInterface $container |
28
|
|
|
*/ |
29
|
|
|
public function __construct(ContainerInterface $container) { |
30
|
|
|
$this->container = $container; |
31
|
|
|
|
32
|
|
|
$this->addAdapter(self::COLLECTION_ADAPTER, function () { |
33
|
|
|
return $this->container->get('adapter.collection'); |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
$this->addAdapter(self::PAGINATION_ADAPTER, function () { |
37
|
|
|
return $this->container->get('adapter.pagination'); |
38
|
|
|
}); |
39
|
|
|
|
40
|
|
|
$this->addAdapter(self::ORDER_ADAPTER, function () { |
41
|
|
|
return $this->container->get('adapter.order'); |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
$this->addAdapter(self::FILTER_ADAPTER, function () { |
45
|
|
|
return $this->container->get('adapter.filter'); |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
$this->addAdapter(self::LIMIT_ADAPTER, function () { |
49
|
|
|
return $this->container->get('adapter.limit'); |
50
|
|
|
}); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $adapterName |
55
|
|
|
* @param callable $filter |
56
|
|
|
*/ |
57
|
|
|
public function addAdapter(string $adapterName, callable $filter) { |
58
|
|
|
$this->adapters[$adapterName] = $filter; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $type |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
* |
66
|
|
|
* @throws UnknownAdapterException |
67
|
|
|
*/ |
68
|
|
|
public function getByType($type) : Adapter { |
69
|
|
|
if (!isset($this->adapters[$type])) { |
70
|
|
|
throw new UnknownAdapterException(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->adapters[$type]($this->container); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|