1213dcf07998c19150c49f8a57e62c5de9169577
[yaffs-website] / vendor / dflydev / dot-access-configuration / src / Dflydev / DotAccessConfiguration / AbstractConfiguration.php
1 <?php
2
3 /*
4  * This file is a part of dflydev/dot-access-configuration.
5  *
6  * (c) Dragonfly Development Inc.
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Dflydev\DotAccessConfiguration;
13
14 use Dflydev\DotAccessData\Data;
15 use Dflydev\PlaceholderResolver\PlaceholderResolverInterface;
16 use Dflydev\PlaceholderResolver\RegexPlaceholderResolver;
17
18 abstract class AbstractConfiguration implements ConfigurationInterface
19 {
20     private $placeholderResolver;
21     private $data;
22     private $exportIsDirty = true;
23     private $resolvedExport;
24
25     /**
26      * {@inheritdocs}
27      */
28     public function getRaw($key)
29     {
30         return $this->data()->get($key);
31     }
32
33     /**
34      * {@inheritdocs}
35      */
36     public function get($key)
37     {
38         $value = $this->getRaw($key);
39         if (is_object($value)) {
40             return $value;
41         }
42         $this->resolveValues($value);
43
44         return $value;
45     }
46
47     /**
48      * {@inheritdocs}
49      */
50     public function set($key, $value = null)
51     {
52         $this->exportIsDirty = true;
53
54         return $this->data()->set($key, $value);
55     }
56
57     /**
58      * {@inheritdocs}
59      */
60     public function append($key, $value = null)
61     {
62         $this->exportIsDirty = true;
63
64         return $this->data()->append($key, $value);
65     }
66
67     /**
68      * {@inheritdocs}
69      */
70     public function exportRaw()
71     {
72         return $this->data()->export();
73     }
74
75     /**
76      * {@inheritdocs}
77      */
78     public function export()
79     {
80         if ($this->exportIsDirty) {
81             $this->resolvedExport = $this->data()->export();
82             $this->resolveValues($this->resolvedExport);
83             $this->exportIsDirty = false;
84         }
85
86         return $this->resolvedExport;
87     }
88
89     /**
90      * {@inheritdocs}
91      */
92     public function exportData()
93     {
94         return new Data($this->export());
95     }
96
97     /**
98      * {@inheritdocs}
99      */
100     public function importRaw($imported = null, $clobber = true)
101     {
102         $this->exportIsDirty = true;
103
104         if (null !== $imported) {
105             $this->data()->import($imported, $clobber);
106         }
107     }
108
109     /**
110      * {@inheritdocs}
111      */
112     public function import(ConfigurationInterface $imported, $clobber = true)
113     {
114         return $this->importRaw($imported->exportRaw(), $clobber);
115     }
116
117     /**
118      * {@inheritdocs}
119      */
120     public function resolve($value = null)
121     {
122         if (null === $value) {
123             return null;
124         }
125
126         return $this->placeholderResolver()->resolvePlaceholder($value);
127     }
128
129     /**
130      * {@inheritdocs}
131      */
132     public function setPlaceholderResolver(PlaceholderResolverInterface $placeholderResolver)
133     {
134         $this->placeholderResolver = $placeholderResolver;
135
136         return $this;
137     }
138
139     /**
140      * Resolve values
141      *
142      * For objects, do nothing. For strings, resolve placeholder.
143      * For arrays, call resolveValues() on each item.
144      *
145      * @param mixed $input
146      */
147     protected function resolveValues(&$input = null)
148     {
149         if (is_array($input)) {
150             foreach ($input as $idx => $value) {
151                 $this->resolveValues($value);
152                 $input[$idx] = $value;
153             }
154         } else {
155             if (!is_object($input)) {
156                 $input = $this->placeholderResolver()->resolvePlaceholder($input);
157             }
158         }
159     }
160
161     /**
162      * Data
163      *
164      * @return Data
165      */
166     protected function data()
167     {
168         if (null === $this->data) {
169             $this->data = new Data;
170         }
171
172         return $this->data;
173     }
174
175     /**
176      * Placeholder Resolver
177      *
178      * @return PlaceholderResolverInterface
179      */
180     protected function placeholderResolver()
181     {
182         if (null === $this->placeholderResolver) {
183             $this->placeholderResolver = new RegexPlaceholderResolver(new ConfigurationDataSource($this), '%', '%');
184         }
185
186         return $this->placeholderResolver;
187     }
188 }