定制 hi-folks/rando-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hi-folks/rando-php

Composer 安装命令:

composer require hi-folks/rando-php

包简介

Random generator Library for PHP

README 文档

README

Testing RandoPHP GitHub last commit GitHub Release Date Packagist PHP Version Support

RandoPHP

RandoPHP is a PHP open-source package for random stuff. With this package you can:

  • Draw: Extract random items (sample) from an array. This is useful when you want to "draw" some numbers or items;
  • Generate: useful for create random items/sequences:
    • item like integer, byte, boolean, float, lat/long coordinates, char (numeric, alphabetic, alphanumeric);
    • sequences like an array of integers or chars;

With the fluent interface, you can control some things like:

  • minimum and maximum value you want to create;
  • how many items you want to create;
  • for sequences if you want or not duplicates ([1,5,3,1,1], 1 is duplicate or [1,6,5,3,8], no duplicates);
  • And other stuff, see the documentation for more options.

Table of contents

Installation

You can install the package via composer:

composer require hi-folks/rando-php

Usage

To import the right class:

use HiFolks\RandoPhp\Randomize;

Generate Char

Sometimes you want to obtain a random char, for example, a numeric char:

Randomize::char()->numeric()->generate();

Or you might want an alphabetic char:

Randomize::char()->alpha()->generate();

You can even do both!

Randomize::char()->alphanumeric()->generate();

You can generate a lower case char:

Randomize::char()->alpha()->lower()->generate();

You can generate an upper case char:

Randomize::char()->alpha()->upper()->generate();

You can generate a special character. A special character is one of '!"#$%&'()*+,-./:;<=>?@[]^_`{|}~':

Randomize::char()->specialCharacters()->generate();

Generate Boolean

Sometimes you want to obtain a random boolean true or false (flip a coin):

$randomBool = Randomize::boolean()->generate();

Generate a Float

Sometimes you want to obtain a random float (default min-max range of 0.0 - 1.0). For example, you want to generate a random temperature for a day:

$randomFloat = Randomize::float()->generate();

Or you can set the min-max range of 0 - 90, which is equivalent to ->min(0)->max(90)

$randomFloat = Randomize::float()->min(0)->max(90)->generate();

Generate an Integer

Sometimes you want to obtain a random integer (min-max range). For example, you want to roll the dice:

$randomNumber = Randomize::integer()->min(1)->max(6)->generate();

The same thing using range() method, instead of min() and max():

$randomNumber = Randomize::integer()->range(1,6)->generate();

You can use a shortcut helper to generate an integer calling the constructor with min and max parameters:

$randomNumber = Randomize::integer(1,6)->generate();

Generate bytes

Sometimes you want to obtain some random bytes (hexadecimal). For example, you want to generate a random RGB color (a hex triplet in hexadecimal format):

$randomRGB = Randomize::byte()->length(3)->generate();

Generate a Date

Sometimes you want to obtain a random date. For example, you want to generate a random date:

$randomDate = Randomize::datetime()->generate();

By default, a date between the first day of the current year to the last day of the current year. If you want to define the range, for example, a range of 01 Jan 2020 to 10 Jan 2020, you can use min() and max() methods:

$randomDate = Randomize::datetime()->min('2020–01–01')->max('2020–01–10')->generate();

You can even specify your preferred format for the random date generated, by using the format() method:

$randomDate = Randomize::datetime()->format('d-M-Y')->generate();

You can use min() and max() with format() method:

$randomDate = Randomize::datetime()->min('2020–01–01')->max('2020–01–10')->format('d-M-Y')->generate();

With the latest example, you can obtain something like "05-Jan-2020".

Generate sequences

Sometimes, you want to obtain some random sequences. For example, you want to roll the dice 15 times:

$randomRolls = Randomize::sequence()->min(1)->max(6)->count(15)->generate();

Sometimes you want to obtain some random char sequences. For example, char sequences of length 10:

$randomChars = Randomize::sequence()->chars()->count(10)->generate();

Or you might want numeric char sequences.

$randomChars = Randomize::sequence()->chars()->numeric()->count(10)->generate();

Or you might want alphabetical char sequences.

$randomChars = Randomize::sequence()->chars()->alpha()->count(10)->generate();

Yes, even both.

$randomChars = Randomize::sequence()->chars()->alphanumeric()->count(10)->generate();

You want 20 lowercase chars:

Randomize::sequence()->chars()->alphaLowerCase()->count(20)->asString()->generate();

You want 20 upper case chars:

Randomize::sequence()->chars()->alphaUpperCase()->count(20)->asString()->generate();

Sometimes you want to obtain some random sequences with no duplicates. For example, you want to play "Tombola" (extracting numbers from 1 to 90 with NO duplicates):

$randomTombola = Randomize::sequence()->min(1)->max(90)->count(90)->noDuplicates()->generate();

Sometimes you want to obtain some random char sequences with no duplicates.

$randomChars = Randomize::sequence()->chars()->count(10)->noDuplicates()->generate();

Or you might want numeric char sequences with no duplicates. For example, char sequences of length 10:

$randomChars = Randomize::sequence()->chars()->numeric()->count(10)->noDuplicates()->generate();

Or you might want alphabetical char sequences with no duplicates.

$randomChars = Randomize::sequence()->chars()->alpha()->count(10)->noDuplicates()->generate();

Yes, even both and with no duplicates.

$randomChars = Randomize::sequence()->chars()->alphanumeric()->count(10)->noDuplicates()->generate();

Random String

If you want to generate a random string (alphanumeric, only letters, only numbers, with no duplication, lower case, upper case etc) you can use random chars generation. To generate a string use Randomize::chars():

$string = Randomize::chars()->generate();

The default behavior is:

  • 10 chars;
  • only letters (a-z);
  • lowercase;
  • duplication is allowed (the string 'aba' has a duplication for 'a' char).

You can change the default values with some methods and parameters. Method to set the type of chars (numeric, alpha, alphanumeric...):

  • alpha() : 'a-z', 'A-Z';
  • alphanumeric() : 'a-z', 'A-Z', '0-9';
  • numeric() : '0-9';
  • alphaLowerCase() : 'a-z';
  • alphaUpperCase() : 'A-Z';
  • specialCharacters() : !"#$%&'()*+,-./:;<=>?@[]^_`{|}~

