@@ -4,45 +4,174 @@ Status](https://travis-ci.org/nonamephp/php7-common.svg?branch=master)](https://
4
4
php7-common
5
5
=============
6
6
7
- A collection of common libraries for PHP 7
7
+ _ This project is in development and is not recommended for use in production environments _
8
8
9
- #### \Noname\Common\Collection
9
+ A collection of common libraries for PHP 7.
10
10
11
- ##### Methods
11
+ ## What's in the box?
12
12
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 `
26
14
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
28
81
29
82
<?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
37
105
}
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 `
38
138
39
- ##### Methods
139
+ Checks if validation has any errors.
40
140
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!');
0 commit comments