angle/keyable

Add random unique keys to any model in a Laravel application.

v1.0 2019-02-03 19:00 UTC

This package is auto-updated.

Last update: 2024-04-12 05:37:44 UTC


README

Introduction

Many web applications require unique references to their data. Sometimes, increments and uuids are not enough for specific use-cases. Keyable attempts to solve this problem by providing methods that are automatically called on your models, via a trait.

Requirements

  • PHP >= 7.1
  • Laravel >= 5.6
  • Relational Database (not tested with other drivers)

Installation

Install the package via Composer:

composer require angle/keyable

Using the Keyable Trait

Granted the Model is using Angle\Keyable\Keyable, unique keys are automatically generated upon the creating() Eloquent Model Event.

<?php

namespace App;

use Angle\Keyable\Keyable;
use Illuminate\Database\Eloquent\Model;

class Vault extends Model
{
    use Keyable;

    /**
     * Attributes containing unique keys.
     *
     * @var array
     */
    public $keys = ['fingerprint' => 256];
}

Implementing A Custom Strategy

By default, the Keyable Trait uses Laravel Str::random() helper to generate unique keys. You may override this behavior by implementing keyableStrategy() method on your model.

<?php

namespace App;

use Angle\Keyable\Keyable;
use Illuminate\Database\Eloquent\Model;

class Endpoint extends Model
{
    use Keyable;

    /**
     * Attributes containing unique keys.
     *
     * @var array
     */
    public $keys = [
        'public_key' => 128,
        'private_key' => 128
    ];

    public function keyableStrategy(string $attribute, int $length) : string
    {
        $prefix = 'public-';

        if ($attribute == 'private_key') {
            $prefix = 'private-';
        }

        return $prefix . \Str::random($length);
    }
}

Contributing

Contributions are welcomed! If you have any idea you'd like to implement, feel free to submit a pull request.

Licence

MIT

Copyright © 2019 Angle Software