mrself / options
There is no license information available for the latest version (v2.19.7) of this package.
v2.19.7
2021-02-28 19:39 UTC
Requires
- ext-memcached: *
- doctrine/annotations: >=1.6
- mrself/container: ^1.13.2
- mrself/util: ^1.5.0
- php-di/phpdoc-reader: ^2.1
- symfony/options-resolver: >=4.4
Requires (Dev)
- phpbench/phpbench: ^1.0@dev
- phpunit/phpunit: ^7.5
- symfony/var-dumper: ^4.2
- dev-master
- v2.19.7
- v2.19.6
- v2.19.5
- v2.19.4
- v2.19.3
- v2.19.2
- v2.19.1
- v2.19.0
- v2.18.0
- v2.17.0
- v2.16.1
- v2.16.0
- v2.15.4
- v2.15.3
- v2.15.2
- v2.15.1
- v2.15.0
- v2.14.1
- v2.13.2
- v2.13.1
- v2.13.0
- v2.12.0
- v2.11.2
- v2.11.1
- v2.11.0
- v2.10.0
- v2.9.3
- v2.9.2
- v2.9.1
- v2.9.0
- v2.8.4
- v2.8.3
- v2.8.2
- v2.8.1
- v2.7.1
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.1
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.1
- v2.2.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- dev-dependabot/composer/symfony/process-5.4.46
- dev-develop
This package is auto-updated.
Last update: 2024-11-06 18:36:30 UTC
README
Add the trait to your class
class ClassWithOptions { use \Mrself\Options\WithOptionsTrait; }
Add the first option:
use Mrself\Options\Annotation\Option; class ClassWithOptions { use \Mrself\Options\WithOptionsTrait; /** * The required option to initialize the class * @Option() * @var array */ private $arrayOption; public function getOption() { return $this->arrayOption; } }
Initialize the class:
$instance = ClassWithOptions::make(['arrayOption' => ['key' => 'value']]); // True $instance->getOption()['key'] === 'value';
An exception is thrown if the option is of missed:
// Exception \Symfony\Component\OptionsResolver\Exception\MissingOptionsException $instance = ClassWithOptions::make();
More examples
Type resolving:
use Mrself\Options\Annotation\Option; class ClassWithOptions { use \Mrself\Options\WithOptionsTrait; /** * @Option() * @var array */ private $arrayOption; public function getOption() { return $this->arrayOption; } } $notArray = 1; // Exception ClassWithOptions::make(['arrayOption' => $notArray]);
use Mrself\Options\Annotation\Option; class ClassWithOptions { use \Mrself\Options\WithOptionsTrait; /** * @Option() * @var \DateTime */ private $arrayOption; public function getOption() { return $this->arrayOption; } } $notDate = 1; // Exception ClassWithOptions::make(['arrayOption' => $notDate]);
Using with container (see mrself/container)
use Mrself\Options\Annotation\Option; use Mrself\Container\Container; use Mrself\Container\Registry\ContainerRegistry; $service = new \stdClass(); $service->property = 'myProperty'; $container = Container::make(); $container->set('service', $service); ContainerRegistry::add('App', $container); class ClassWithOptions { use \Mrself\Options\WithOptionsTrait; /** * @Option() * @var \stdClass */ private $service; public function getService() { return $this->service; } } $instance = ClassWithOptions::make(); // True $instance->getService()->property === 'myProperty';
This trait can be used with Symfony or another framework with public services.
Suspend all errors:
$object->init(['.silent' => true]);
If an annotated property has a non-primitive type, the property can be resolved only of that type:
$object = new class { /** * @Option * @var \Reflection */ public $option1; }; // Throws since 'option1' expected a value of type '\Reflection' $object->init(['option1' => 1]);
Primitive types are not processed so they should be declared in array schema:
new class { protected function getOptionsSchema() { return [ 'allowedTypes' => ['option1' => \Reflection::class] ]; } };
Array schema has a higher priority than an annotation schema
An option can be set as optional:
$object = new class { /** * @Option(required=false) * @var \Reflection */ public $option1; };
Options can be preset by a specific key:
$object = new class { /** * @Option() * @var string */ public $option1; }; $object::presetOptions('nameOfPreset', [ 'option1' => 'value1' ]); $object->init(['presetName' => 'nameOfPreset']); $object->option1 === 'value1';