Coder Social home page Coder Social logo

php7-common's People

Contributors

cdtweb avatar iamface avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

php7-common's Issues

Static methods to check types in validator

<?php
use Noname\Common\Validator;

// @return bool
if(Validator::isString('Hello World!')){
    echo 'Value is a string';
}

// @return bool
if(Validator::is('string', 'Hello World!')){
    echo 'Value is a string';
}

I imagine that Validator::isString($value) would resolve through the Validator::__callStatic($name, $args) and make a call to new method Validator::is($type, $value).

Validator: Type Inheritance

Allow types to inherit/extend other types. If a type is extended, the value must pass validation for the extended type first and then again for the primary type.

Use the following type rule as an example:

<?php
$validator->addType('email', [
    'extends' => 'string', 
    'validator' => function($value, $rule, $validator){
        return (bool) filter_var($value, FILTER_VALIDATE_EMAIL);
    }
]);

In this example, a value being validated with the email type would first have to pass as a string.

Validator: Allow closure to be used to validate a value

Allow a closure to be used to validate a value.

$name, $value, and $validator (instance of Validator) would be passed to the closure as arguments. Having the validator instance available allows for access to set values/rules and the ability to set custom error messages.

For example:

<?php
use Noname\Common\Validator;

$values = ['user_id' => 100];

$rules = [
    'user_id' => function ($name, $value, $validator) {
        // Load user model 
        $user = UserModel::find($value);
        if (is_null($user)) {
            $validator->setError($name, "Value supplied for $name is not a valid user");
            return false; // validation fails
        }
        return true; // validation passes
    }
];

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

if (!$validator->validate()) {
    $errors = $validator->getErrors();
    // handle errors
}

Array helper library

Need an array helper library that provides useful methods for working with/manipulating arrays.

Requirements:

  • Flatten an array with a custom separator (e.g. array.dot.notation)
<?php
use Noname\Common\Arr;

$array = ['customer' => ['id' => 100, 'email' => '[email protected]', 'is_banned' => false]];

$flatArray = Arr::flatten($array);

echo $flatArray['customer.email']; // Prints out '[email protected]'

Validator: Add support for type[] validation

The validator already allows types like 'string' and 'integer' but it needs to allow types like 'string[]' and 'integer[]', for each data type. The validator would need to validate all values in the array as that type.

For example:

<?php
use Noname\Common\Validator;

$values = ['emails' => ['[email protected]', '[email protected]']];
$rules = ['emails' => 'email[]'];
$validator = new Validator($values, $rules);

The above would function similar to the following, which is how something like this would have to be done for the time being.

<?php
use Noname\Common\Validator;

$emails = ['[email protected]', '[email protected]'];
foreach ($emails as $email) {
    if (!Validator::isEmail($email)) {
        echo "$email is not a valid email address!";
    }
}

Validator: Add values and rules after construct

<?php
use Noname\Common\Validator;

$validator = new Validator();

// Add a single value
$validator->addValue('name', 'John Doe');

// Add multiple values
$validator->addValues([
    'id' => 100, 
    'email' => '[email protected]'
]);

// Add a single rule
$validator->addRule('name', ['type' => 'string']);

// Add multiple rules
$validator->addRules([
    'id' => 'int', 
    'email' => 'email'
]);

Validator: Array dot notation to define rules for data in multi-dimensional array

<?php
use Noname\Common\Validator;

$values = [
    'users' => [
        ['email' => '[email protected]'],
        ['email' => '[email protected]']
    ]
];

$rules = [
    'users.*.email' => 'email'
];

$validator = new Validator($values, $rules);
if (!$validator->validate()) {
    // handle errors
    $errors = $validator->getErrors();
}

In the above example $values consists of an array of users, each with an email property. The goal of the dot notation is to be able to pinpoint which values in $values that need to be validated.

Another thought: In the example above, let's suppose that one of the users has an invalid email address. The error messaging returned by the validator would need to accurately describe which user email was invalid. The error key could be something like users.INDEXOF.email.

