Gerador de chaves com padrões de caracteres

Overview

Keygen

Essa documentação está disponível também em Português do Brasil.

This plugin is under development and may exhibit unexpected behavior while using it, please report any bugs or errors you find when using this solution, so that we can make it the best possible for your use!

The Keygen plugin is a key or serial generator based on user-defined mask patterns. The plugin is able to generate keys like the example below:

6W2F-4RJB-KV0Z-ADA6-4SJ2

It can be used to generate keys for several purposes, some examples are:

  • Unique links to reset passwords
  • Registration via invitation link
  • Two-Factor Authentication Codes
  • File sharing links
  • Product Redemption Keys
  • Discount coupons

Installation

Fisrt, run the following command in your project:

composer require kaiopiola/keygen

Then, call the main class on the PHP file you want to load it:

use Kaiopiola\Keygen\Key;

How Keygen works

How to create a new key

To create a new key, just instantiate the class in a variable, and then define its configuration parameters, as shown in the example below:

# Instantiating class in a variable:
$exampleKey = new Key;

# Setting parameters:
$exampleKey->setPattern("XXXX-NNNN-LLLL"); // Defines the pattern that the code will use to define its new key. 
// The letters represent the type of content that can be used in that space, as per the rule:
// X - Any letter or number
// N - Numbers only as defined
// L - Letters only as defined
// Any other characters like dashes, dots, slashes, and even other letters and numbers, will not be replaced.
// If this property is not defined, the following will be used by default: XXXXX-XXXXX-XXXXX

$exampleKey->setNumbers("789"); // Defines the numbers that can compose your key, if not defined, it will use by default "0123456789"
$exampleKey->setLetters("ABC"); // Defines the letters that can compose your key, if not defined, it will use by default "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

$exampleKey->generate(); // Generate your key and return a string value with the result

Considerations

This project is open to contributions, whether with suggestions or improvements to the code of any kind. Feel free to participate in its development.

License

MIT License (MIT). Please, read the License File for more informations.

You might also like...
Comments
  • Setters don't need to return a value

    Setters don't need to return a value

    https://github.com/kaiopiola/keygen-package/blob/501660d6388d2cdc7df86fdbc73129483b8c20c7/src/Key.php#L37-L42

    The value returned is the same as the parameter passed.

    Also, the phpdoc says it returns a string, but the return type will be the type of the parameter passed. Which is not assured it will be a string.

    opened by ordago 2
  • Dynamic properties will be removed in php9, deprecated in php8.2

    Dynamic properties will be removed in php9, deprecated in php8.2

    https://github.com/kaiopiola/keygen-package/blob/a39b6852756fce60af3dbf96c73f9281f1d2043b/src/Key.php#L71-L72

    Here is a link to the RFC:

    https://wiki.php.net/rfc/deprecate_dynamic_properties

    deprecated 
    opened by ordago 0
  • Possible PHP8 solution

    Possible PHP8 solution

    Hey, I saw your post on reddit and just wanted to give you a little inspiration to see what could be possible. My solution may not be the 100% perfect solution, but maybe you can learn something from it and optimize your library as you think it is useful.

    https://github.com/regnerisch/keygen-package

    However, great work and I like you are wanting to improve your skills!

    Edit: I just changed code, to see how the library works take a look a tests/KeyGeneratorTest.php

    opened by regnerisch 2
Releases(1.2.5)
  • 1.2.5(May 24, 2022)

    What's Changed

    • New function to generate unique keys to avoid duplicates.

    Full Changelog: https://github.com/kaiopiola/keygen-package/compare/1.2.0...1.2.5

    Source code(tar.gz)
    Source code(zip)
  • 1.2.0(Jan 18, 2022)

    What's Changed

    • moves default values to properties by @ordago in https://github.com/kaiopiola/keygen-package/pull/8
    • adds phpunit and creates first test by @ordago in https://github.com/kaiopiola/keygen-package/pull/11
    • some functions has their names changed: $example->pattern() to $example->setPattern(); $example->numbers() to $example->setNumbers(); $example->letters() to $example>setLetters();

    New Contributors

    • @ordago made their first contribution in https://github.com/kaiopiola/keygen-package/pull/8

    Full Changelog: https://github.com/kaiopiola/keygen-package/compare/v1.1...1.2.0

    Source code(tar.gz)
    Source code(zip)
  • v1.1(Jan 13, 2022)

  • v1.0(Jan 12, 2022)

    What's Changed

    • Create LICENSE by @kaiopiola in https://github.com/kaiopiola/keygen/pull/1

    New Contributors

    • @kaiopiola made their first contribution in https://github.com/kaiopiola/keygen/pull/1

    Full Changelog: https://github.com/kaiopiola/keygen/compare/v0.9...v1.0

    Source code(tar.gz)
    Source code(zip)
  • v0.9(Jan 12, 2022)

  • v0.8(Jan 12, 2022)

  • v0.7(Jan 12, 2022)

  • v0.6(Jan 12, 2022)

Owner
Kaio Piola
Backend developer
Kaio Piola
API para Consulta de CPF, Gerador e Validador de CPF feito em PHP

Ferramentas para CPF em PHP Você poderá desenvolver paineis de consulta, geradores, bots e o que mais a sua mente imaginar. Instalação e atualização D

Rosiel Victor 1 Nov 27, 2021
Biblioteca PHP para validação de padrões brasileiros

BrCommons Validators BrCommons validator is a set of usefull validators that helps you develop fast and realiable code. Installation by composer Run c

Fernando Rech 3 Oct 2, 2021
Um micro sistema de exemplo que usa alguns padrões, mas o destaque para chain of resposibility

Projeto para ilustrar o uso do Chain Of Responsibility O objetivo é apresentar exemplos de aplicação do padrão de projeto Chain of Responsibility. Nes

Alexandre Bezerra Barbosa 2 Sep 15, 2022