Code Monkey home page Code Monkey logo

rando-php's People

Contributors

anita-ihuman avatar armsasmart avatar davidribeiro avatar jirkavrba avatar marcio-adue avatar paresh27 avatar pret3nti0u5 avatar quanlh-smartosc avatar rebelchris avatar roberto-butti avatar rocksheep avatar sam0hack avatar septikwar avatar vrabe avatar webhaikal avatar wesolowski avatar xanadev avatar zuruckt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rando-php's Issues

Test: add test case to RandomSequenceTest.php

In order to test the Character Sequence random generation, in the file tests/RandomSequenceTest.php add tests for :

  • test alpha() method;
  • test numeric() method;
  • test alphanumeric() method.

Useful things:

  • In order to understand how to call Randomize::sequence()->chars() take a look to examples/RandomSequenceChar.php

examples: create an example for src/Models/LatLong.php

In examples directory create a new php file, named : RandomLatLong.php

In this file, I would like to:

  • include autoload
  • use the class needed (HiFolks\RandoPhp\Randomize)
  • write the call to generate() method from Randomize class with latlong().

I would like to show how to use:

  • Generate coordinates in array format. Something like: Randomize::latlong()->asArray()->generate();
  • Generate coordinates in object format. Something like: Randomize::latlong()->asObject()->generate();
  • Generate latitude in float format. Something like: Randomize::latlong()->asLatitude()->generate();
  • Generate longitude in float format. Something like: Randomize::latlong()->asLongitude()->generate();

My suggestion is to take a look from: examples/RandomFloat.php file, just to see the structure of the file

examples: create an example for Random Sequence Char

In examples directory create a new php file, named : RandomSequenceChar.php

In this file, I would like to:

  • include autoload
  • use the class needed (HiFolks\RandoPhp\Randomize)
  • write the call to generate() method from Randomize class with ::sequence()

My suggestion is to take a look from: examples/basic.php file where Randomize::sequence() is used , and in Readme file where Randomize::sequence()->chars() is used, for example:

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

WE would need 3 examples:

  • numeric chars sequence;
  • alphabetical chars sequence;
  • alphanumeric chars sequence

Add snap() method to return 1 item with Extract from Array

In Draw class, extract method returns a sub array.
In order to retrieve just 1 value from original array add snap() method.
In order to retrieve just 1 key from original array add snapKey() method.

Example usage:

use HiFolks\RandoPhp\Draw;

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

create src/Models/Float.php

Create new Model class for float number (with decimals).
Start from src/Models/Integer.php as "template"

  • file: src/Models/Float.php
  • class: Float
  • namespace: HiFolks\RandoPhp\Models
  • attribtues: min (default 0 )and max (default 1), for lowest char and highest char
  • attributes: decimals , default (2 decimal)
  • generate: genrate a float number considering min, max, decimals attributes;

Example:

$float = Randomize::float()->decimal(5)->min(0)->max(100)->generate();

It generates something like this: 12.42213

Test: create RandomCharTest

In order to test the Character random generation, create a test :

class RandomCharTest extends TestCase

in tests/RandomCharTest.php.

Things to test:

  • generate a random numeric char.
  • generate a random alphabetic char.
  • generate a random alphanumeric char.

Useful things:

  • In order to understand how to call Randomize::char() take a look to examples/RandomChar.php
  • If you want to start quickly i suggest you to take a look to tests/RandomIntegerTest.php . I think it is useful to create the structure of the test class.

examples: create an example for Random Integer

In examples directory create a new php file, named : RandomInteger.php

In this file, I would like to:

  • include autoload
  • use the class needed (HiFolks\RandoPhp\Randomize)
  • write the call to generate() method from Randomize class with integer

I would like to show how to use:

  • Generate integer with min and max ( something like: Randomize::integer()->min(1)->max(6)->generate() );
  • Generate integer with range ( something like: Randomize::integer()->range(1,6)->generate() );
  • Generate integere using the default ( something like Randomize::integer()->generate() ) explaining that the default are 0 and 100;

My suggestion is to take a look from: examples/basic.php file

Add Char in Sequence

Some infos on the context:

Sequence model rely on basic model (liike integer) in order to create a sequence of items.
For example

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

it generates a sequence of integer (because integer is the default type):

  • count(): number of items
  • min(): the min value of the integers
  • max(): the max value of the integers
  • noDuplicates() the generated items in the sequence must be no duplicated (2,4,1,2 is not valid because the 2 ; instead 5,3,7,6 are valid because unique values)

