* $page = Bedrock::get_setting('PageTypes.SomePage');
* $fields = $page->getFields();
* echo $fields->path();
* // returns "PageTypes.SomePage.Fields"
*
*/
protected $path = null;
/**
* @var string The iterator class to use when looping through this object
*/
protected $iteratorClass = "BedrockSetting_Iterator";
/**
* A wildcard method for trapping "get" functions
*
* @param string $method The method called
* @param array $args The arguments provided to the method.
*/
public function __call($method, $args) {
if(substr($method,0,3) == "get") {
return $this->get(substr($method,3));
}
return parent::__call($method,$args);
}
/**
* Builds a new BedrockSetting object
*
* @param string $key The array key that was used to get the source
* @param array $source The settings contained in this object
* @param string $path The path, in dot syntax, that can be used to get this setting
* with {@link Bedrock::get_setting()}
*/
public function __construct($key, $source = null, $path = null) {
$this->key = BedrockUtil::proper_form($key);
$this->source = $source;
$this->path = $path;
}
/**
* Gets a member of the {@link $source} array. If $val doesn't exist, it looks in
* _defaults.yml using {@link Bedrock::get_default()}. If $val returns an array,
* it returns a new BedrockSetting object with that source. This is useful for
* daisy-chaining to traverse the configuration.
* Example:
*
* $settings = Bedrock::get_setting('PageTypes.SomePage');
* echo $settings->getRSS()->getSubject();
*
*
* If $val returns a string, return the string.
*
* @param string $val The array key to check in {@link $source}
* @return mixed
*/
public function get($val) {
if(is_array($this->source) && isset($this->source[$val])) {
$v = $this->source[$val];
return is_array($v) ? BedrockSetting::create($val, $v, $this->path.".".$val) : $v;
}
return Bedrock::get_default($val);
}
/**
* For some settings, it's useful to return an array instead of a BedrockSetting object.
* Ex. $pageSettings->getAllowedChildren()->toArray();
*
* @return array
*/
public function toArray() {
return is_array($this->source) ? $this->source : array($this->source);
}
/**
* Returns the path to find this setting in dot syntax
*
* @return string
*/
public function getPath() {
return $this->path;
}
public function getKey() {
return $this->key;
}
/**
* Necessary for allowing BedrockSetting objects to be traversed in a loop
*
* @return BedrockSetting_Iterator
*/
public function getIterator() {
$class = $this->iteratorClass;
return new $class($this->key, $this->source, $this->path);
}
/**
* Merge two BedrockSetting objects into one.
*
* @param BedrockSetting $b The BedrockSetting to merge into this one.
*/
public function merge(BedrockSetting $b) {
foreach($b->toArray() as $k => $v)
$this->source[$k] = $v;
}
}
/**
* This class informs PHP how to deal with iterating over a BedrockSetting object
* as an array.
*
* Example:
*
* // getFields() does not return an array, but it can still be iterated like one.
* foreach($page->getFields() as $fieldName => $fieldSettings) {
* echo $fieldName;
* echo $fieldSettings->getDBField();
* echo $fieldSettings->getLabel();
* }
*
*
* @package Bedrock
*/
class BedrockSetting_Iterator implements Iterator
{
/**
* @var array The key of the array that generated this setting.
*/
protected $key;
/**
* @var array The source of the {@link BedrockSetting} object being iterated.
*/
protected $source;
/**
* @var array The path of the {@link BedrockSetting} object being iterated.
*/
protected $path;
/**
* @var string The iterator class to use when looping through this object
*/
protected $iteratorSettingClass = "BedrockSetting";
/**
* Constructs a new BedrockSetting_Iterator using the same path and source
* as the {@link BedrockSetting} object it will return
*
* @param array $items The source of the BedrockSetting object
* @param string $path The path, in dot syntax to the BedrockSetting object
*/
public function __construct($key, $items, $path) {
$this->key = BedrockUtil::proper_form($key);
$this->source = $items;
$this->path = $path;
}
protected function createSetting($key, $source = null, $path = null) {
$class = BedrockSetting::get_setting_class($key);
if(!$class) {
$class = $this->iteratorSettingClass;
}
return new $class($key, current($this->source), "{$this->path}.{$key}");
}
/**
* Return the current object of the iterator.
*
* @return boolean|BedrockSetting
*/
public function current() {
$key = key($this->source);
return is_array($this->source) ? $this->createSetting($key, current($this->source), "{$this->path}.{$key}") : false;
}
/**
* Return the key of the current object of the iterator.
*
* @return mixed
*/
public function key() {
return key($this->source);
}
/**
* Return the next item in this set.
*
* @return boolean|BedrockSetting
*/
public function next() {
$key = key($this->source);
return is_array($this->source) ? $this->createSetting($key, next($this->source), "{$this->path}.{$key}") : false;
}
/**
* Rewind the iterator to the beginning of the set.
*
* @return boolean|BedrockSetting The first item in the set.
*/
public function rewind() {
$key = key($this->source);
return is_array($this->source) ? $this->createSetting($key, reset($this->source), "{$this->path}.{$key}") : false;
}
/**
* Check the iterator is pointing to a valid item in the set.
*
* @return boolean
*/
public function valid() {
return is_array($this->source) && current($this->source) !== false;
}
}