2017 © Pedro Peláez
 

library php7-common

A collection of common PHP 7 libraries

image

nonamephp/php7-common

A collection of common PHP 7 libraries

  • Tuesday, March 20, 2018
  • by cdtweb
  • Repository
  • 2 Watchers
  • 1 Stars
  • 220 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 24 Versions
  • 11 % Grown

The README.md

Build
Status, (*1)

php7-common

A collection of common libraries for PHP 7.1+., (*2)

Installation

Use Composer to install php7-common into your project., (*3)

composer require nonamephp/php7-common, (*4)

Included Libraries

  • Noname\Arr
  • Noname\Collection
  • Noname\Str
  • Noname\Validator

\Noname\Arr

A helper library for working with arrays., (*5)

Arr Methods

static flatten(array $array, string $separator = '.') : array

Flatten an associative array using a custom separator. This method will use a dot (.) for $separator by defult., (*6)

static dot(array $array) : array

Alias for Arr::flatten() that always uses a dot (.) separator., (*7)

static each(array $array, callable $callable) : array

Recursively assign the callable's return value to each array item. Array keys are preserved., (*8)

<?php
use Noname\Arr;

$values = [1, 2, 3, 4, 5];

// @return [2, 4, 6, 8, 10]
$values_doubled = Arr::each($values, function ($value) {
    return $value * 2;
});

\Noname\Collection

Create a Collection with an associative array to provide helpful methods for working with your data., (*9)

Collection implements the following interfaces: Countable, ArrayAccess, IteratorAggregate, Serializable, JsonSerializable, (*10)

Usage Example

<?php
use Noname\Collection;

$userData = [
    'user_id' => 100,
    'user_name' => 'John Doe',
    'user_email' => 'john.doe@example.org'
];

$collection = new Collection($userData);

// output: 'john.doe@example.org'
echo $collection->get('user_email');

Collection Methods

__construct(array $items = []) : Collection

Creates an instance of Collection. Optionally pass an associative array for $items to prefill the collection with items., (*11)

static make(...$arrays) : Collection

Make a collection from one or more arrays., (*12)

set(string $key, mixed $value) : void

Add an item to the collection. If $key already exists in the collection it will be overwritten., (*13)

get(string|array $key, mixed $default = null) : mixed

Get an item from the collection. Returns $default if item not found., (*14)

Passing an array of item keys for the value of $key will result in multiple items being returned as an array. Keys that are missing from the collection will be returned with a value of $default., (*15)

has(string $key) : bool

Check if the collection has an item with same $key., (*16)

is(string $key, mixed $value, mixed $operater = null) : bool

Compare an item's value against $value. By default, the method will check if the item's value is equal to $value. Optionally, you may supply an $operator to change the comparison logic., (*17)

Supported $operator values: =, ==, ===, >, >=, <, <=, <>, !=, (*18)

Note: = and == are the same, but === is will perform a strict comparison. <> and != are the same., (*19)

pluck(string $key, mixed $default = null) : mixed

Pluck an item from the collection. If $key doesn't exist in the collection then $default will be returned., (*20)

delete(string $key) : void

Remove an item from the collection., (*21)

destroy() : void

Remove all items from the collection., (*22)

count() : int

Returns the count of all of the items in the collection., (*23)

keys() : array

Returns an array containing the keys of all of the items in the collection., (*24)

values() : array

Returns an array containing the values of all of the items in the collection., (*25)

all() : array

Alias for toArray()., (*26)

toArray() : array

Returns an array of all of the items in the collection., (*27)

toJson() : string

Returns collection as JSON., (*28)

flatten() : array

Flatten all of the items in the collection using dot (.) notation., (*29)

\Noname\Str

A helper library for working with strings., (*30)

Str Methods

static startsWith(string $string, string $prefix, bool $caseSensitive = true) : bool

Checks if string starts with given prefix. By default this method is case-sensitive., (*31)

static endsWith(string $string, string $suffix, bool $caseSensitive = true) : bool

Checks if string ends with given prefix. By default this method is case-sensitive., (*32)

static equals(string $a, string $b, bool $caseSensitive = true) : bool

Checks if two strings equal each other. By default this method is case-sensitive., (*33)

static contains(string $string, string $search, bool $caseSensitive = true) : bool

Checks if string contains another string. By default this method is case-sensitive., (*34)

static toArray(string $string) : array

Splits a string into an array containing each character., (*35)

\Noname\Validator

Use Validator to validate your data based on a set of rules., (*36)

Usage Example
<?php
use Noname\Validator;

// Data to be validated
$data = [
    'customer_id' => 100,
    'customer_email' => 'john.doe@example.org'
];

// Validation rules
$rules = [
    'customer_id' => 'int',  // customer_id MUST be an integer
    'customer_email' => 'email' // customer_email MUST be an email address
];

// Create Validator
$validator = new Validator($data, $rules);

// Validate data using rules
// @return bool
$valid = $validator->validate();

if ($valid) {
    echo 'Data passed validation!';
} else {
    $errors = $validator->getErrors();
    print_r($errors);
}

