2017 © Pedro Peláez
 

library args

A PHP 5.2 compatible arguments handling library.

image

lucatume/args

A PHP 5.2 compatible arguments handling library.

  • Sunday, February 11, 2018
  • by theAverageDev
  • Repository
  • 1 Watchers
  • 0 Stars
  • 66,746 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 16 Versions
  • 62 % Grown

The README.md

Args

A library to make argument checking less of a chore., (*1)

Missing type hinting

PHP will not allow type hinting scalar values in functions and methods arguments forcing cycles like this to be added in each function body:, (*2)

if (!is_string($s)){
    throw ...
}
if(strlen($s) > 20 || strlen($s) < 10){
    throw ...
}
if(preg_match($this->name_pattern, $s) === false){
    throw ...
}

The library, still a work in progress aims at making argument check more granular, tested and DRYer ') by introducing a fluent interface.
The check above could become, (*3)

Arg::_($s)->is_string()->length(10, 20)->match($this->name_pattern);

Some array-related functions are there too:, (*4)

Arg::_($a)->count(6, 10)->contains('value1', 'value2')->has_key('key_one');

Each method will throw an Exception if the condition is not satisfied. In the array example if array is ['some', 23] then an exception will be thrown with the message:, (*5)

Array must contain at least 6 elements!

Installation

Download the class and add it to your project or use Composer like, (*6)

require: {
    "lucatume/args": "~0.1"
}

Methods

Each call to the class should begin using the static method _() and passing it the value to check and, optionally, the name that value will be referred to in exceptions, (*7)

Arg::_($value, 'amount');

To actually check the argument against a condition use the assert() method, (*8)

Arg::_($value)->assert($value > 13);

While that's the base this syntax is best used in special cases the clas doesn't cover and the convenient methods provided by the class should be used.
Each method is defined in a positive logic: nothing will happen if the argument matches the expectation, an exception will be thrown otherwise., (*9)

  • is_bool() - checks if value is a boolean, same as is_bool() method
  • is_scalar() - checks if value is a scalar, same as is_scalar() method
  • is_string() - checks if value is a string, same as is_string() method
  • is_numeric() - checks if value is a numeric, same as is_numeric() method
  • is_int() - checks if value is a int, same as is_int() method
  • is_double() - checks if value is a double, same as is_double() method
  • is_float() - checks if value is a float, same as is_float() method
  • is_null() - checks if value is null, same as is_null() method
  • is_resource() - checks if value is a resource, same as is_resource() method
  • is_array() - checks if value is an array, same as is_array() method
  • is_associative_array() - checks if value is an associative array
  • is_object() - checks if value is an object, same as is_object() method
  • else_throw($exception) - throws the specified exception if the checks are failing; $exception can be an object instance or a class name. If the class name ends in Exception then that part of the class name can be omitted (e.g. "NotGoodException" to "NotGood")

Scalar methods

If an argument is a scalar then additional check methods are available:, (*10)

  • at_least($value) - checks if the argument is >= the value
  • at_most($value) - checks if the argument is <= the value
  • greater_than($value) - checks if the argument is > the value
  • less_than($value) - checks if the argument is < the value

String methods

If an argument is a string then additional methods are available, (*11)

  • length($min, [$max]) - checks if the argument is at least $min long and, optionally, at most $max chars long.
  • match($pattern) - checks if the argument matches the given regex patterns

Array methods

If an argument is an array then additional methods are available, (*12)

  • count($min, [$max]) - checks if the argument contains at least $min elements and, optionally, that it contains at most $max elements.
  • has_structure($structure) - checks if the array has exactly the provided strcture, e.g., (*13)

    $structure = array( 'key1' => null, 'key2' => null, 'key3' => array( 'subKey1' => null, 'subKey1' => null ) );, (*14)

    $structure = array( 'key1' => 'some', 'key2' => 'foo', 'key3' => array( 'subKey1' => 12, 'subKey1' => 23 ) );, (*15)

    Arg::_($arr)->has_structure($structure);, (*16)

  • extends_structure($structure) - checks if the array extends the provided strcture, (*17)

  • defaults($defaults) - does not perform a check but merges the argument array with a default set
  • contains($value [,$value] [,$value2]) - checks if the argument contains the specified value(s)
  • has_key($value [,$value] [,$value2]) - checks if the argument contains the specified key(s)

Object methods

  • is_set($property [, $property][, $property] ) - checks if one ore more specified properties are set

The Versions

11/02 2018

dev-master

9999999-dev

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL-2.0 GPL2

The Requires

 

The Development Requires

11/02 2018

1.0.1.1

1.0.1.1

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

21/03 2016

1.0.1

1.0.1.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

13/09 2015

1.0.0

1.0.0.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

13/09 2015

dev-develop

dev-develop

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

17/05 2015

0.1.9

0.1.9.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

07/05 2015

0.1.8

0.1.8.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

20/04 2015

0.1.10

0.1.10.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

20/04 2015

0.1.7

0.1.7.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

10/02 2015

0.1.6

0.1.6.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

10/02 2015

0.1.5

0.1.5.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

01/12 2014

0.1.4

0.1.4.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

08/11 2014

0.1.3

0.1.3.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

08/11 2014

0.1.2

0.1.2.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

08/11 2014

0.1.1

0.1.1.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires

23/10 2014

0.1.0

0.1.0.0

A PHP 5.2 compatible arguments handling library.

  Sources   Download

GPL2

The Requires

 

The Development Requires