Implementation needed:

Add type "Char" in. Sequence, in order to generate a sequence of chars

Randomize::sequence()->chars()->alphanumeric()->noDuplicates()->generate();

Char Models: manage lower and upper case

Now setAlpha sets just lower case char, ascii codes: range(97, 122).
We need to change in src/Models/Char.php:

  • change method alpha() : needs to include lower range(97, 122) _AND upper(69,90)
  • implement new method alphaLower() : needs to include just lower range(97, 122)
  • implement new method alphaUpper() : needs to include just upper range(69,90)

examples: create an example for Random Boolean

In examples directory create a new php file, named : RandomBoolean.php

In this file, I would like to:

  • include autoload
  • use the class needed (HiFolks\RandoPhp\Randomize)
  • write the call to generate() method from Randomize class with boolean

My suggestion is to take a look from: examples/basic.php file

Makefile for unit test, phpstan and phpcs

Create a Make file with some tasks for:

  • launching unit tests:
vendor/bin/phpunit --coverage-text
  • executing phpstan with configuration from phpstan.neon
phpstan analyse
  • executing phpcs for PSR12 standard
phpcs --standard=PSR12 src

examples: create an example for Random Sequence Integer

In examples directory create a new php file, named : RandomSequenceInteger.php

In this file, I would like to:

  • include autoload
  • use the class needed (HiFolks\RandoPhp\Randomize)
  • write the call to generate() method from Randomize class with ::integer()

My suggestion is to take a look from: examples/basic.php file where Randomize::sequence() is used , and in Readme file where Randomize::sequence() is used, for example:

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

We would need 2 examples:

  • noDuplicates() usage;
  • implode() usage;

Array union operator "+" overwrites ranges

Array "+" operator doesn't yield the desired result when using numerical keys.

From PHP docs:

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

$this->ascii_codes = range(48, 57) + range(97, 122);

$this->ascii_codes = range(48, 57) + range(97, 122);

In both cases if you print_r the "ascii_codes" the result would be:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => k
[11] => l
[12] => m
[13] => n
[14] => o
[15] => p
[16] => q
[17] => r
[18] => s
[19] => t
[20] => u
[21] => v
[22] => w
[23] => x
[24] => y
[25] => z
)

You might want to use array_merge instead

Fix standard=PSR12

Fix waring / error following indications provided by:

phpcs --standard=PSR12 src

create src/Models/Char.php

Create new Model class for characters.
Start from src/Models/Integer.php as "template"

  • file: src/Models/Char.php
  • class: Char
  • namespace: HiFolks\RandoPhp\Models
  • attribtues: min and mix, for lowest char and highest char
  • generate: use random_int to extract elements from an array created (for example) with: $ascii_codes = range(48, 57) + range(97, 122);

Better solutions and/or implementations are welcome :)

add asString() method to SEquence

In Sequence when you call generate() you obtain and array of values (char or integer).
If you use implode() , when you call generate() you obtain a string with values separated by ;
Add asString() method , that: when you call generate you obtain a string with values concatenated without any separator.
For example calling:

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

You could obtain something like this:

"pks3220x8w"

examples: create an example for src/Models/FloatModel.php

In examples directory create a new php file, named : RandomInteger.php

In this file, I would like to:

  • include autoload
  • use the class needed (HiFolks\RandoPhp\Randomize)
  • write the call to generate() method from Randomize class with integer

I would like to show how to use:

  • Generate float with min and max ( something like: Randomize::float()->min(0)->max(90)->generate() );
  • Generate float with range ( something like: Randomize::float()->range(0.5,1.5)->generate() );
  • Generate float using the default ( something like Randomize::float()->generate() ) explaining that the default are 0.0 and 1.0;

My suggestion is to take a look from: examples/RandomInteger.php file

[Feature request] Random expression parser

Let's say I have a user provided string which contains some placeholder expressions which the library has to parse and generate random expressions based on user input.

Let's say the user provides this input:

Lorem __rand_range(1, 4) dolor sit amet, __rand_int(10) adipiscing elit. Ut nisi libero, luctus id iaculis vel, __rand_str(20) non eros. Morbi porttitor velit id leo __rand_date() vestibulum. Quisque egestas __rand_chr(3) nibh. Proin maximus eros ut magna pellentesque __rand_float(). Vivamus rhoncus nulla id tellus __rand_byte(3) pharetra. Cras scelerisque libero a maximus egestas. Duis at sapien sed augue gravida pellentesque. Cras interdum volutpat porttitor. Vivamus a auctor erat.

