1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stevenmaguire\Yelp\Tool; |
4
|
|
|
|
5
|
|
|
trait ConfigurationTrait |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Retrives the value of a given property from the client. |
9
|
|
|
* |
10
|
|
|
* @param string $property |
11
|
|
|
* |
12
|
|
|
* @return mixed|null |
13
|
|
|
*/ |
14
|
4 |
|
public function __get($property) |
15
|
|
|
{ |
16
|
4 |
|
if (property_exists($this, $property)) { |
17
|
4 |
|
return $this->$property; |
18
|
|
|
} |
19
|
|
|
|
20
|
4 |
|
return null; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Maps legacy configuration keys to updated keys. |
25
|
|
|
* |
26
|
|
|
* @param array $configuration |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
protected function mapConfiguration(array $configuration) |
31
|
|
|
{ |
32
|
42 |
|
array_walk($configuration, function ($value, $key) use (&$configuration) { |
33
|
42 |
|
$newKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key)))); |
34
|
42 |
|
$configuration[$newKey] = $value; |
35
|
42 |
|
}); |
36
|
|
|
|
37
|
42 |
|
return $configuration; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Parse configuration using defaults |
42
|
|
|
* |
43
|
|
|
* @param array $configuration |
44
|
|
|
* @param array $defaults |
45
|
|
|
* |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
42 |
|
protected function parseConfiguration($configuration = [], $defaults = []) |
49
|
|
|
{ |
50
|
42 |
|
$configuration = array_merge($defaults, $this->mapConfiguration($configuration)); |
51
|
|
|
|
52
|
42 |
|
array_walk($configuration, [$this, 'setConfig']); |
53
|
|
|
|
54
|
42 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Attempts to set a given value. |
59
|
|
|
* |
60
|
|
|
* @param mixed $value |
61
|
|
|
* @param string $key |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
42 |
|
protected function setConfig($value, $key) |
66
|
|
|
{ |
67
|
42 |
|
$setter = 'set' . ucfirst($key); |
68
|
|
|
|
69
|
42 |
|
if (method_exists($this, $setter)) { |
70
|
2 |
|
$this->$setter($value); |
71
|
42 |
|
} elseif (property_exists($this, $key)) { |
72
|
42 |
|
$this->$key = $value; |
73
|
21 |
|
} |
74
|
|
|
|
75
|
42 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|