diff --git a/src/Patterns/Structural/Composite/AbstractComposite.php b/src/Patterns/Structural/Composite/AbstractComposite.php index f6758c4..98b85cb 100644 --- a/src/Patterns/Structural/Composite/AbstractComposite.php +++ b/src/Patterns/Structural/Composite/AbstractComposite.php @@ -13,11 +13,25 @@ namespace O2System\Spl\Patterns\Structural\Composite; +use O2System\Spl\Traits\Collectors\OptionsCollectorTrait; + /** * Class AbstractBuilder * @package O2System\Spl\Patterns\Builder */ abstract class AbstractComposite implements RenderableInterface { + use OptionsCollectorTrait; + + // ------------------------------------------------------------------------ + /** + * AbstractComposite::__toString + * + * @return mixed + */ + public function __toString() + { + return $this->render($this->options); + } } \ No newline at end of file diff --git a/src/Traits/Collectors/ErrorCollectorTrait.php b/src/Traits/Collectors/ErrorCollectorTrait.php index ae69c13..b9cf0d2 100644 --- a/src/Traits/Collectors/ErrorCollectorTrait.php +++ b/src/Traits/Collectors/ErrorCollectorTrait.php @@ -73,13 +73,13 @@ public function hasErrors() // ------------------------------------------------------------------------ /** - * ErrorCollectorTrait::getLatestErrorMessage + * ErrorCollectorTrait::getLastErrorMessage * * Returns latest error message. * * @return string|bool Returns FALSE if Failed. */ - public function getLatestErrorMessage() + public function getLastErrorMessage() { if (count($this->errors)) { return end($this->errors); @@ -91,13 +91,13 @@ public function getLatestErrorMessage() // ------------------------------------------------------------------------ /** - * ErrorCollectorTrait::getLatestErrorCode + * ErrorCollectorTrait::getLastErrorCode * * Returns latest error code. * * @return int|bool Returns FALSE if Failed. */ - public function getLatestErrorCode() + public function getLastErrorCode() { if (count($this->errors)) { end($this->errors); diff --git a/src/Traits/Collectors/OptionsCollectorTrait.php b/src/Traits/Collectors/OptionsCollectorTrait.php new file mode 100644 index 0000000..2a207cc --- /dev/null +++ b/src/Traits/Collectors/OptionsCollectorTrait.php @@ -0,0 +1,146 @@ +options[ $key ])) { + if (is_array($value) AND is_array($this->options[ $key ])) { + $this->options[ $key ] = array_merge($this->options[ $key ], $value); + } else { + $this->options[ $key ] = $value; + } + } else { + $this->options[ $key ] = $value; + } + + return $this; + } + + // ------------------------------------------------------------------------ + + /** + * Get Options + * + * @access public + * @final this method can't be overwritten + * + * @param string|null $key Options item index name + * + * @return mixed + */ + final public function getOption($key, $offset = null) + { + if (isset($this->options[ $key ])) { + if (isset($offset)) { + return isset($this->options[ $key ][ $offset ]) ? $this->options[ $key ][ $offset ] : null; + } + + return $this->options[ $key ]; + } + + return false; + } + + // ------------------------------------------------------------------------ + + /** + * Get Options + * + * @access public + * @final this method can't be overwritten + * + * @param string|null $key Options item index name + * + * @return mixed + */ + final public function getOptions($key = null, $offset = null) + { + if (isset($key)) { + if (isset($this->options[ $key ])) { + if (isset($offset)) { + return isset($this->options[ $key ][ $offset ]) ? $this->options[ $key ][ $offset ] : null; + } + + return $this->options[ $key ]; + } + + return false; + } + + return $this->options; + } + + // ------------------------------------------------------------------------ + + /** + * Set Options + * + * @access public + * + * @param array|string|int|Options $key + * + * @return static + */ + public function setOptions($key, $value = null) + { + if (is_array($key)) { + if (empty($this->options)) { + $this->options = $key; + } else { + $this->options = array_merge($this->options, $key); + } + } elseif ($key instanceof Options) { + $this->options = $key; + } elseif (isset($this->options[ $key ])) { + if (is_array($value) AND is_array($this->options[ $key ])) { + $this->options[ $key ] = array_merge($this->options[ $key ], $value); + } else { + $this->options[ $key ] = $value; + } + } else { + $this->options[ $key ] = $value; + } + + return $this; + } +} \ No newline at end of file