-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
138 lines (116 loc) · 3.72 KB
/
example.php
File metadata and controls
138 lines (116 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use Signalforge\Validation\MessageFormatter;
use Signalforge\Validation\Validator;
// Basic usage
$validator = new Validator([
'email' => ['required', 'email'],
'name' => ['required', 'string', ['min', 2], ['max', 100]],
'age' => ['nullable', 'integer', ['between', 18, 120]],
]);
$result = $validator->validate([
'email' => 'user@example.com',
'name' => 'John Doe',
'age' => 25,
]);
echo "=== Basic Validation ===\n";
echo "Valid: " . ($result->valid() ? 'Yes' : 'No') . "\n";
echo "Validated data: " . json_encode($result->validated()) . "\n\n";
// Validation with errors
$result = $validator->validate([
'email' => 'not-an-email',
'name' => 'J',
]);
echo "=== Validation with Errors ===\n";
echo "Valid: " . ($result->valid() ? 'Yes' : 'No') . "\n";
echo "Errors: " . json_encode($result->errors(), JSON_PRETTY_PRINT) . "\n\n";
// With message formatting
$formatter = MessageFormatter::fromDirectory(__DIR__ . '/messages');
$messages = $formatter->format($result, 'en');
echo "=== Formatted Messages (English) ===\n";
foreach ($messages as $field => $fieldMessages) {
foreach ($fieldMessages as $message) {
echo " - $field: $message\n";
}
}
echo "\n";
$messages = $formatter->format($result, 'hr');
echo "=== Formatted Messages (Croatian) ===\n";
foreach ($messages as $field => $fieldMessages) {
foreach ($fieldMessages as $message) {
echo " - $field: $message\n";
}
}
echo "\n";
// Array validation with wildcards
$validator = new Validator([
'items' => ['required', 'array', ['min', 1]],
'items.*.name' => ['required', 'string'],
'items.*.quantity' => ['required', 'integer', ['min', 1]],
]);
$result = $validator->validate([
'items' => [
['name' => 'Apple', 'quantity' => 5],
['name' => 'Orange', 'quantity' => 3],
],
]);
echo "=== Array Validation ===\n";
echo "Valid: " . ($result->valid() ? 'Yes' : 'No') . "\n";
echo "Validated data: " . json_encode($result->validated(), JSON_PRETTY_PRINT) . "\n\n";
// Conditional validation
$validator = new Validator([
'type' => ['required', ['in', ['personal', 'business']]],
'company_name' => [
['when', ['type', '=', 'business'], [
'required',
'string',
['max', 200],
]],
],
'vat_number' => [
['when', ['type', '=', 'business'], [
'vat_eu',
]],
],
]);
$result = $validator->validate([
'type' => 'business',
'company_name' => 'Acme Corp',
'vat_number' => 'HR12345678903',
]);
echo "=== Conditional Validation ===\n";
echo "Valid: " . ($result->valid() ? 'Yes' : 'No') . "\n";
if (!$result->valid()) {
echo "Errors: " . json_encode($result->errors(), JSON_PRETTY_PRINT) . "\n";
}
echo "\n";
// Croatian-specific validation
$validator = new Validator([
'oib' => ['required', 'oib'],
'phone' => ['required', ['phone', 'HR']],
'iban' => ['required', ['iban', 'HR']],
]);
$result = $validator->validate([
'oib' => '12345678903',
'phone' => '+385 91 1234567',
'iban' => 'HR1234567890123456789',
]);
echo "=== Croatian-Specific Validation ===\n";
echo "Valid: " . ($result->valid() ? 'Yes' : 'No') . "\n";
if (!$result->valid()) {
$messages = $formatter->format($result, 'hr');
foreach ($messages as $field => $fieldMessages) {
foreach ($fieldMessages as $message) {
echo " - $field: $message\n";
}
}
}
echo "\n";
// Static factory method
$result = Validator::make(
['email' => 'user@example.com'],
['email' => ['required', 'email']]
)->validate(['email' => 'user@example.com']);
echo "=== Static Factory ===\n";
echo "Valid: " . ($result->valid() ? 'Yes' : 'No') . "\n";