PluginProbe
Billingo Official for WooCommerce / 4.2.9
Billingo Official for WooCommerce v4.2.9
4.3.3 4.3.2 trunk 0.0.2 1.0.0 2.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 All 82 releases
billingo / src / Validation / RuleConfigurator.php

RuleConfigurator.php in Billingo Official for WooCommerce 4.2.9, at src/Validation/RuleConfigurator.php

71 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace App\Billingo\Validation;
4
5 use Valitron\Validator as Valitron;
6
7 class RuleConfigurator
8 {
9 private static ?self $instance = null;
10
11 private function __construct()
12 {
13 $this->initRules();
14 }
15
16 public static function getInstance(): self
17 {
18 if (self::$instance === null) {
19 self::$instance = new self();
20 }
21
22 return self::$instance;
23 }
24
25 private function initRules(): void
26 {
27 Valitron::addRule('string', function ($field, $value, array $params, array $fields) {
28
29 return is_string($value);
30 }, 'must be a string');
31
32 Valitron::addRule('instanceOfAny', function ($field, $value, array $params, array $fields) {
33 if (!is_object($value)) {
34
35 return false;
36 }
37
38 foreach ($params[0] as $class) {
39 if ($value instanceof $class) {
40
41 return true;
42 }
43 }
44
45 return false;
46 }, 'must be an instance of one of the specified classes');
47
48 Valitron::addRule('regex', function ($field, $value, array $params, array $fields) {
49
50 if (is_null($value)) {
51
52 return false;
53 }
54
55 return preg_match($params[0], $value);
56 }, 'contains invalid characters');
57
58 Valitron::addRule('dateFormat', function ($field, $value, array $params, array $fields) {
59
60 if (is_null($value)) {
61
62 return false;
63 }
64
65 $parsed = date_parse_from_format($params[0], $value);
66
67 return $parsed['error_count'] === 0 && $parsed['warning_count'] === 0;
68 });
69 }
70 }
71