Giter Club home page Giter Club logo

keyable's Introduction

๐Ÿ— Keyable Models in Laravel

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

keyable's People

Contributors

benvilliere avatar

Stargazers

 avatar

Watchers

James Cloos 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.