Validator: Additional rule parameters for built-in types

String parameters

  • allow_empty : bool Allow an empty string value; Enabled by default
  • allow_null : bool Allow a null value; Disabled by default
  • min_length : int Minimum allowed strlen of value; Ignore by default
  • max_length : int Maximum allowed strlen of value; Ignore by default
  • equals : string Value must be equal to this value
  • in : array Value must be in array
  • pattern: regex Value must match regex pattern

Integer parameters

  • unsigned : bool Only allow unsigned integer values; Disabled by default
  • >: int Value must be greater than this value
  • < : int Value must be less than this value
  • >= : int Value must be greater than or equal to this value
  • <=: int Value must be less than or equal to this value
  • !=: int Value must not be equal to this value
  • equals: int Value must be equal to this value
  • in: array Value must be in array

Array parameters

  • count: intArray must have exactly this many values
  • min_count: int Array must have greater than or equal to this many values
  • max_count: int Array must have less than or equal to this many values
  • allow_empty: bool Allow an empty array; Enabled by default

Basic Example: String Rule

<?php
use Noname\Common\Validator;

$values = [
    'user_name' => 'john.doe'
];

$rules = [
    'user_name' => [
        'type' => 'str', 
        'allow_empty' => false, 
        'min_length' => 3, 
        'max_length' => 255
    ]
];

$validator = new Validator($values, $rules);
if(!$validator->validate()){
    $errors = $validator->getErrors();
    // handle errors
}

Collection: Method to retrieve multiple items by key

In the following example, we would re-use the existing get($key, $default = null) method, changing the first argument to mixed to allow for either a string or an array.

With regards to $default, if an array is passed:

  • Option 1: Return missing keys with $default
  • Option 2: Do not return missing keys, ignore $default
<?php
use Noname\Common\Collection;

$collection = new Collection(['key1' => 'value1', 'key2' => 'value2']);

// Option 1: @return ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'missing_key']
// Option 2: @return ['key1' => 'value1', 'key2' => 'value2']
$values = $collection->get(['key1', 'key2', 'key3'], 'missing_key');

Validator: Allow for custom types to be added to Validator instance

Allow for custom types to be added to Validator and used for type in rule declaration.

For Example:

<?php
use Noname\Common\Validator;

$validator = new Validator();

// As long as type is added prior to validate() being called then it may be used
$validator->addType('my_custom_type', function ($value, $rule, $validator) {
    // @return bool
});

$validator->addValue('name', 'John Doe');
$validator->addRule('name', 'my_custom_type');
// Alternatively: $validator->addRule('name', ['type' => 'my_custom_type']);

$valid = $validator->validate();
var_dump($valid);

Validator: Allow rules to be passed to is() and isType()

We need to be able to pass a rule array to the static type validator methods:

<?php
use \Noname\Common\Validator;

// Currently a third argument is not supported and would be ignored
Validator::is('date', 'January 1, 2016');
Validator::isDate('January 1, 2016');

// Support needs to be added for passing a rule array as the third argument
Validator::is('date', 'January 1, 2016', ['format' => 'Y-m-d']);
Validator::isDate('January 1, 2016', ['format' => 'Y-m-d']);

Validator: Add 'date' type validation

New 'date' type validation for Validator.

<?php
$values = ['date' => '2016-09-01'];
$rules = ['date' => ['type' => 'date', 'format' => 'Y-m-d']];
$validator = new Validator($values, $rules);

String helper library

Methods

  • startsWith(string $string, string $prefix, bool $caseSensitive = true): bool Checks if $string starts with $prefix. Optionally case-insensitive.
  • endsWith(string $string, string $suffix, bool $caseSensitive = true): bool Checks if $string ends with $suffix. Optionally case-insensitive.
  • equals(string $a, string $b, bool $caseSensitive = true): bool Checks if $a is equal to $b. Optionally case-insensitive.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.