Giter Site home page Giter Site logo

atkdatamodeltraits's Introduction

atkdatamodeltraits

codecov

A small collection of traits to be used with Atk4\Data\Model.

CreatedDateAndLastUpdatedTrait

This trait can be used to add a created_date and/or a last_updated field to a model. The corresponding hooks are automatically added to save created_date on insert and last_updated on update.

class SomeModel extends Model
{
    use CreatedDateAndLastUpdatedTrait;

    protected function init(): void
    {
        parent::init();
        $this->addCreatedDateFieldAndHook();
        $this->addLastUpdatedFieldAndHook();
    }
}

UniqueFieldTrait

This trait provides a function isFieldUnique() to check if the field's current value is unique among all records in persistence. This functionality can also be achieved by adding a UNIQUE index to an SQL persistence. Having this functionality in the application layer can be sensible to be independent of the persistence's features - or to avoid getting back an exception from the persistence if a non-unique value is being tried to save.

class SomeModel extends Model
{
    use UniqueFieldTrait;

    protected function init(): void
    {
        parent::init();
        $this->addField('unique_field');
    }
    
    $this->onHook(
        Model::HOOK_BEFORE_SAVE,
        function(self $entity, bool $isUpdate) {
            while(!$entity->isFieldUnique('unique_field')) {
                //some function which recalculates field value.
            }
        }
    );
}

CryptIdTrait

This trait is used to generate cryptic IDs like D6f2-a395Jskv2. You can freely define the format the cryptic ID shall have. The functionality can for example be used to create coupon codes or create unguessable, yet human-readable codes/identifiers. The characters I,l,0 and O are removed as they can be easily be mistaken by humans. To use this trait, you just need to add the cryptic ID field in Model::init() using addCryptIdFieldAndHooks and implement a custom method generateCryptId() that returns a random string in the format of your choice:

class SomeModel extends Model
{  
    use CryptIdTrait;

    protected function init(): void
    {
        parent::init();
        $this->addCryptIdFieldAndHooks('crypt_id');
    }

    /**
     * Custom implementation if cryptic ID format, In this case XXXXX-XXXXX-XXXXX
     */
    protected function generateCryptId(): string
    {
        $cryptId = '';
        for ($i = 0; $i < 3; $i++) {
            if($i > 0) {
                $cryptId .= '-';
            }
            for ($j = 0; $j < 5; $j++) {
                $cryptId .= $this->getRandomChar();
            }
        }

        return $cryptId;
    }
}

atkdatamodeltraits's People

Contributors

philippgrashoff avatar

Stargazers

Imants Horsts avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.