yuriitatur / strategies
A simple package to update entities in certain declarative way
Requires
- php: >=8.2
- psr/event-dispatcher: ^1.0.0
- psr/log: ^3.0
- yuriitatur/pipeline: ^1.0
- yuriitatur/repository: dev-master
Requires (Dev)
- dg/bypass-finals: ^1.3
- jms/serializer: ^3.32
- kint-php/kint: ^6.0
- phpunit/phpunit: ^12
- psr/container: ^1.0
- symfony/serializer: ^7.3
- symfony/yaml: ^7.3
Suggests
- jms/serializer: Adds ability to use JmsSerializerDehydrator
- symfony/serializer: Allows to use SymfonySerializerDehydrator
This package is auto-updated.
Last update: 2025-08-08 15:45:26 UTC
README
Simple entity strategy package
Installation
Composer:
composer require yuriitatur/strategies
Usage
// $manager instanceOf ChangesManager
$saga = $manager->initialize($entity); // initialize changes listening
$entity->setNewValue(123); // modify data
$saga->apply(); //save changes| do smth else
This will get all the possible strategies and apply them.
Strategy
The strategy is an object of a class implementing StrategyInterface.
It has only one method - apply
which applies it. You can add conditions to it by applying
PreCondition
attribute and specify the condition under what this strategy may be applied.
After all preconditions are met, the strategy is applied. You can manipulate it by specifying middlewares
BeforeApply, AfterApply and
OverApply, which, as you can guess, are applied before, after or have full
control around the apply method.
#[PreCondition(RequiresEntityId::class)]
#[PreCondition(HasChanged::class, ['status'])]
#[BeforeApply(SaveEntity::class)]
class ShipOrder implements StrategyInterface
{
public function apply(Entity $order)
{
event(new OrderShipped($order));
}
}
Testing
composer test
License
This code is under MIT license, read more in the LICENSE file.