@@ -103,6 +103,81 @@ if ($validation->validateArray($data) === true) {
103103}
104104```
105105
106+ #### Example: Validating Nested Objects with ` Item `
107+
108+ The ` Item ` rule allows you to validate nested objects or associative arrays with specific rules for each key.
109+
110+ ``` php
111+ use PhpDevCommunity\Validator\Validation;
112+ use PhpDevCommunity\Validator\Rules\NotNull;
113+ use PhpDevCommunity\Validator\Rules\Item;
114+ use PhpDevCommunity\Validator\Rules\StringLength;
115+ use PhpDevCommunity\Validator\Rules\Alphabetic;
116+
117+ // Define validation rules for a nested object (e.g., a "person" object)
118+ $validation = new Validation([
119+ 'person' => [new NotNull(), new Item([
120+ 'first_name' => [new NotNull(), new Alphabetic(), (new StringLength())->min(3)],
121+ 'last_name' => [new NotNull(), new Alphabetic(), (new StringLength())->min(3)],
122+ ])]
123+ ]);
124+
125+ // Example data
126+ $data = [
127+ 'person' => [
128+ 'first_name' => 'John',
129+ 'last_name' => 'Doe'
130+ ]
131+ ];
132+
133+ // Validate the data
134+ if ($validation->validateArray($data) === true) {
135+ echo "Person object is valid!";
136+ } else {
137+ $errors = $validation->getErrors();
138+ echo "Validation errors: " . json_encode($errors, JSON_PRETTY_PRINT);
139+ }
140+ ```
141+
142+ #### Example: Validating Arrays of Items with ` Collection `
143+
144+ The ` Collection ` rule is used to validate arrays where each item in the array must satisfy a set of rules.
145+
146+ ``` php
147+ use PhpDevCommunity\Validator\Validation;
148+ use PhpDevCommunity\Validator\Rules\NotEmpty;
149+ use PhpDevCommunity\Validator\Rules\Collection;
150+ use PhpDevCommunity\Validator\Rules\Item;
151+ use PhpDevCommunity\Validator\Rules\NotNull;
152+ use PhpDevCommunity\Validator\Rules\StringLength;
153+
154+ // Define validation rules for a collection of articles
155+ $validation = new Validation([
156+ 'articles' => [new NotEmpty(), new Collection([
157+ new Item([
158+ 'title' => [new NotNull(), (new StringLength())->min(3)],
159+ 'body' => [new NotNull(), (new StringLength())->min(10)],
160+ ])
161+ ])]
162+ ]);
163+
164+ // Example data
165+ $data = [
166+ 'articles' => [
167+ ['title' => 'Article 1', 'body' => 'This is the body of the first article.'],
168+ ['title' => 'Article 2', 'body' => 'Second article body here.']
169+ ]
170+ ];
171+
172+ // Validate the data
173+ if ($validation->validateArray($data) === true) {
174+ echo "Articles are valid!";
175+ } else {
176+ $errors = $validation->getErrors();
177+ echo "Validation errors: " . json_encode($errors, JSON_PRETTY_PRINT);
178+ }
179+ ```
180+
106181#### URL Validation
107182
108183Validate a URL to ensure it is not null and is a valid URL format.
0 commit comments