drupol/yaroc

YAROC: Yet Another Random.Org Client.

1.0.0 2017-02-04 08:11 UTC

README

Build Status Codacy Badge Codacy Badge Scrutinizer Code Quality Versioneye Status

Yet Another Random.Org Client.

YAROC fully supports V1 and V2 API.

Most of the classes of this library are stateless and immutable.

Requirements

  • PHP >= 7.0,
  • A PSR-7 http client (Guzzle library or any other equivalent),

Installation

The first step to use yaroc is to install the dependencies with Composer:

$ composer install

Or if you need it in an existent project, then run the following command to install the dependencies:

$ composer require drupol/yaroc php-http/guzzle6-adapter

Why do we need php-http/guzzle6-adapter ?

We are decoupled form any HTTP messaging client with help by HTTPlug. You may use any other HTTP client library that support PSR-7.

Usage

First request an API Key or use the temporary key.

The temporary API key used in the examples will be disabled when the beta ends.

You can call any API methods described in the documentation from Random.org.

Currently support all the Random.org API methods in the basic and signed APIs.

Examples


<?php

require 'vendor/autoload.php';

$generateIntegers = \drupol\Yaroc\Plugin\Provider::withResource('generateIntegers')
    ->withParameters(['n' => 10, 'min' => 0, 'max' => 100]);

$result = (new drupol\Yaroc\RandomOrgAPI())
    ->withApiKey('00000000-0000-0000-0000-000000000000')
    ->getData($generateIntegers);

print_r($result);

$provider = \drupol\Yaroc\Plugin\Provider::withResource('generateStrings')
    ->withParameters([
        'n' => 10,
        'length' => 15,
        'characters' => implode(array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9))),
    ]);

$result = (new drupol\Yaroc\RandomOrgAPI())
    ->getData($provider);

print_r($result);

// To use the upcoming version 2 of the random.org's API:

$result = (new drupol\Yaroc\RandomOrgAPI())
    ->withEndPoint('https://api.random.org/json-rpc/2/invoke')
    ->getData($provider);

print_r($result);

Providing the API key can be accomplished using a .env file. Copy the .env.dist file into .env and modify the latter accordingly.

Third party libraries integration

ircmaxell/RandomLib integration

YAROC provides a Source for ircmaxell/RandomLib.

<?php

require 'vendor/autoload.php';

$randomLib = new RandomLib\Factory();
$generator = $randomLib->getGenerator(new SecurityLib\Strength(SecurityLib\Strength::HIGH))
  ->addSource(new \drupol\Yaroc\Plugin\RandomLib\Source\RandomOrg());
$randomString = $generator->generateString(10);

echo $randomString;

rchouinard/rych-random integration

YAROC provides a Generator for rchouinard/rych-random.

<?php

require 'vendor/autoload.php';

$rychRandom = new Rych\Random\Random(new \drupol\Yaroc\Plugin\RychRandom\Generator\RandomOrg());
$randomString = $rychRandom->getRandomString(8);

echo $randomString;

Tests coverage

Make a copy of the file .env.dist into .env and set your API in it. If you do not have it, the tests will use the temporary API key.

To run the tests, run this command:

composer grumphp

History

I discovered the Random.Org the 22 November 2016, by chance and I found the idea amazing.

I had the idea to build a library that would be following the best development practice and up to date.

Feel free to contact me at: pol.dellaiera@protonmail.com

TODO

  • Documentation
  • Tests coverage