100% of 358OPs |
100% of 57Lines |
86% of 50Branches |
73% of 33Paths |
# | |
---|---|
1 |
<?php |
2 |
|
3 |
namespace mageekguy\atoum\test\assertion; |
4 |
|
5 |
use mageekguy\atoum\asserter; |
6 |
|
7 |
class aliaser implements \arrayAccess |
8 |
{ |
9 |
protected $resolver = null; |
10 |
protected $aliases = array(); |
11 |
|
12 |
private $context = null; |
13 |
private $keyword = null; |
14 |
|
15 |
public function __construct(asserter\resolver $resolver = null)100% |
16 |
{ |
17 |
$this->setResolver($resolver); |
18 |
} |
19 |
|
20 |
public function __set($alias, $keyword)100% |
21 |
{ |
22 |
return $this->aliasKeyword($keyword, $alias); |
23 |
} |
24 |
|
25 |
public function __get($alias)100% |
26 |
{ |
27 |
return $this->resolveAlias($alias); |
28 |
} |
29 |
|
30 |
public function __unset($alias)100% |
31 |
{ |
32 |
$contextKey = $this->getContextKey($this->context); |
33 |
|
34 |
if (isset($this->aliases[$contextKey]) === true) |
35 |
{ |
36 |
$aliasKey = $this->getAliasKey($alias); |
37 |
|
38 |
if (isset($this->aliases[$contextKey][$aliasKey]) === true) |
39 |
{ |
40 |
unset($this->aliases[$contextKey][$aliasKey]); |
41 |
} |
42 |
} |
43 |
} |
44 |
|
45 |
public function __isset($alias)100% |
46 |
{ |
47 |
$contextKey = $this->getContextKey($this->context); |
48 |
|
49 |
if (isset($this->aliases[$contextKey]) === true) |
50 |
{ |
51 |
$aliasKey = $this->getAliasKey($alias); |
52 |
|
53 |
return (isset($this->aliases[$contextKey][$aliasKey]) === true); |
54 |
} |
55 |
} |
56 |
|
57 |
public function offsetGet($context)100% |
58 |
{ |
59 |
$this->context = $context; |
60 |
|
61 |
return $this; |
62 |
} |
63 |
|
64 |
public function offsetSet($newContext, $context)100% |
65 |
{ |
66 |
$contextKey = $this->getContextKey($context); |
67 |
|
68 |
if (isset($this->aliases[$contextKey]) === true) |
69 |
{ |
70 |
$this->aliases[$this->getContextKey($newContext)] = $this->aliases[$contextKey]; |
71 |
} |
72 |
|
73 |
return $this; |
74 |
} |
75 |
|
76 |
public function offsetUnset($context)100% |
77 |
{ |
78 |
$contextKey = $this->getContextKey($context); |
79 |
|
80 |
if (isset($this->aliases[$contextKey]) === true) |
81 |
{ |
82 |
unset($this->aliases[$contextKey]); |
83 |
} |
84 |
|
85 |
return $this; |
86 |
} |
87 |
|
88 |
public function offsetExists($context)100% |
89 |
{ |
90 |
return (isset($this->aliases[$this->getContextKey($context)]) === true); |
91 |
} |
92 |
|
93 |
public function setResolver(asserter\resolver $resolver = null)100% |
94 |
{ |
95 |
$this->resolver = $resolver ?: new asserter\resolver(); |
96 |
|
97 |
return $this; |
98 |
} |
99 |
|
100 |
public function getResolver()100% |
101 |
{ |
102 |
return $this->resolver; |
103 |
} |
104 |
|
105 |
public function from($context)100% |
106 |
{ |
107 |
$this->context = $context; |
108 |
|
109 |
return $this; |
110 |
} |
111 |
|
112 |
public function alias($keyword)100% |
113 |
{ |
114 |
$this->keyword = $keyword; |
115 |
|
116 |
return $this; |
117 |
} |
118 |
|
119 |
public function to($alias)100% |
120 |
{ |
121 |
$this->aliasKeyword($this->keyword, $alias, $this->context); |
122 |
|
123 |
$this->keyword = null; |
124 |
|
125 |
return $this; |
126 |
} |
127 |
|
128 |
public function aliasKeyword($keyword, $alias, $context = null)100% |
129 |
{ |
130 |
$this->aliases[$this->getContextKey($context)][$this->getAliasKey($alias)] = $keyword; |
131 |
|
132 |
return $this; |
133 |
} |
134 |
|
135 |
public function resolveAlias($alias, $context = null)100% |
136 |
{ |
137 |
$aliasKey = $this->getAliasKey($alias); |
138 |
$contextKey = $this->getContextKey($context); |
139 |
|
140 |
return (isset($this->aliases[$contextKey]) === false || isset($this->aliases[$contextKey][$aliasKey]) === false ? $alias : $this->aliases[$contextKey][$aliasKey]); |
141 |
} |
142 |
|
143 |
private function getAliasKey($alias)100% |
144 |
{ |
145 |
return strtolower($alias); |
146 |
} |
147 |
|
148 |
private function getContextKey($context)100% |
149 |
{ |
150 |
if ($context === null && $this->context !== null) |
151 |
{ |
152 |
$context = $this->context; |
153 |
|
154 |
$this->context = null; |
155 |
} |
156 |
|
157 |
return ($context == '' ? '' : strtolower($this->resolver->resolve($context) ?: $context)); |
158 |
} |
159 |
} |