Skip to content

Commit 1388767

Browse files
committed
Merge branch 'release/0.2.1'
2 parents 02d29a8 + 0a6911a commit 1388767

File tree

3 files changed

+433
-90
lines changed

3 files changed

+433
-90
lines changed

README.md

Lines changed: 162 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,174 @@ Status](https://travis-ci.org/nonamephp/php7-common.svg?branch=master)](https://
44
php7-common
55
=============
66

7-
A collection of common libraries for PHP 7
7+
_This project is in development and is not recommended for use in production environments_
88

9-
#### \Noname\Common\Collection
9+
A collection of common libraries for PHP 7.
1010

11-
##### Methods
11+
## What's in the box?
1212

13-
* `__construct(array $data)` Create Collection
14-
* `set($key, $value)` Add an item
15-
* `get($key, $default = null)` Get value of item; Returns $default if item doesn't exist
16-
* `has($key) : bool` Check if item exists
17-
* `is($key, $value, $operater = null)` Compare value of an item
18-
* `pluck($key, $default = null)` Pluck an item from the collection; Returns $default if item doesn't exist
19-
* `delete($key)` Delete an item
20-
* `destroy()` Remove all items
21-
* `count()` Get count of items
22-
* `keys() : array` Get item keys
23-
* `values() : array` Get item values
24-
* `all() : array` Alias for `toArray()`
25-
* `toArray() : array` Returns collection as an array
13+
### `\Noname\Common\Collection`
2614

27-
#### \Noname\Common\Validator
15+
Create a `Collection` with an associative array to provide helpful methods for working with your data.
16+
17+
`Collection` implements the following interfaces: `Countable`, `ArrayAccess`, `IteratorAggregate`, `Serializable`, `JsonSerializable`
18+
19+
#### Collection Methods
20+
21+
##### `__construct(array $items = [])`
22+
23+
Create instance of `Collection`.
24+
25+
##### `set(string $key, mixed $value)`
26+
27+
Add an item to the collection. If `$key` already exists in the collection it will be overwritten.
28+
29+
##### `get(string $key, mixed $default = null)`
30+
31+
Return the value of an item from the collection. If `$key` doesn't exist in the collection then `$default` will be returned.
32+
33+
##### `has(string $key) : bool`
34+
35+
Check if the collection has an item with same `$key`.
36+
37+
##### `is(string $key, mixed $value, mixed $operater = null)`
38+
39+
Compare an item's value against `$value`. By default, the method will check if the item's value is equal to `$value`.
40+
Optionally, you may supply an `$operator` to change the comparison logic.
41+
42+
Supported `$operator` values: `=`, `==`, `===`, `>`, `>=`, `<`, `<=`, `<>`, `!=`
43+
44+
##### `pluck(string $key, mixed $default = null)`
45+
46+
Pluck an item from the collection. If `$key` doesn't exist in the collection then `$default` will be returned.
47+
48+
##### `delete(string $key)`
49+
50+
Remove an item from the collection.
51+
52+
##### `destroy()`
53+
54+
Delete all items in the collection.
55+
56+
##### `count()`
57+
58+
Returns the count of all of the items in the collection.
59+
60+
##### `keys() : array`
61+
62+
Returns an array containing keys for all of the items in the collection.
63+
64+
###### `values() : array`
65+
66+
Returns an array containing values for all of the items in the collection.
67+
68+
###### `all() : array`
69+
70+
Alias for `toArray()`.
71+
72+
###### `toArray() : array`
73+
74+
Returns all items in the collection as an associative array.
75+
76+
### \Noname\Common\Validator
77+
78+
Use `Validator` to validate your data based on a set of rules.
79+
80+
##### Basic Example
2881

2982
<?php
30-
use \Noname\Common\Validator;
31-
$values = ['email' => '[email protected]'];
32-
$rules = ['email' => 'email'];
33-
$validator = new Validator($values, $rules);
34-
$valid = $validator->validate();
35-
if(!$valid){
36-
print_r($validator->getErrors());
83+
use Noname\Common\Validator;
84+
85+
// $data must be an associative array of user input
86+
$data = [
87+
'customer_id' => 100,
88+
'customer_email' => '[email protected]'
89+
];
90+
91+
// Define a rule for each field you want to validate
92+
$rules = [
93+
'customer_id' => 'int', // customer_id MUST be an integer
94+
'customer_email' => 'email' // customer_email MUST be an email address
95+
];
96+
97+
// Create Validator
98+
$validator = new Validator($data, $rules);
99+
100+
// Validate the data based on the rules
101+
if(!$validator->validate()){
102+
// getErrors() will return an array of validation errors
103+
$errors = $validator->getErrors();
104+
// handle errors
37105
}
106+
107+
#### Built-in Validation Types
108+
109+
* `null` Validate that value is null
110+
* `bool`, `boolean` Validate that value is boolean
111+
* `scalar` Validate that value is scalar (integer, float, string or boolean)
112+
* `str`, `string` Validate that value is string
113+
* `num`, `numeric` Validate that value is numeric
114+
* `int`, `integer` Validate that value is integer
115+
* `float`, `double` Validate that value is float/double
116+
* `alnum`, `alphanumeric` Validate that value is alpha-numeric only
117+
* `alpha` Validate that value is alpha only
118+
* `arr`, `array` Validate that value is array
119+
* `obj`, `object` Validate that value is object
120+
* `closure` Validate that value is instance of `\Closure`
121+
* `callable` Validate that value is callable
122+
* `email` Validate that value is valid email address
123+
* `ip` Validate that value is either of IPv4 or IPv6
124+
* `ipv4` Validate that value is IPv4
125+
* `ipv6` Validate that value is IPv6
126+
127+
#### Validator Methods
128+
129+
##### `__construct(array $values, array $rules)`
130+
131+
Create instance of `Validator`.
132+
133+
##### `validate() : bool`
134+
135+
Validate the data based on the rules.
136+
137+
##### `hasErrors() : bool`
38138

39-
##### Methods
139+
Checks if validation has any errors.
40140

41-
* `__construct(array $data, array $rules, array $settings = [])` Create Validator
42-
* $data : array
43-
* $rules : array
44-
* $settings : array
45-
* `validate() : bool` Validate data based on supplied rules
46-
* `hasErrors() : bool` Check if validator has errors
47-
* `getErrors() : array` Return validation errors
48-
* `validateType($type, $value, array $rule = []) : bool` Type-specific validator
141+
##### `getErrors() : array`
142+
143+
Returns an array of validation errors.
144+
145+
146+
###### `static is($type, $value) : bool`
147+
148+
Static method to check if `$value` is valid `$type`. You can pass any of the built-in validator types for `$type`.
149+
150+
<?php
151+
use Noname\Common\Validator;
152+
153+
Validator::is('string', 'Hello world!');
154+
Validator::is('integer', 100);
155+
156+
##### `static is{Type}($value)`
157+
158+
Similar to `Validator:is()`, except type is passed in the method name.
159+
160+
<?php
161+
use Noname\Common\Validator;
162+
163+
Validator::isString('Hello world!');
164+
Validator::isInteger(100);
165+
166+
It's important to note that the type in the method name MUST start with an uppercased letter.
167+
168+
To provide a quick example:
169+
170+
<?php
171+
use Noname\Common\Validator;
172+
173+
// This is not valid because 'string' starts with lowercased letter.
174+
Validator::isstring('Hello world!');
175+
176+
// This is valid because 'String' starts with uppercased letter.
177+
Validator::isString('Hello world!');

src/Validator.php

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@
66
*
77
* @package Noname\Common
88
* @since 0.2.0
9+
*
10+
* @method static bool is(string $type, mixed $value) Checks if value passes as type
11+
* @method static bool isNull(mixed $value) Checks if value is null
12+
* @method static bool isBool(mixed $value) Checks if value is boolean
13+
* @method static bool isBoolean(mixed $value) Checks if value is boolean
14+
* @method static bool isScalar(mixed $value) Checks if value is scalar
15+
* @method static bool isStr(mixed $value) Checks if value is string
16+
* @method static bool isString(mixed $value) Checks if value is string
17+
* @method static bool isInt(mixed $value) Checks if value is integer
18+
* @method static bool isInteger(mixed $value) Checks if value is integer
19+
* @method static bool isNum(mixed $value) Checks if value is numeric
20+
* @method static bool isNumeric(mixed $value) Checks if value is numeric
21+
* @method static bool isFloat(mixed $value) Checks if value is float
22+
* @method static bool isDouble(mixed $value) Checks if value is double
23+
* @method static bool isAlNum(mixed $value) Checks if value contains only alpha-numeric characters
24+
* @method static bool isAlphaNumeric(mixed $value) Checks if value contains only alpha-numeric characters
25+
* @method static bool isAlpha(mixed $value) Checks if value contains only alpha characters
26+
* @method static bool isArr(mixed $value) Checks if value is an array
27+
* @method static bool isArray(mixed $value) Checks if value is an array
28+
* @method static bool isObj(mixed $value) Checks if value is an object
29+
* @method static bool isObject(mixed $value) Checks if value is an object
30+
* @method static bool isClosure(mixed $value) Checks if value is instance of \Closure
31+
* @method static bool isCallable(mixed $value) Checks if value is callable
32+
* @method static bool isEmail(mixed $value) Checks if value is valid email address
33+
* @method static bool isIP(mixed $value) Checks if value is valid IPv4 or IPv6
34+
* @method static bool isIPv4(mixed $value) Checks if value is valid IPv4
35+
* @method static bool isIPv6(mixed $value) Checks if value is valid IPv6
936
*/
1037
class Validator
1138
{
@@ -19,11 +46,6 @@ class Validator
1946
*/
2047
private $rules;
2148

22-
/**
23-
* @var Collection
24-
*/
25-
private $settings;
26-
2749
/**
2850
* @var Collection
2951
*/
@@ -45,11 +67,10 @@ class Validator
4567
'integer' => 'validateInteger',
4668
'num' => 'validateNumeric',
4769
'numeric' => 'validateNumeric',
48-
'number' => 'validateNumeric',
4970
'float' => 'validateFloat',
5071
'double' => 'validateFloat',
5172
'alnum' => 'validateAlphaNumeric',
52-
'alphaNumeric' => 'validateAlphaNumeric',
73+
'alphanumeric' => 'validateAlphaNumeric',
5374
'alpha' => 'validateAlpha',
5475
'arr' => 'validateArray',
5576
'array' => 'validateArray',
@@ -68,17 +89,50 @@ class Validator
6889
*
6990
* @param array $values
7091
* @param array $rules
71-
* @param array $settings
7292
*/
73-
public function __construct(array $values = [], array $rules = [], array $settings = [])
93+
public function __construct(array $values = [], array $rules = [])
7494
{
7595
// Create collections for datasets
7696
$this->values = new Collection($values);
7797
$this->rules = new Collection($rules);
78-
$this->settings = new Collection($settings);
7998
$this->errors = new Collection();
8099
}
81100

101+
/**
102+
* Magic method to handle calls to undefined static methods.
103+
*
104+
* @param string $method
105+
* @param array $arguments
106+
* @throws \InvalidArgumentException, \BadMethodCallException
107+
* @return bool
108+
*/
109+
public static function __callStatic($method, $arguments)
110+
{
111+
// Split split method name into parts on each uppercased letter
112+
$parts = preg_split('/(?=[A-Z])/', lcfirst($method));
113+
114+
$func = array_shift($parts);
115+
$numArgs = count($arguments);
116+
117+
if($func == 'is'){
118+
if(empty($parts)){
119+
// Handle call to Validator::is($type, $value)
120+
if($numArgs == 2){
121+
list($type, $value) = $arguments;
122+
return (new self())->validateType($type, $value);
123+
}
124+
throw new \InvalidArgumentException("Validator::is() expects exactly 2 parameters, $numArgs parameters were given.");
125+
}else{
126+
// Handle call to Validator::is{Type}($value)
127+
$type = implode('', $parts);
128+
return (new self())->validateType($type, $arguments[0]);
129+
}
130+
}
131+
132+
// Undefined method; Throw exception
133+
throw new \BadMethodCallException("Validator::{$method}() not defined.");
134+
}
135+
82136
/**
83137
* Validate the values based on the rules.
84138
*

0 commit comments

Comments
 (0)