1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Push notification services abstraction (http://github.com/juliangut/tify) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/juliangut/tify for the canonical source repository |
6
|
|
|
* |
7
|
|
|
* @license https://github.com/juliangut/tify/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jgut\Tify; |
11
|
|
|
|
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
|
14
|
|
|
trait OptionsTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Doctrine\Common\Collections\ArrayCollection |
18
|
|
|
*/ |
19
|
|
|
protected $options = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Initialize options collection. |
23
|
|
|
*/ |
24
|
|
|
protected function initializeOptions() |
25
|
|
|
{ |
26
|
|
|
if (!$this->options instanceof ArrayCollection) { |
27
|
|
|
$this->options = new ArrayCollection; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get options. |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
public function getOptions() |
37
|
|
|
{ |
38
|
|
|
$this->initializeOptions(); |
39
|
|
|
|
40
|
|
|
return $this->options; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Has option. |
45
|
|
|
* |
46
|
|
|
* @param string $option |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function hasOption($option) |
51
|
|
|
{ |
52
|
|
|
$this->initializeOptions(); |
53
|
|
|
|
54
|
|
|
return $this->options->containsKey($option); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get option. |
59
|
|
|
* |
60
|
|
|
* @param string $option |
61
|
|
|
* @param mixed $default |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function getOption($option, $default = null) |
66
|
|
|
{ |
67
|
|
|
$this->initializeOptions(); |
68
|
|
|
|
69
|
|
|
return $this->options->containsKey($option) ? $this->options->get($option) : $default; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set options. |
74
|
|
|
* |
75
|
|
|
* @param array $options |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function setOptions($options) |
80
|
|
|
{ |
81
|
|
|
if (!$this->options instanceof ArrayCollection) { |
82
|
|
|
$this->options->clear(); |
83
|
|
|
} else { |
84
|
|
|
$this->initializeOptions(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
foreach ($options as $option => $value) { |
88
|
|
|
$this->options->set($option, $value); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set option. |
96
|
|
|
* |
97
|
|
|
* @param string $option |
98
|
|
|
* @param mixed $value |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function setOption($option, $value) |
103
|
|
|
{ |
104
|
|
|
$this->initializeParameters(); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$this->options->set($option, $value); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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.