1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Config |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Config; |
16
|
|
|
|
17
|
|
|
use Phossa2\Config\Message\Message; |
18
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
19
|
|
|
use Phossa2\Config\Traits\ArrayAccessTrait; |
20
|
|
|
use Phossa2\Shared\Reference\DelegatorTrait; |
21
|
|
|
use Phossa2\Config\Exception\LogicException; |
22
|
|
|
use Phossa2\Config\Interfaces\ConfigInterface; |
23
|
|
|
use Phossa2\Config\Interfaces\WritableInterface; |
24
|
|
|
use Phossa2\Config\Interfaces\DelegatorInterface; |
25
|
|
|
use Phossa2\Config\Traits\DelegatorWritableTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Delegator |
29
|
|
|
* |
30
|
|
|
* Implmentation of DelegatorInterface |
31
|
|
|
* |
32
|
|
|
* @package Phossa2\Config |
33
|
|
|
* @author Hong Zhang <[email protected]> |
34
|
|
|
* @see ObjectAbstract |
35
|
|
|
* @see DelegatorInterface |
36
|
|
|
* @see ConfigInterface |
37
|
|
|
* @see \ArrayAccess |
38
|
|
|
* @see WritableInterface |
39
|
|
|
* @version 2.0.0 |
40
|
|
|
* @since 2.0.0 added |
41
|
|
|
*/ |
42
|
|
|
class Delegator extends ObjectAbstract implements DelegatorInterface, \ArrayAccess |
43
|
|
|
{ |
44
|
|
|
use ArrayAccessTrait, DelegatorTrait, DelegatorWritableTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function get(/*# string */ $id, $default = null) |
50
|
|
|
{ |
51
|
|
|
if ($this->hasInLookup($id)) { |
52
|
|
|
return $this->getFromLookup($id); |
53
|
|
|
} |
54
|
|
|
return $default; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function has(/*# string */ $id)/*# : bool */ |
61
|
|
|
{ |
62
|
|
|
return $this->hasInLookup($id); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
|
|
public function addConfig(ConfigInterface $config) |
69
|
|
|
{ |
70
|
|
|
return $this->addRegistry($config); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
|
|
public function set(/*# string */ $id, $value) |
77
|
|
|
{ |
78
|
|
|
if ($this->isWritable()) { |
79
|
|
|
$this->writable->set($id, $value); |
80
|
|
|
return $this; |
81
|
|
|
} else { |
82
|
|
|
throw new LogicException( |
83
|
|
|
Message::get(Message::CONFIG_NOT_WRITABLE), |
84
|
|
|
Message::CONFIG_NOT_WRITABLE |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
|
|
protected function isValidRegistry($registry)/*# : bool */ |
93
|
|
|
{ |
94
|
|
|
return $registry instanceof ConfigInterface; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
*/ |
100
|
|
|
protected function hasInRegistry( |
101
|
|
|
$registry, |
102
|
|
|
/*# string */ $id |
103
|
|
|
)/*# : bool */ { |
104
|
|
|
/* @var $registry ConfigInterface */ |
105
|
|
|
return $registry->has($id); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
|
|
protected function getFromRegistry( |
112
|
|
|
$registry, |
113
|
|
|
/*# string */ $id |
114
|
|
|
) { |
115
|
|
|
/* @var $registry ConfigInterface */ |
116
|
|
|
return $registry->get($id); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|