34 public static function fromConfig(array $config = array(), array $defaults = array(), array $required = array())
36 $data = $config + $defaults;
38 if ($missing = array_diff($required, array_keys(
$data))) {
42 return new self(
$data);
45 public function count()
47 return count($this->data);
52 return new \ArrayIterator($this->data);
65 public function clear()
67 $this->data = array();
79 public function getAll(array $keys =
null)
81 return $keys ? array_intersect_key($this->data, array_flip($keys)) :
$this->data;
91 public function get($key)
93 return isset($this->data[$key]) ? $this->data[$key] :
null;
104 public function set($key, $value)
106 $this->data[$key] = $value;
120 public function add($key, $value)
122 if (!array_key_exists($key, $this->data)) {
123 $this->data[$key] = $value;
124 } elseif (is_array($this->data[$key])) {
125 $this->data[$key][] = $value;
127 $this->data[$key] = array($this->data[$key], $value);
140 public function remove($key)
142 unset($this->data[$key]);
154 return array_keys($this->data);
164 public function hasKey($key)
166 return array_key_exists($key, $this->data);
178 foreach (array_keys($this->data) as $k) {
179 if (!strcasecmp($k, $key)) {
196 return array_search($value, $this->data);
222 foreach (
$data as $key => $value) {
238 if (is_array(
$data)) {
243 foreach (
$data as $key => $value) {
244 $this->data[$key] = $value;
262 public function map(\Closure $closure, array $context = array(), $static =
true)
264 $collection = $static ?
new static() :
new self();
265 foreach ($this as $key => $value) {
266 $collection->add($key, $closure($key, $value, $context));
282 public function filter(\Closure $closure, $static =
true)
284 $collection = ($static) ?
new static() :
new self();
285 foreach ($this->data as $key => $value) {
286 if ($closure($key, $value)) {
287 $collection->add($key, $value);
296 return isset($this->data[$offset]);
301 return isset($this->data[$offset]) ? $this->data[$offset] :
null;
304 public function offsetSet($offset, $value)
306 $this->data[$offset] = $value;
311 unset($this->data[$offset]);
323 public function setPath($path, $value)
326 $queue = explode(
'/', $path);
327 while (
null !== ($key = array_shift($queue))) {
328 if (!is_array($current)) {
329 throw new RuntimeException(
"Trying to setPath {$path}, but {$key} is set and is not an array");
331 $current[$key] = $value;
332 } elseif (isset($current[$key])) {
333 $current =& $current[$key];
335 $current[$key] = array();
336 $current =& $current[$key];
354 public function getPath($path, $separator =
'/',
$data =
null)
356 if (
$data ===
null) {
360 $path = is_array($path) ? $path : explode($separator, $path);
361 while (
null !== ($part = array_shift($path))) {
362 if (!is_array(
$data)) {
364 } elseif (isset(
$data[$part])) {
366 } elseif ($part !=
'*') {
371 foreach (
$data as $value) {
373 $result = array_merge_recursive($result, (array) $value);
374 } elseif (
null !== ($test = $this->
getPath($path, $separator, $value))) {
375 $result = array_merge_recursive($result, (array) $test);
393 public function inject($input)
397 foreach ($this->data as $key => $val) {
398 $replace[
'{' . $key .
'}'] = $val;
401 return strtr($input, $replace);