jeyroik/extas-repositories-fields-uuid

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

UUID fields support for extas

0.1.0 2020-04-09 16:22 UTC

This package is auto-updated.

Last update: 2024-11-10 03:00:22 UTC


README

PHP Composer codecov.io

Описание

Allow to auto-fill fields on create with uuid.

Usage

  1. Add plugin for your repo create stage.
  2. Install plugin.
  3. Place field marker (see below) into field value.

Applicable field markers

  • @uuid6
  • @uuid4
  • @uuid5.<namespace>.<name>

Example

Prepare repository + item class.

namespace example;

use extas\components\repositories\Repository;
use extas\components\Item;
use extas\components\THasId;

class Example extends Item
{
    use THasId;

    public function getUuid()
    {
        return $this->config['uuid'] ?? '';
    }

    protected function getSubjectForExtension() : string
    {
        return 'example';
    }
}

class ExampleRepo extends Repository
{
    protected string $name = 'example';
    protected string $itemClass = Example::class;
}

extas.json

{
  "plugins": [
    {
      "class": "extas\\components\\plugins\\repositories\\PluginUUidField",
      "stage": "extas.example.create.before"
    }
  ]
}

Usage

use example\Example;
use example\ExampleRepo;

$example = new Example(['uuid' => '@uuid4']);
$repo = new ExampleRepo();
$created = $repo->create($example);
echo $created->getUuid(); // something like af29a3f4-f865-3a4a-8a87-dc8dc0b813cr

Notice

Your repository should allow create before stage (this is by default).

See extas\components\repositories\Repository::isAllowCreateBeforeStage property for details.