A collection of common libraries for PHP 7.1+.
Use Composer to install php7-common into your project.
composer require nonamephp/php7-common
Noname\ArrNoname\CollectionNoname\StrNoname\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' => '[email protected]'
];
$collection = new Collection($userData);
// output: '[email protected]'
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' => '[email protected]'
];
// 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);
}*,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.
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 falseSimilar 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 falseNote: These methods are case-sensitive. If you are having issues it is recommended that you use is() instead.