|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Nucleus - XMPP Library for PHP |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2016, Some rights reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* Contact with author: |
|
10
|
|
|
* Xmpp: [email protected] |
|
11
|
|
|
* E-mail: [email protected] |
|
12
|
|
|
* |
|
13
|
|
|
* From Kadet with love. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Kadet\Xmpp\Utils; |
|
17
|
|
|
|
|
18
|
|
|
use Kadet\Xmpp\Exception\ReadOnlyException; |
|
19
|
|
|
use Kadet\Xmpp\Exception\WriteOnlyException; |
|
20
|
|
|
|
|
21
|
|
|
trait Accessors |
|
22
|
|
|
{ |
|
23
|
|
|
private $_magic = []; |
|
24
|
|
|
|
|
25
|
14 |
|
public function __get($property) |
|
26
|
|
|
{ |
|
27
|
14 |
|
$getter = 'get' . ucfirst($property); |
|
28
|
|
|
|
|
29
|
14 |
|
if (method_exists($this, $getter)) { |
|
30
|
14 |
|
return $this->$getter(); |
|
31
|
|
|
} elseif (method_exists($this, 'set' . ucfirst($property))) { |
|
32
|
|
|
throw new WriteOnlyException("Property \$$property is write-only, which is rather strange. Maybe you should write custom getter with proper explanation?"); |
|
33
|
|
|
} else { |
|
34
|
|
|
return $this->_get($property); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
15 |
|
public function __set($property, $value) |
|
39
|
|
|
{ |
|
40
|
15 |
|
$setter = 'set' . ucfirst($property); |
|
41
|
|
|
|
|
42
|
15 |
|
if (method_exists($this, $setter)) { |
|
43
|
15 |
|
$this->$setter($value); |
|
44
|
8 |
|
} elseif (method_exists($this, 'get' . ucfirst($property))) { |
|
45
|
|
|
throw new ReadOnlyException("Property \$$property is read-only."); |
|
46
|
|
|
} else { |
|
47
|
8 |
|
$this->_magic[$property] = $value; |
|
48
|
|
|
} |
|
49
|
15 |
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function __isset($property) |
|
52
|
|
|
{ |
|
53
|
1 |
|
return $this->$property !== null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function __unset($property) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->$property = null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function _get($property) |
|
62
|
|
|
{ |
|
63
|
|
|
return isset($this->_magic[$property]) ? $this->_magic[$property] : null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function _set($property, $value) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->_magic[$property] = $value; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
14 |
|
public function applyOptions(array $options) |
|
72
|
|
|
{ |
|
73
|
14 |
|
foreach ($options as $key => $value) { |
|
74
|
13 |
|
$this->$key = $value; |
|
75
|
|
|
} |
|
76
|
14 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|