1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rvdlee\ZfImageOptimiser\Adapter; |
4
|
|
|
|
5
|
|
|
use Rvdlee\ZfImageOptimiser\Interfaces\ImageOptimiserInterface; |
6
|
|
|
use Rvdlee\ZfImageOptimiser\Model\Image; |
7
|
|
|
use Zend\Validator\ValidatorChain; |
8
|
|
|
|
9
|
|
|
abstract class AbstractImageOptimiser implements ImageOptimiserInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $binaryOptions; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $binaryPath; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ValidatorChain |
23
|
|
|
*/ |
24
|
|
|
protected $validatorChain; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* AbstractImageOptimiser constructor. |
28
|
|
|
* |
29
|
|
|
* @param array $binaryOptions |
30
|
|
|
* @param ValidatorChain $validatorChain |
31
|
|
|
*/ |
32
|
|
|
public function __construct(array $binaryOptions, ValidatorChain $validatorChain) |
33
|
|
|
{ |
34
|
|
|
$this->setBinaryOptions($binaryOptions) |
35
|
|
|
->setValidatorChain($validatorChain); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function optimizeCommand(Image $image) : string |
39
|
|
|
{ |
40
|
|
|
return sprintf('%s %s %s', $this->getBinaryPath(), implode(' ', $this->getBinaryOptions()), $image->getPath()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* In the Abstract class we just run the validator stack |
45
|
|
|
* that has been configured, you can override ofcourse. |
46
|
|
|
* |
47
|
|
|
* @param string $imagePath |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function canHandle(string $imagePath) : bool |
52
|
|
|
{ |
53
|
|
|
return $this->getValidatorChain()->isValid($imagePath); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getBinaryOptions() : array |
60
|
|
|
{ |
61
|
|
|
return $this->binaryOptions; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $binaryOptions |
66
|
|
|
* |
67
|
|
|
* @return AbstractImageOptimiser |
68
|
|
|
*/ |
69
|
|
|
public function setBinaryOptions(array $binaryOptions) : AbstractImageOptimiser |
70
|
|
|
{ |
71
|
|
|
$this->binaryOptions = $binaryOptions; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getBinaryPath() : string |
80
|
|
|
{ |
81
|
|
|
return $this->binaryPath; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $binaryPath |
86
|
|
|
* |
87
|
|
|
* @return AbstractImageOptimiser |
88
|
|
|
*/ |
89
|
|
|
public function setBinaryPath(string $binaryPath) : AbstractImageOptimiser |
90
|
|
|
{ |
91
|
|
|
$this->binaryPath = $binaryPath; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return ValidatorChain |
98
|
|
|
*/ |
99
|
|
|
public function getValidatorChain() : ValidatorChain |
100
|
|
|
{ |
101
|
|
|
return $this->validatorChain; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param ValidatorChain $validatorChain |
106
|
|
|
* |
107
|
|
|
* @return AbstractImageOptimiser |
108
|
|
|
*/ |
109
|
|
|
public function setValidatorChain(ValidatorChain $validatorChain) : AbstractImageOptimiser |
110
|
|
|
{ |
111
|
|
|
$this->validatorChain = $validatorChain; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
} |