1
|
|
|
<?php |
2
|
|
|
namespace HtImgModule\Imagine\Filter; |
3
|
|
|
|
4
|
|
|
use HtImgModule\Exception; |
5
|
|
|
use HtImgModule\Options\FilterOptionsInterface; |
6
|
|
|
use Imagine\Image\ImageInterface; |
7
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
8
|
|
|
|
9
|
|
|
class FilterManager implements FilterManagerInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var FilterOptionsInterface |
13
|
|
|
*/ |
14
|
|
|
protected $filterOptions; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ServiceLocatorInterface |
18
|
|
|
*/ |
19
|
|
|
protected $filterLoaderPluginManager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor |
23
|
|
|
* |
24
|
|
|
* @param FilterOptionsInterface $filterOptions |
25
|
|
|
* @param ServiceLocatorInterface $filterLoaderPluginManager |
26
|
|
|
*/ |
27
|
7 |
|
public function __construct( |
28
|
|
|
FilterOptionsInterface $filterOptions, |
29
|
|
|
ServiceLocatorInterface $filterLoaderPluginManager |
30
|
|
|
) { |
31
|
7 |
|
$this->filterOptions = $filterOptions; |
32
|
7 |
|
$this->filterLoaderPluginManager = $filterLoaderPluginManager; |
33
|
7 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
4 |
|
public function getFilter($filter) |
39
|
|
|
{ |
40
|
4 |
|
$this->validateFilter($filter); |
41
|
1 |
|
$options = $this->filterOptions->getFilters()[$filter]; |
42
|
|
|
|
43
|
1 |
|
return $this->filterLoaderPluginManager->get($options['type'])->load($options['options']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
1 |
|
public function getFilterOptions($filter) |
50
|
|
|
{ |
51
|
1 |
|
$this->validateFilter($filter); |
52
|
1 |
|
$options = $this->filterOptions->getFilters()[$filter]; |
53
|
|
|
|
54
|
1 |
|
return $options['options']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Validates if filter exists and is valid |
59
|
|
|
* |
60
|
|
|
* @param string $filter Filter Service |
61
|
|
|
* @throws Exception\FilterNotFoundException |
62
|
|
|
* @throws Exception\InvalidArgumentException |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
5 |
|
protected function validateFilter($filter) |
66
|
|
|
{ |
67
|
5 |
|
if (!isset($this->filterOptions->getFilters()[$filter])) { |
68
|
1 |
|
throw new Exception\FilterNotFoundException(sprintf( |
69
|
1 |
|
'Filter "%s" was not found', $filter |
70
|
|
|
)); |
71
|
|
|
} |
72
|
4 |
|
$options = $this->filterOptions->getFilters()[$filter]; |
73
|
4 |
|
if (!isset($options['type'])) { |
74
|
1 |
|
throw new Exception\InvalidArgumentException(sprintf( |
75
|
1 |
|
'Filter type for "%s" image filter must be specified', $filter |
76
|
|
|
)); |
77
|
|
|
} |
78
|
3 |
|
if (!isset($options['options'])) { |
79
|
1 |
|
throw new Exception\InvalidArgumentException(sprintf( |
80
|
1 |
|
'Options for filter type "%s" must be specified', $filter |
81
|
|
|
)); |
82
|
|
|
} |
83
|
2 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
1 |
|
public function applyFilter(ImageInterface $image, $filter) |
89
|
|
|
{ |
90
|
1 |
|
return $this->getFilter($filter)->apply($image); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
*/ |
96
|
1 |
|
public function addFilter($name, array $options) |
97
|
|
|
{ |
98
|
1 |
|
$this->filterOptions->addFilter($name, $options); |
99
|
|
|
|
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|