PHP code example of nonamephp / php7-common

1. Go to this page and download the library: Download nonamephp/php7-common library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nonamephp / php7-common example snippets



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;
});


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');


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);
}


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);
}


use Noname\Validator;

Validator::is('string', 'Hello world!'); // @return true
Validator::is('integer', 'Hello world!'); // @return false


use Noname\Validator;

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