yuriitatur/strategies

A simple package to update entities in certain declarative way

dev-master 2025-08-08 15:45 UTC

This package is auto-updated.

Last update: 2025-08-08 15:45:26 UTC


README

Quality Gate Status Coverage

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.