Use this document to help you transition away from deprecated code.
Version 2.0.0 simplifies the CommandInterface
and adds an OptionsInterface
.
Combined, these two interfaces represent the fundamental requirements of a
command system that is flexible and can be used in modern and legacy environments.
All options must implement OptionsInterface
.
Options must validate the values passed during construction. If you are using an
array to hydrate the options, the OptionsRequiredTrait
will help you validate
requirements. Type checking should also be done when suitable.
use Equip\Command\OptionsInterface;
use Equip\Command\OptionsRequiredTrait;
class LoginOptions implements OptionsInterface
{
use OptionsRequiredTrait;
private $email;
private $password;
public function __construct(array $options)
{
$this->checkRequired($options, [
'email',
'password',
]);
$this->email = $options['email'];
$this->password = $options['password'];
}
public function email()
{
return $this->email;
}
public function password()
{
return $this->password;
}
}
All commands must implement the CommandInterface
instead of AbstractCommand
.
Commands should be immutable and return a copy when withOptions()
is called.
The CommandImmutableOptionsTrait
can provide this functionality.
use Equip\Command\CommandInterface;
use Equip\Command\CommandImmutableOptionsTrait;
class LoginCommand implements CommandInterface
{
use CommandImmutableOptionsTrait;
/**
* @var LoginOptions
*/
private $options;
public function withOptions(LoginOptions $options)
{
return $this->copyWithOptions($options);
}
/**
* @return object
*/
public function execute()
{
$user = $this->getUser($this->options->email());
$this->checkCredentials($user, $this->options->password());
return $user;
}
}
$options = new LoginOptions([
'email' => '[email protected]',
'password' => 'very secret',
]);
$command = new LoginCommand();
$command = $command->withOptions($options);
$user = $command->execute();