123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- class PropelConfiguration implements ArrayAccess
- {
- const TYPE_ARRAY = 1;
- const TYPE_ARRAY_FLAT = 2;
- const TYPE_OBJECT = 3;
-
- protected $parameters = array();
-
- public function __construct(array $parameters = array())
- {
- $this->parameters = $parameters;
- }
-
- public function offsetExists($offset)
- {
- return array_key_exists($offset, $this->parameters);
- }
-
- public function offsetSet($offset, $value)
- {
- $this->parameter[$offset] = $value;
- }
-
- public function offsetGet($offset)
- {
- return $this->parameters[$offset];
- }
-
- public function offsetUnset($offset)
- {
- unset($this->parameters[$offset]);
- }
-
- public function getParameter($name, $default = null)
- {
- $ret = $this->parameters;
- $parts = explode('.', $name);
- while ($part = array_shift($parts)) {
- if (isset($ret[$part])) {
- $ret = $ret[$part];
- } else {
- return $default;
- }
- }
- return $ret;
- }
-
- public function setParameter($name, $value)
- {
- $param = &$this->parameters;
- $parts = explode('.', $name);
- while ($part = array_shift($parts)) {
- $param = &$param[$part];
- }
- $param = $value;
- }
-
- public function getParameters($type = PropelConfiguration::TYPE_ARRAY)
- {
- switch ($type) {
- case PropelConfiguration::TYPE_ARRAY:
- return $this->parameters;
- case PropelConfiguration::TYPE_ARRAY_FLAT:
- return $this->toFlatArray();
- case PropelConfiguration::TYPE_OBJECT:
- return $this;
- default:
- throw new PropelException('Unknown configuration type: '. var_export($type, true));
- }
- }
-
- protected function toFlatArray()
- {
- $result = array();
- $it = new PropelConfigurationIterator(new RecursiveArrayIterator($this->parameters), RecursiveIteratorIterator::SELF_FIRST);
- foreach($it as $key => $value) {
- $ns = $it->getDepth() ? $it->getNamespace() . '.'. $key : $key;
- if ($it->getNodeType() == PropelConfigurationIterator::NODE_ITEM) {
- $result[$ns] = $value;
- }
- }
- return $result;
- }
- }
- ?>
|