The parser should be able to read the input and swap the placeholder expressions with the appropriate random value then return the parsed string.

To use it:
Randomize::expression()->parse($input_string)->generate();

Add special characters preset to Char Model

In the Char model we hav some preset (for mapping int ascii code to char)for:

  • low case letter,
  • up case letter,
  • digit.

Add preset for special characters:

  • 33-47 : !"#$%&'()*+,-./
  • 58-64 : :;<=>?@
  • 91-96 : []^_`
  • 123-126 : {|}~

Cannot call method max() on int

Resolving conflict name Integer in Randomize.php
If you run phpstan with level 2 you have this kind of Issue:

 ------ ----------------------------------
  Line   Models/Sequence.php
 ------ ----------------------------------
  105    Cannot call method max() on int.
 ------ ----------------------------------

This is because in Randomize even you you are specifing the name space (
HiFolks\RandoPhp\Models\Integer ) some tools like phpstan and / or PhpStorm raises a warning because they are using Integer as \Interger and not HiFolks\RandoPhp\Models\Integer.

So the first solution that i have, is to use and alias when we refer to Integer class, for example:

use HiFolks\RandoPhp\Models\Integer as ModelInt;

and in public static function integer , return a ModelInt instead of Integer.

Probably someone has a better solution... Better solutions are welcome!

Create Latitude Longitude model

Create new Model class for generating radom Latitude / Longitude coordinates (with decimals).
Start from src/Models/Float.php as "template"

  • file: src/Models/LatLong.php
  • class: LatLong
  • method array() : you want to generate an array with lat and long like: [41.890242, 12.492326];
  • method object() : you want to generate an object like { latitude: 41.890242, longitude: 12.492326];
  • method justLatitude() : you want to generate only latitude value like 41.890242;
  • method justLongitude() : you want to generate only longitude value like 12.492326;
  • namespace: HiFolks\RandoPhp\Models
  • generate: genrate a Latitude/Longitude array number;

Example:

$latlong = Randomize::latlong()->generate();

It generates something like this: [41.890242, 12.492326]

Suggestion:

  • use Float model to generate random float;
  • considering that latitude is -90 to 90
  • considering that longitude is -180 to 180

Documentation: in readme file add random Datetime usage example

Add a section in readme file in order to explain Datetime usage.
You can start from example files:
https://github.com/Hi-Folks/rando-php/blob/master/examples/RandomChar.php

Explain 3 scenarios:

  • Generate random datetime with dafault (min: first day of the current year, max: last day of the current year):
Randomize::datetime()->generate();
  • Generate random datetime with specific range:
Randomize::datetime()->min('01-01-2020')->max('10-01-2020')->generate();
  • Generate randon datetime with a specific format:
Randomize::datetime()->format('d-M-Y')->generate()

Documentation: in readme file add range() example

IN the readme file, in the "Generate an Integer" section after this example:

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

add an example with usage with range(1,6) instead of min() and max().
Add also a description line for range() method usage.
From phpdoc:

     * Set the range (min and max)
     * Calling range(1,10), it is equivalent of ->min(1)->max(10)

Randomize::chars()

Create static method chars() for Randomize class that:

  • Create the sequence : Randomize::sequence()->chars()
  • has a optional parameter for counts ( for implicit ->counts() )
  • has implicit : asString()

So, for example:

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

could be:

Randomize::chars(10)->generate();

Or

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

Could be:

Randomize::chars(20)->alphaLowerCase()->generate();

Model: create DateTime Model

Create a new model src/Models/DateTime.php in order to generate a random datetime.
Implement fluent interface (see src/Models/Integer.php) with:

  • min : minimum date (date from);
  • max : max date (date to).

The goal is to implement a functionality like this:

Randomize::datetime()->generate();
Randomize::datetime()->min($startDate)->max($endDate)->generate();

DateTime Model, max / min default

In DateTime model set default for min and max. Now it the min and max is hardcoded in attribute declaration.
File: src/Models/DateTime.php

  • min the first day of the current year
  • max the last day of the current year

Suggestion:

  • see mktime for creating timestamps in int format.

Fix phpdoc in Sequence class, generate() method

In generate() method we have a phpdoc that says the return value is only array.
There is one scenario (implode) that the method could return a string.

Fix the phpdoc in order to return array|string and not only array

The exact issue is:

Method HiFolks\RandoPhp\Models\Sequence::generate() should return array but returns string.

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.