nonamephp / php7-common
A collection of common PHP 7 libraries
Installs: 315
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 0
Open Issues: 0
pkg:composer/nonamephp/php7-common
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: 3.2.*
README
php7-common
A collection of common libraries for PHP 7.1+.
Installation
Use Composer to install php7-common into your project.
composer require nonamephp/php7-common
Included Libraries
Noname\ArrNoname\CollectionNoname\StrNoname\Validator
\Noname\Arr
A helper library for working with arrays.
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.
static dot(array $array) : array
Alias for Arr::flatten() that always uses a dot (.) separator.
static each(array $array, callable $callable) : array
Recursively assign the callable's return value to each array item. Array keys are preserved.
<?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.
Collection implements the following interfaces: Countable, ArrayAccess, IteratorAggregate, Serializable, JsonSerializable
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.
static make(...$arrays) : Collection
Make a collection from one or more arrays.
set(string $key, mixed $value) : void
Add an item to the collection. If $key already exists in the collection it will be overwritten.
get(string|array $key, mixed $default = null) : mixed
Get an item from the collection. Returns $default if item not found.
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.
has(string $key) : bool
Check if the collection has an item with same $key.
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.
Supported $operator values: =, ==, ===, >, >=, <, <=, <>, !=
Note: = and == are the same, but === is will perform a strict comparison. <> and != are the same.
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.
delete(string $key) : void
Remove an item from the collection.
destroy() : void
Remove all items from the collection.
count() : int
Returns the count of all of the items in the collection.
keys() : array
Returns an array containing the keys of all of the items in the collection.
values() : array
Returns an array containing the values of all of the items in the collection.
all() : array
Alias for toArray().
toArray() : array
Returns an array of all of the items in the collection.
toJson() : string
Returns collection as JSON.
flatten() : array
Flatten all of the items in the collection using dot (.) notation.
\Noname\Str
A helper library for working with strings.
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.
static endsWith(string $string, string $suffix, bool $caseSensitive = true) : bool
Checks if string ends with given prefix. By default this method is case-sensitive.
static equals(string $a, string $b, bool $caseSensitive = true) : bool
Checks if two strings equal each other. By default this method is case-sensitive.
static contains(string $string, string $search, bool $caseSensitive = true) : bool
Checks if string contains another string. By default this method is case-sensitive.
static toArray(string $string) : array
Splits a string into an array containing each character.
\Noname\Validator
Use Validator to validate your data based on a set of rules.
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
*,anyAlways pass validation for any data typenullValidate that value is nullbool,booleanValidate that value is booleanscalarValidate that value is scalar (integer, float, string or boolean)str,stringValidate that value is stringnum,numericValidate that value is numericint,integerValidate that value is integerfloat,doubleValidate that value is float/doublealnum,alphanumericValidate that value only contains alpha-numeric charactersalphaValidate that value only contains alpha charactersarr,arrayValidate that value is arrayobjectValidate that value is objectcallableValidate that value is callableemailValidate that value is a valid email addressipValidate that value is either of IPv4 or IPv6ipv4Validate that value is IPv4ipv6Validate that value is IPv6date,datetimeValidate that value is date/datetimeresourceValidate that value is a resourcestreamValidate that value is a streamdir,directoryValidate that value is a directoryfileValidate that value is a file
Hint: Adding [] to any type (e.g. int[]) will validate an array of values.
Validator Methods
__construct(array $values, array $rules) : Validator
Create an instance of Validator.
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.
<?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.
addValue(string $name, mixed $value) : void
Add value to dataset that is to be validated.
addValues(array $values) : void
Add multiple values to dataset that is to be validated.
values() : array
Returns an array of the values that are set to be validated.
addRule(string $name, mixed $rule) : void
Add a rule to the validator.
addRules(array $rules) : void
Add multiple rules to the validator.
rules() : array
Returns an array of the rules that are set for validation.
validate() : bool
Validate the set data based on the set rules.
hasErrors() : bool
Checks if any errors were set during validation.
getErrors() : array
Returns an array of the errors that were set during validation.
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.
This method is useful when validating a single value.
<?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.
<?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.