Built-in Validation Types

  • *, any Always pass validation for any data type
  • null Validate that value is null
  • bool, boolean Validate that value is boolean
  • scalar Validate that value is scalar (integer, float, string or boolean)
  • str, string Validate that value is string
  • num, numeric Validate that value is numeric
  • int, integer Validate that value is integer
  • float, double Validate that value is float/double
  • alnum, alphanumeric Validate that value only contains alpha-numeric characters
  • alpha Validate that value only contains alpha characters
  • arr, array Validate that value is array
  • object Validate that value is object
  • callable Validate that value is callable
  • email Validate that value is a valid email address
  • ip Validate that value is either of IPv4 or IPv6
  • ipv4 Validate that value is IPv4
  • ipv6 Validate that value is IPv6
  • date, datetime Validate that value is date/datetime
  • resource Validate that value is a resource
  • stream Validate that value is a stream
  • dir, directory Validate that value is a directory
  • file Validate that value is a file

Hint: Adding [] to any type (e.g. int[]) will validate an array of values., (*37)

Validator Methods

__construct(array $values, array $rules) : Validator

Create an instance of Validator., (*38)

addType(string $typeName, array $typeRule) : void

Add a custom validator type. The following example will add a type of equals_2 which validates that the value is equal to 2 and will set an error otherwise., (*39)

<?php
use Noname\Validator;

// Data to be validated
$values = ['a' => 3];

// Validation rules
$rules = ['a' => ['type' => 'equals_2']];

// Create Validator
$validator = new Validator($values, $rules);

// Add custom 'equals_2' type
$validator->addType('equals_2', [
    'validator' => function ($value, $rule, $validator) {
        $valid = $value === 2;
        if (!$valid) {
            $validator->setError($rule['name'], 'Value does not equal 2');
        }
        return $valid;
    }
]);

// Validate data using rules
// @return bool
$valid = $validator->validate();

if ($valid) {
    echo 'Data passed validation!';
} else {
    $errors = $validator->getErrors();
    print_r($errors);
}

Note: Custom types must be added prior to calling validate() or an InvalidArgumentException will be thrown., (*40)

addValue(string $name, mixed $value) : void

Add value to dataset that is to be validated., (*41)

addValues(array $values) : void

Add multiple values to dataset that is to be validated., (*42)

values() : array

Returns an array of the values that are set to be validated., (*43)

addRule(string $name, mixed $rule) : void

Add a rule to the validator., (*44)

addRules(array $rules) : void

Add multiple rules to the validator., (*45)

rules() : array

Returns an array of the rules that are set for validation., (*46)

validate() : bool

Validate the set data based on the set rules., (*47)

hasErrors() : bool

Checks if any errors were set during validation., (*48)

getErrors() : array

Returns an array of the errors that were set during validation., (*49)

static is(string $type, mixed $value) : bool

Static method to check if $value is valid $type. You can pass any of the built-in validator types for $type., (*50)

This method is useful when validating a single value., (*51)

<?php
use Noname\Validator;

Validator::is('string', 'Hello world!'); // @return true
Validator::is('integer', 'Hello world!'); // @return false
static is{Type}(mixed $value) : bool

Similar to is(), except type is passed in the method name., (*52)

<?php
use Noname\Validator;

Validator::isString('Hello world!'); // @return true
Validator::isInteger('Hello world!'); // @return false

Note: These methods are case-sensitive. If you are having issues it is recommended that you use is() instead., (*53)

The Versions

20/03 2018

dev-master

9999999-dev https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Clint Tyler
by Jared Ivory

collection validator array common php7 string noname

20/03 2018

1.0.0

1.0.0.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Clint Tyler
by Jared Ivory

collection validator array common php7 string noname

28/06 2017

0.3.13

0.3.13.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler
by Jared Ivory

collection validator array common php7 string noname

24/03 2017

0.3.12

0.3.12.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator array common noname nonamephp

14/03 2017

0.3.11

0.3.11.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator array common noname nonamephp

13/03 2017

0.3.10

0.3.10.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator array common noname nonamephp

13/03 2017

0.3.9

0.3.9.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator array common noname nonamephp

12/03 2017

0.3.8

0.3.8.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator array common noname nonamephp

22/02 2017

0.3.7

0.3.7.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator array common noname nonamephp

22/02 2017

0.3.6

0.3.6.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator array common noname nonamephp

03/02 2017

0.3.5

0.3.5.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator array common noname nonamephp

01/02 2017

0.3.4

0.3.4.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator array common noname nonamephp

16/01 2017

0.3.3

0.3.3.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator array common noname nonamephp

14/01 2017

0.3.2

0.3.2.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator common noname nonamephp

22/12 2016

0.3.1

0.3.1.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator common noname nonamephp

22/12 2016

0.3.0

0.3.0.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator common noname nonamephp

06/11 2016

0.2.8

0.2.8.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator common noname nonamephp

03/11 2016

0.2.7

0.2.7.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator common noname nonamephp

02/11 2016

0.2.6

0.2.6.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator common noname nonamephp

01/11 2016

0.2.5

0.2.5.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator common noname nonamephp

01/11 2016

0.2.4

0.2.4.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator common noname nonamephp

01/11 2016

0.2.3

0.2.3.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection validator common noname nonamephp

26/09 2016

0.2.0

0.2.0.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection common noname nonamephp

23/09 2016

0.1.4

0.1.4.0 https://github.com/nonamephp/php7-common

A collection of common PHP 7 libraries

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Clint Tyler

collection common noname nonamephp