A collection of common libraries for PHP 7.1+.
Use Composer to install php7-common
into your project.
composer require nonamephp/php7-common
Noname\Arr
Noname\Collection
Noname\Str
Noname\Validator
A helper library for working with arrays.
Flatten an associative array using a custom separator. This method will use a dot (.) for $separator
by defult.
Alias for Arr::flatten()
that always uses a dot (.) separator.
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;
});
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
<?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');
Creates an instance of Collection
. Optionally pass an associative array for $items
to prefill the collection with items.
Make a collection from one or more arrays.
Add an item to the collection. If $key
already exists in the collection it will be overwritten.
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
.
Check if the collection has an item with same $key
.
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 an item from the collection. If $key
doesn't exist in the collection then $default
will be returned.
Remove an item from the collection.
Remove all items from the collection.
Returns the count of all of the items in the collection.
Returns an array containing the keys of all of the items in the collection.
Returns an array containing the values of all of the items in the collection.
Alias for toArray()
.
Returns an array of all of the items in the collection.
Returns collection as JSON.
Flatten all of the items in the collection using dot (.) notation.
A helper library for working with strings.
Checks if string starts with given prefix. By default this method is case-sensitive.
Checks if string ends with given prefix. By default this method is case-sensitive.
Checks if two strings equal each other. By default this method is case-sensitive.
Checks if string contains another string. By default this method is case-sensitive.
Splits a string into an array containing each character.
Use Validator
to validate your data based on a set of rules.
<?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);
}
*
,any
Always pass validation for any data typenull
Validate that value is nullbool
,boolean
Validate that value is booleanscalar
Validate that value is scalar (integer, float, string or boolean)str
,string
Validate that value is stringnum
,numeric
Validate that value is numericint
,integer
Validate that value is integerfloat
,double
Validate that value is float/doublealnum
,alphanumeric
Validate that value only contains alpha-numeric charactersalpha
Validate that value only contains alpha charactersarr
,array
Validate that value is arrayobject
Validate that value is objectcallable
Validate that value is callableemail
Validate that value is a valid email addressip
Validate that value is either of IPv4 or IPv6ipv4
Validate that value is IPv4ipv6
Validate that value is IPv6date
,datetime
Validate that value is date/datetimeresource
Validate that value is a resourcestream
Validate that value is a streamdir
,directory
Validate that value is a directoryfile
Validate that value is a file
Hint: Adding []
to any type (e.g. int[]
) will validate an array of values.
Create an instance of Validator
.
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.
Add value to dataset that is to be validated.
Add multiple values to dataset that is to be validated.
Returns an array of the values that are set to be validated.
Add a rule to the validator.
Add multiple rules to the validator.
Returns an array of the rules that are set for validation.
Validate the set data based on the set rules.
Checks if any errors were set during validation.
Returns an array of the errors that were set during validation.
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
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.