1
|
|
|
<?php |
2
|
|
|
namespace Michaels\Manager\Traits; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Access Deeply nested manager items through magic methods |
6
|
|
|
* |
7
|
|
|
* MUST be used with ManagesItemsTrait |
8
|
|
|
* |
9
|
|
|
* @implements Michaels\Manager\Contracts\ChainsNestedItemsInterface |
10
|
|
|
* @package Michaels\Manager |
11
|
|
|
*/ |
12
|
|
|
trait ChainsNestedItemsTrait |
13
|
|
|
{ |
14
|
|
|
use DependsOnManagesItemsTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Current level of nesting |
18
|
|
|
* @var bool|string |
19
|
|
|
*/ |
20
|
|
|
protected $currentLevel = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Deletes item at the current level of nesting (and below) |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
public function drop() |
27
|
|
|
{ |
28
|
|
|
$this->remove($this->currentLevel); |
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets the current level of nesting. |
34
|
|
|
* |
35
|
|
|
* @see Michaels\Manager\Contracts\ChainsNestedItemsInterface |
36
|
|
|
* @param string $name Next level in dot notation to set |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function __get($name) |
40
|
|
|
{ |
41
|
|
|
$prefix = $this->buildPrefix(); |
42
|
|
|
|
43
|
|
|
return $this->get($prefix . $name); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Retrieves a value from the manager at the current nest level. |
48
|
|
|
* |
49
|
|
|
* @see Michaels\Manager\Contracts\ChainsNestedItemsInterface |
50
|
|
|
* @param string $name The alias to be retrieved |
51
|
|
|
* @param array $arguments Not used at present |
52
|
|
|
* @throws \Michaels\Manager\Exceptions\ItemNotFoundException |
53
|
|
|
* @return mixed item value |
54
|
|
|
*/ |
55
|
|
|
public function __call($name, $arguments) |
56
|
|
|
{ |
57
|
|
|
return $this->addToChain($name); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets an item at the current nest level. |
62
|
|
|
* |
63
|
|
|
* @see Michaels\Manager\Contracts\ChainsNestedItemsInterface |
64
|
|
|
* @param string $key The alias to be retrieved |
65
|
|
|
* @param mixed $value Value to be set |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function __set($key, $value) |
69
|
|
|
{ |
70
|
|
|
$prefix = $this->buildPrefix(); |
71
|
|
|
|
72
|
|
|
return $this->add($prefix . $key, $value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Creates a prefix |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function buildPrefix() |
80
|
|
|
{ |
81
|
|
|
|
82
|
|
|
if ($this->currentLevel === false) { |
83
|
|
|
return ""; |
84
|
|
|
} else { |
85
|
|
|
$prefix = $this->currentLevel; |
86
|
|
|
$this->currentLevel = false; |
87
|
|
|
|
88
|
|
|
return $prefix . "."; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Adds to the chain |
94
|
|
|
* @param $name |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
protected function addToChain($name) |
98
|
|
|
{ |
99
|
|
|
$dot = ($this->currentLevel === false) ? '' : '.'; |
100
|
|
|
$this->currentLevel .= $dot . $name; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|