Source of file Options.php
Size: 2,572 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Common/Options.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Common; /** * Options Trait * * Example of usage * class Foo * { * use \Bluz\Common\Options; * * protected $bar = ''; * protected $baz = ''; * * public function setBar($value) * { * $this->bar = $value; * } * * public function setBaz($value) * { * $this->baz = $value; * } * } * * $Foo = new Foo(); * $Foo->setOptions(['bar'=>123, 'baz'=>456]); * * @package Bluz\Common * @author Anton Shevchuk * @link https://github.com/bluzphp/framework/wiki/Trait-Options */ trait Options { /** * @var array options store */ protected $options; /** * Get option by key * * @param string $key * @param array $keys * * @return mixed */ public function getOption($key, ...$keys) { $method = 'get' . Str::toCamelCase($key); if (method_exists($this, $method)) { return $this->$method($key, ...$keys); } return Collection::get($this->options, $key, ...$keys); } /** * Set option by key over setter * * @param string $key * @param string $value * * @return void */ public function setOption($key, $value) { $method = 'set' . Str::toCamelCase($key); if (method_exists($this, $method)) { $this->$method($value); } else { $this->options[$key] = $value; } } /** * Get all options * * @return array */ public function getOptions(): array { return $this->options; } /** * Setup, check and init options * * Requirements * - options must be a array * - options can be null * * @param array $options * * @return self */ public function setOptions(array $options = null) { // store options by default $this->options = (array)$options; // apply options foreach ($this->options as $key => $value) { $this->setOption($key, $value); } // check and initialize options $this->initOptions(); return $this; } /** * Check and initialize options in package * * @throws \Bluz\Config\ConfigException * @return void */ protected function initOptions() { } } |