1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Image\Effect; |
4
|
|
|
|
5
|
|
|
use \InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Full implementation, as a Trait, of the Layer Effect Interface. |
9
|
|
|
*/ |
10
|
|
|
trait LayerEffectTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var float $opacity |
14
|
|
|
*/ |
15
|
|
|
private $opacity = 1.0; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string $gravity |
19
|
|
|
*/ |
20
|
|
|
private $gravity = 'nw'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Horizontal adjustment, in pixels. |
24
|
|
|
* Negative values will move mask to the left, positive values to the right. |
25
|
|
|
* Depends on the gravity setting |
26
|
|
|
* @var integer $x |
27
|
|
|
*/ |
28
|
|
|
private $x = 0; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Vertical adjustment, in pixels. |
32
|
|
|
* Negative values will move mask to the top, positive values to the bottom. |
33
|
|
|
* Depends on the gravity setting |
34
|
|
|
* @var integer $y |
35
|
|
|
*/ |
36
|
|
|
private $y = 0; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param float $opacity The mask opacity. |
40
|
|
|
* @throws InvalidArgumentException If the mask opacity is not a numeric value or not between 0.0 and 1.0. |
41
|
|
|
* @return AbstractMaskEffect Chainable |
42
|
|
|
*/ |
43
|
|
|
public function setOpacity($opacity) |
44
|
|
|
{ |
45
|
|
|
if (!is_numeric($opacity) || ($opacity < 0) || ( $opacity > 1)) { |
46
|
|
|
throw new InvalidArgumentException( |
47
|
|
|
'Opacity must be a float between 0.0 and 1.0' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
$this->opacity = (float)$opacity; |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return float |
56
|
|
|
*/ |
57
|
|
|
public function opacity() |
58
|
|
|
{ |
59
|
|
|
return $this->opacity; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $gravity The mask gravity. |
64
|
|
|
* @throws InvalidArgumentException If the argument is not a valid gravity name. |
65
|
|
|
* @return AbstractMaskEffect Chainable |
66
|
|
|
*/ |
67
|
|
|
public function setGravity($gravity) |
68
|
|
|
{ |
69
|
|
|
if (!in_array($gravity, $this->image()->availableGravities())) { |
|
|
|
|
70
|
|
|
throw new InvalidArgumentException( |
71
|
|
|
'Gravity is not valid' |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
$this->gravity = $gravity; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function gravity() |
82
|
|
|
{ |
83
|
|
|
return $this->gravity; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param integer $x The mask X position. |
88
|
|
|
* @throws InvalidArgumentException If the position is not a numeric value. |
89
|
|
|
* @return AbstractMaskEffect Chainable |
90
|
|
|
*/ |
91
|
|
|
public function setX($x) |
92
|
|
|
{ |
93
|
|
|
if (!is_numeric($x)) { |
94
|
|
|
throw new InvalidArgumentException( |
95
|
|
|
'X must be a an integer (in pixel)' |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
$this->x = (int)$x; |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return float |
104
|
|
|
*/ |
105
|
|
|
public function x() |
106
|
|
|
{ |
107
|
|
|
return $this->x; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param integer $y The Y position. |
112
|
|
|
* @throws InvalidArgumentException If the position is not a numeric value. |
113
|
|
|
* @return AbstractMaskEffect Chainable |
114
|
|
|
*/ |
115
|
|
|
public function setY($y) |
116
|
|
|
{ |
117
|
|
|
if (!is_numeric($y)) { |
118
|
|
|
throw new InvalidArgumentException( |
119
|
|
|
'Y must be a an integer (in pixel)' |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
$this->y = (int)$y; |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return float |
128
|
|
|
*/ |
129
|
|
|
public function y() |
130
|
|
|
{ |
131
|
|
|
return $this->y; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.