1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kadet\Xmpp\Utils; |
4
|
|
|
|
5
|
|
|
use Kadet\Xmpp\Exception\ReadOnlyException; |
6
|
|
|
use Kadet\Xmpp\Exception\WriteOnlyException; |
7
|
|
|
|
8
|
|
|
trait Accessors |
9
|
|
|
{ |
10
|
|
|
private $_magic = []; |
11
|
|
|
|
12
|
26 |
|
public function __get($property) |
13
|
|
|
{ |
14
|
26 |
|
$getter = 'get' . ucfirst($property); |
15
|
|
|
|
16
|
26 |
|
if (method_exists($this, $getter)) { |
17
|
26 |
|
return $this->$getter(); |
18
|
|
|
} elseif (method_exists($this, 'set' . ucfirst($property))) { |
19
|
|
|
throw new WriteOnlyException("Property \$$property is write-only, which is rather strange."); |
20
|
|
|
} else { |
21
|
|
|
return $this->_get($property); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
12 |
|
public function __set($property, $value) |
26
|
|
|
{ |
27
|
12 |
|
$setter = 'set' . ucfirst($property); |
28
|
|
|
|
29
|
12 |
|
if (method_exists($this, $setter)) { |
30
|
12 |
|
$this->$setter($value); |
31
|
|
|
} elseif (method_exists($this, 'get' . ucfirst($property))) { |
32
|
|
|
throw new ReadOnlyException("Property \$$property is read-only."); |
33
|
|
|
} else { |
34
|
|
|
$this->_magic[$property] = $value; |
35
|
|
|
} |
36
|
12 |
|
} |
37
|
|
|
|
38
|
|
|
public function __isset($property) |
39
|
|
|
{ |
40
|
|
|
return $this->$property !== null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function __unset($property) |
44
|
|
|
{ |
45
|
|
|
$this->$property = null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function _get($property) |
49
|
|
|
{ |
50
|
|
|
return isset($this->_magic[$property]) ? $this->_magic[$property] : null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function _set($property, $value) |
54
|
|
|
{ |
55
|
|
|
$this->_magic[$property] = $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function applyOptions(array $options) |
59
|
|
|
{ |
60
|
|
|
foreach ($options as $key => $value) { |
61
|
|
|
$this->$key = $value; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|