Skip to content

Commit

Permalink
add options setter trait
Browse files Browse the repository at this point in the history
  • Loading branch information
steevenz committed Jul 10, 2020
1 parent 790b21f commit a4c60d9
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/Patterns/Structural/Composite/AbstractComposite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 4 additions & 4 deletions src/Traits/Collectors/ErrorCollectorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
146 changes: 146 additions & 0 deletions src/Traits/Collectors/OptionsCollectorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* This file is part of the O2System Framework package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Steeve Andrian Salim
* @copyright Copyright (c) Steeve Andrian Salim
*/

// ------------------------------------------------------------------------

namespace O2System\Spl\Traits\Collectors;

// ------------------------------------------------------------------------

use O2System\Kernel\DataStructures\Options;

/**
* Class OptionsCollectorTrait
*
* @package O2System\Spl\Traits\Collectors
*/
trait OptionsCollectorTrait
{
/**
* List of Options
*
* @type array
*/
protected $options = [];

// ------------------------------------------------------------------------

/**
* Add Option
*
* @param $key
* @param $value
*
* @return $this
*/
public function addOption($key, $value)
{
if (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;
}

// ------------------------------------------------------------------------

/**
* 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;
}
}

0 comments on commit a4c60d9

Please sign in to comment.