-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductRules.php
More file actions
96 lines (71 loc) · 2.4 KB
/
ProductRules.php
File metadata and controls
96 lines (71 loc) · 2.4 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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) 2021 CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace App\Validation;
class ProductRules
{
public function valid_product_name(string $str, ?string &$error = null): bool
{
if (preg_match('/^[a-zA-Z0-9\s]+$/', $str)) {
return true;
}
$error = 'The {field} field must contain only alphabetic characters, numbers, and spaces.';
return false;
}
public function valid_sku(string $str, ?string &$error = null): bool
{
if (preg_match('/^[a-zA-Z0-9-]+$/', $str)) {
return true;
}
$error = 'The {field} field must contain only alphanumeric characters and hyphens.';
return false;
}
public function valid_price(string $str, ?string &$error = null): bool
{
if (is_numeric($str) && (float) $str > 0) {
return true;
}
$error = 'The {field} field must be a positive number.';
return false;
}
public function valid_quantity(string $str, ?string &$error = null): bool
{
if (ctype_digit($str) && (int) $str >= 0) {
return true;
}
$error = 'The {field} field must be a non-negative integer.';
return false;
}
public function valid_category(string $str, ?string &$error = null): bool
{
$validCategories = ['Electronics', 'Books', 'Clothing', 'Home', 'Beauty', 'Toys']; // Example categories
if (in_array($str, $validCategories, true)) {
return true;
}
$error = 'The {field} field must be a valid category.';
return false;
}
public function valid_description(string $str, ?string &$error = null): bool
{
if (strlen($str) >= 10 && strlen($str) <= 1000) {
return true;
}
$error = 'The {field} field must be between 10 and 1000 characters.';
return false;
}
public function valid_image_url(string $str, ?string &$error = null): bool
{
if (filter_var($str, FILTER_VALIDATE_URL) && preg_match('/\.(jpg|jpeg|png|gif)$/', $str)) {
return true;
}
$error = 'The {field} field must be a valid image URL.';
return false;
}
}