You have also some methods to "control" the output, for example, avoid duplications:

  • unique(): it generates a string with at most one occurrence per character.

String with numeric chars ('0'-'9')

If you want to generate a string with 16 chars, made with numeric chars ('0'-'9'):

$string = Randomize::chars(16)->numeric()->generate();

You could obtain something like this '3963233500573002'.

String with 20 chars, lower case, just letters, no dups

If you want to obtain a string with 20 chars, lower case and just letters ('a'-'z'), and you want to avoid character duplications ( alphaLowerCase() and unique() ):

$string = Randomize::chars(20)->alphaLowerCase()->unique()->generate();

You could obtain something like this: 'nmbsjhrgdyfxwoltqkzp'.

String with 20 chars, letters and special characters

If you want to obtain a string with 20 chars, with letters and special characters:

$string = Randomize::chars(20)->specialCharacters()->alpha()->generate();

You could obtain something like this: 'IOgPckeGGifrD%DRy[*!'.

Draw random stuff

If you have a list of values and you want to extract/select/draw one or more elements, you could use the Draw class instead of Randomize. For using the Draw class you need to import it:

use HiFolks\RandoPhp\Draw;

Suggest which programming language you could use in your next project (random)

use HiFolks\RandoPhp\Draw;

$randomLanguage = Draw::sample(["PHP", "Python", "Golang", "Javascript"])->snap();
var_dump($randomLanguage);

Suggest which JS framework you could use in your next project (random)

$array=["React.js", "Vue.js", "Svelte.js", "Angular.js" , "Alpine.js", "Vanilla js"];
$randomJs = Draw::sample($array)->extract();

Extract 3 JS frameworks you could use in your next project

$array=["React.js", "Vue.js", "Svelte.js", "Angular.js" , "Alpine.js", "Vanilla js"];
$randomJs = Draw::sample($array)->count(3)->extract();

Extract 3 JS frameworks (duplicates allowed)

$array = ["React.js", "Vue.js", "Svelte.js", "Angular.js", "Alpine.js", "Vanilla js"];
$randomJs = Draw::sample($array)->count(3)->allowDuplicates()->extract();

Testing

composer test

If you want to see some coverage reports you can execute phpunit command with coverage-text option:

vendor/bin/phpunit --coverage-text

Warning ⚠️

Under the hood, RandoPHP uses some native PHP functions like:

These PHP functions use a pseudo-random number generator that is not suitable for cryptographic purposes.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Submit ideas or feature requests or issues

Take a look if your request is already created https://github.com/Hi-Folks/rando-php/issues If it is not present, you can create a new one https://github.com/Hi-Folks/rando-php/issues/new

Credits

License

The MIT License (MIT). Please see License File for more information.

hi-folks/rando-php 适用场景与选型建议

hi-folks/rando-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 91.64k 次下载、GitHub Stars 达 128, 最近一次更新时间为 2020 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generator」 「library」 「random」 「sequence」 「draw」 「hi-folks」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hi-folks/rando-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hi-folks/rando-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 hi-folks/rando-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 91.64k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 129
  • 点击次数: 24
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 128
  • Watchers: 2
  • Forks: 22
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-19