States
Apporter le pattern State à vos projets.
Pour créer des classes implémentant le pattern "State" en PHP. Une méthode efficace pour permettre à un objet de changer de comportement sans à avoir recours à de nombreuses instructions conditionnelles..
The state pattern is a behavioral software design pattern that implements a state machine in an object-oriented way. With the state pattern, a state machine is implemented by implementing each individual state as a derived class of the state pattern interface, and implementing state transitions by invoking methods defined by the pattern's superclass.
The state pattern can be interpreted as a strategy pattern which is able to switch the current strategy through invocations of methods defined in the pattern's interface.
This pattern is used in computer programming to encapsulate varying behavior for the same object based on its internal state. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements and this improve maintainability.
Fonctionnalités
Création d'états
Organiser vos classes en différents états afin d'éviter de trop nombreuses instructions conditionnelles.
Héritage d'états et de classes
Compléter et factoriser vos états grâce à l'héritage. Les états peuvent aussi être hérités.
Automisation des changements d'états
Définir les règles permettant le changement automatique dans un état pré-défini.
Utilisable n'importe ou
Grâce aux traits et interfaces, implémenter ce pattern sur vos codes existants. Compatible avec Doctrine.
Fork le project sur GitHub
Il est sous licence open source! Il est hébergé, développé et maintenu sur GitHub par des contributeurs comme vous.
Voir le projet sur GitHub
Exemple
<?php
require 'vendor/autoload.php';
use Teknoo\States\Automated\AutomatedInterface;
use Teknoo\States\Automated\AutomatedTrait;
use Teknoo\States\Automated\Assertion\AssertionInterface;
use Teknoo\States\Automated\Assertion\Property;
use Teknoo\States\Automated\Assertion\Property\IsEqual;
use Teknoo\States\Proxy\ProxyInterface;
use Teknoo\States\Proxy\ProxyTrait;
use Teknoo\States\State\AbstractState;
class English extends AbstractState
{
public function sayHello(): \Closure
{
return function(): string {
return 'Good morning, '.$this->name;
};
}
public function displayDate(): \Closure
{
return function(\DateTime $now): string {
return $now->format('m d, Y');
};
}
}
class French extends AbstractState
{
public function sayHello(): \Closure
{
return function(): string {
return 'Bonjour, '.$this->name;
};
}
public function displayDate(): \Closure
{
return function(\DateTime $now): string {
return $now->format('d m Y');
};
}
}
class Person implements ProxyInterface, AutomatedInterface
{
use ProxyTrait,
AutomatedTrait;
/** @var string */
private $name;
/** @var string */
private $country;
public function __construct()
{
$this->initializeProxy();
}
protected static function statesListDeclaration(): array
{
return [
English::class,
French::class
];
}
protected function listAssertions(): array
{
return [
(new Property([English::class]))
->with('country', new IsEqual('en')),
(new Property([French::class]))
->with('country', new IsEqual('fr')),
];
}
public function setName(string $name): Person
{
$this->name = $name;
return $this;
}
public function setCountry(string $country): Person
{
$this->country = $country;
$this->updateStates();
return $this;
}
}
$frenchMan = new Person();
$frenchMan->setCountry('fr');
$frenchMan->setName('Roger');
$englishMan = new Person();
$englishMan->setCountry('en');
$englishMan->setName('Richard');
$now = new \DateTime('2016-07-01');
foreach ([$frenchMan, $englishMan] as $man) {
echo $man->sayHello().PHP_EOL;
echo 'Date: '.$man->displayDate($now).PHP_EOL;
}
//Display
//Bonjour, Roger
//Date: 01 07 2016
//Good morning, Richard
//Date: 07 01, 2016