jeyroik/extas-fields-conditions

There is no license information available for the latest version (1.1.0) of this package.

Fields conditions for Extas

1.1.0 2020-09-03 07:05 UTC

This package is auto-updated.

Last update: 2024-03-29 04:19:22 UTC


README

PHP Composer codecov.io PHPStan Enabled 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f62303662323037363061626333613634306231362f6d61696e7461696e6162696c697479

Описание

Пакет позволяет настроить для полей условия (проверки) для стадий

  • перед созданием
  • после создания
  • перед обновлением
  • после обновления
  • перед удаленеим
  • после удаления

Использование

Для нашей сущности item настроим две проверки перед созданием:

  • Проверим, что значение не равно test.
  • Проверим, что сущность с текущим значением отсутствует.

extas.json

{
  "fields": [
    {
      "name": "value",
      "parameters": {
        "subject": {
          "name": "subject",
          "value": "item"
        }
      },
      "before_create": [
        {
          "condition": "neq",
          "value": "test"
        },
        {
          "condition": "empty",
          "value": {
            "repository": "itemRepository",
            "method": "all",
            "query": {"value": "@value"}
          }
        }
      ]
    }
  ]
}
/**
 * @method itemRepo()
 */
$item = new class ([
    'value' => 'test'
]) extends \extas\components\Item {
    use \extas\components\THasValue;
    protected function getSubjectForExtension() : string{
        return 'item';
    }
};

try {
    $this->itemRepo()->create($item); // Exception "Condition failed"
} catch (\Exception $e) {

}
$item->setValue('unique');
$this->itemRepo()->create($item); // ok
$this->itemRepo()->create($item); // Exception "Condition failed"