WildcardData.php in Crumbs, the Breadcrumbs suite 7.2
File
lib/Container/WildcardData.php
View source
<?php
class crumbs_Container_WildcardData {
protected $data;
protected $fallback;
function __construct(array $data = array()) {
$this->data = $data;
$this->fallback = isset($this->data['*']) ? $this->data['*'] : NULL;
}
function getAll($key) {
$fragments = explode('.', $key);
$partial_key = array_shift($fragments);
$values = array();
while (!empty($fragments)) {
$wildcard_key = $partial_key . '.*';
if (isset($this->data[$wildcard_key])) {
$values[$wildcard_key] = $this->data[$wildcard_key];
}
$partial_key .= '.' . array_shift($fragments);
}
if (isset($this->data[$key])) {
$values[$key] = $this->data[$key];
}
return array_reverse($values);
}
function getAllMerged($key) {
$merged = array();
foreach ($this
->getAll($key) as $values) {
if (is_array($values)) {
$merged = array_merge($merged, $values);
}
}
return $merged;
}
function valueAtKey($key) {
if (isset($this->data[$key])) {
return $this->data[$key];
}
return $this
->wildcardValue($key);
}
protected function buildPrefixedData($prefix) {
$data = array();
$k = strlen($prefix);
$data[''] = $data['*'] = $this
->wildcardValue($prefix);
if (isset($this->data[$prefix])) {
$data[''] = $this->data[$prefix];
}
if (isset($this->data[$prefix . '.*'])) {
$data['*'] = $this->data[$prefix . '.*'];
}
foreach ($this->data as $key => $value) {
if (strlen($key) > $k && substr($key, 0, $k + 1) === $prefix . '.') {
$data[substr($key, $k + 1)] = $value;
}
}
return $data;
}
protected function wildcardValue($key) {
$fragments = explode('.', $key);
$partial_key = array_shift($fragments);
$value = $this->fallback;
while (!empty($fragments)) {
if (isset($this->data[$partial_key . '.*'])) {
$value = $this->data[$partial_key . '.*'];
}
$partial_key .= '.' . array_shift($fragments);
}
return $value;
}
}