|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2019-2025 |
|
6
|
|
|
* @package MShop |
|
7
|
|
|
* @subpackage Common |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\MShop\Common\Helper\Config; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Default implementation of the config helper item |
|
16
|
|
|
* |
|
17
|
|
|
* @package MShop |
|
18
|
|
|
* @subpackage Common |
|
19
|
|
|
*/ |
|
20
|
|
|
class Standard implements \Aimeos\MShop\Common\Helper\Config\Iface |
|
21
|
|
|
{ |
|
22
|
|
|
private array $criteria; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Initializes the object with the criteria objects to check against |
|
27
|
|
|
* |
|
28
|
|
|
* @param \Aimeos\Base\Criteria\Attribute\Iface $criteria Criteria attribute objects |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct( array $criteria ) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->criteria = $criteria; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Checks required fields and the types of the config array |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $map Values to check agains the criteria |
|
40
|
|
|
* @return array An array with the attribute keys as key and an error message as values for all attributes that are |
|
41
|
|
|
* known by the provider but aren't valid resp. null for attributes whose values are OK |
|
42
|
|
|
*/ |
|
43
|
|
|
public function check( array $config ) : array |
|
44
|
|
|
{ |
|
45
|
|
|
$errors = []; |
|
46
|
|
|
|
|
47
|
|
|
foreach( $this->criteria as $key => $attr ) |
|
48
|
|
|
{ |
|
49
|
|
|
if( $attr->isRequired() === true && ( !isset( $config[$key] ) || $config[$key] === '' ) ) { |
|
50
|
|
|
$errors[$key] = sprintf( 'Configuration for "%1$s" is missing', $key ); continue; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if( isset( $config[$key] ) ) |
|
54
|
|
|
{ |
|
55
|
|
|
switch( $attr->getType() ) |
|
56
|
|
|
{ |
|
57
|
|
|
case 'bool': |
|
58
|
|
|
case 'boolean': |
|
59
|
|
|
if( !in_array( $config[$key], ['', '0', '1'] ) ) { |
|
60
|
|
|
$errors[$key] = sprintf( 'Not a true/false value' ); continue 2; |
|
61
|
|
|
} |
|
62
|
|
|
break; |
|
63
|
|
|
case 'string': |
|
64
|
|
|
case 'text': |
|
65
|
|
|
if( !is_string( $config[$key] ) && !is_numeric( $config[$key] ) ) { |
|
66
|
|
|
$errors[$key] = sprintf( 'Not a string' ); continue 2; |
|
67
|
|
|
} |
|
68
|
|
|
break; |
|
69
|
|
|
case 'int': |
|
70
|
|
|
case 'integer': |
|
71
|
|
|
if( !is_integer( $config[$key] ) && !( is_string( $config[$key] ) && ctype_digit( $config[$key] ) ) ) { |
|
72
|
|
|
$errors[$key] = sprintf( 'Not an integer number' ); continue 2; |
|
73
|
|
|
} |
|
74
|
|
|
break; |
|
75
|
|
|
case 'number': |
|
76
|
|
|
if( !is_numeric( $config[$key] ) ) { |
|
77
|
|
|
$errors[$key] = sprintf( 'Not a number' ); continue 2; |
|
78
|
|
|
} |
|
79
|
|
|
break; |
|
80
|
|
|
case 'date': |
|
81
|
|
|
$pattern = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$/'; |
|
82
|
|
|
if( !is_string( $config[$key] ) || preg_match( $pattern, $config[$key] ) !== 1 ) { |
|
83
|
|
|
$errors[$key] = sprintf( 'Not a date' ); continue 2; |
|
84
|
|
|
} |
|
85
|
|
|
break; |
|
86
|
|
|
case 'datetime': |
|
87
|
|
|
$pattern = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9](:[0-5][0-9])?$/'; |
|
88
|
|
|
if( !is_string( $config[$key] ) || preg_match( $pattern, $config[$key] ) !== 1 ) { |
|
89
|
|
|
$errors[$key] = sprintf( 'Not a date and time' ); continue 2; |
|
90
|
|
|
} |
|
91
|
|
|
break; |
|
92
|
|
|
case 'time': |
|
93
|
|
|
$pattern = '/^([0-2])?[0-9]:[0-5][0-9](:[0-5][0-9])?$/'; |
|
94
|
|
|
if( !is_string( $config[$key] ) || preg_match( $pattern, $config[$key] ) !== 1 ) { |
|
95
|
|
|
$errors[$key] = sprintf( 'Not a time' ); continue 2; |
|
96
|
|
|
} |
|
97
|
|
|
break; |
|
98
|
|
|
case 'list': |
|
99
|
|
|
case 'select': |
|
100
|
|
|
$default = (array) $attr->getDefault(); |
|
101
|
|
|
if( !empty( $default ) && !isset( $default[$config[$key]] ) && !in_array( $config[$key], $default ) ) { |
|
102
|
|
|
$errors[$key] = sprintf( 'Not a listed value' ); continue 2; |
|
103
|
|
|
} |
|
104
|
|
|
break; |
|
105
|
|
|
case 'map': |
|
106
|
|
|
if( !is_array( $config[$key] ) ) { |
|
107
|
|
|
$errors[$key] = sprintf( 'Not a key/value map' ); continue 2; |
|
108
|
|
|
} |
|
109
|
|
|
break; |
|
110
|
|
|
default: |
|
111
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid type "%1$s"', $attr->getType() ) ); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$errors[$key] = null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $errors; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|