PHP code example of mrself / options

1. Go to this page and download the library: Download mrself/options library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mrself / options example snippets


class ClassWithOptions
{
    use \Mrself\Options\WithOptionsTrait;
}

use Mrself\Options\Annotation\Option;

class ClassWithOptions
{
    use \Mrself\Options\WithOptionsTrait;

    /**
    * The this->arrayOption;
    }
}

$instance = ClassWithOptions::make(['arrayOption' => ['key' => 'value']]);

// True
$instance->getOption()['key'] === 'value';


// Exception \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
$instance = ClassWithOptions::make();

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]);


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';

$object->init(['.silent' => true]);

$object = new class {
    /**
     * @Option
     * @var \Reflection
     */
    public $option1;
};

// Throws since 'option1' expected a value of type '\Reflection'
$object->init(['option1' => 1]);

new class {
    protected function getOptionsSchema()
    {
        return [
            'allowedTypes' => ['option1' => \Reflection::class]
        ];
    }
 };

$object = new class {
    /**
     * @Option(option1;
};

$object = new class {

    /**
     * @Option()
     * @var string
     */
    public $option1;
};
$object::presetOptions('nameOfPreset', [
    'option1' => 'value1'
]);
$object->init(['presetName' => 'nameOfPreset']);
$object->option1 === 'value1';