nonamephp / php7-common Goto Github PK
View Code? Open in Web Editor NEWA collection of common libraries for PHP 7
License: MIT License
A collection of common libraries for PHP 7
License: MIT License
<?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)
.
Look into using Faker (https://github.com/fzaninotto/Faker) to generate data for Validator unit tests.
Add starts_with
, ends_with
, and contains
rule params to string type validator.
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.
The label would be used instead the field name within error messages.
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
}
Need an array helper library that provides useful methods for working with/manipulating arrays.
Requirements:
<?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]'
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!";
}
}
<?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'
]);
Add validation types of resource, stream, dir, and file.
For dir and file add the following rule params:
<?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
.
Add getDelete()
method to Collection. Method will function similarly to get()
except for the item will also be deleted from the collection.
Add new method to Str.
contains(string $string, string $search, bool $caseSensitive = true)
The following functions can be used:
Investigate adding toArray(string $string)
to Str.
This method could simply wrap str_split()
.
Move "What's in the box?" from README to the wiki and add usage guides/examples for each of the libraries.
bool
Allow an empty string value; Enabled by defaultbool
Allow a null value; Disabled by defaultint
Minimum allowed strlen
of value; Ignore by defaultint
Maximum allowed strlen
of value; Ignore by defaultstring
Value must be equal to this valuearray
Value must be in arrayregex
Value must match regex patternbool
Only allow unsigned integer values; Disabled by defaultint
Value must be greater than this valueint
Value must be less than this valueint
Value must be greater than or equal to this valueint
Value must be less than or equal to this valueint
Value must not be equal to this valueint
Value must be equal to this valuearray
Value must be in arrayint
Array must have exactly this many valuesint
Array must have greater than or equal to this many valuesint
Array must have less than or equal to this many valuesbool
Allow an empty array; Enabled by default<?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
}
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:
$default
$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');
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);
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']);
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);
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.A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.