1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Skeleton module |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\skeleton\handlers; |
11
|
|
|
|
12
|
|
|
use gplcart\core\Config; |
13
|
|
|
use gplcart\core\handlers\validator\Base as BaseValidator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Provides methods to validate Skeleton module data |
17
|
|
|
*/ |
18
|
|
|
class Validator extends BaseValidator |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Config class instance |
23
|
|
|
* @var \gplcart\core\Config $config |
24
|
|
|
*/ |
25
|
|
|
protected $config; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor |
29
|
|
|
* @param Config $config |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Config $config) |
32
|
|
|
{ |
33
|
|
|
parent::__construct(); |
34
|
|
|
|
35
|
|
|
$this->config = $config; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Validates an array of submitted data while creating a skeleton |
40
|
|
|
* @param array $submitted |
41
|
|
|
* @param array $options |
42
|
|
|
* @return boolean|array |
43
|
|
|
*/ |
44
|
|
|
public function skeleton(array &$submitted, array $options = array()) |
45
|
|
|
{ |
46
|
|
|
$this->options = $options; |
47
|
|
|
$this->submitted = &$submitted; |
48
|
|
|
|
49
|
|
|
$this->validateModuleIdSkeleton(); |
50
|
|
|
$this->validateModuleVersionSkeleton(); |
51
|
|
|
$this->validateModuleCoreSkeleton(); |
52
|
|
|
$this->validateModuleAuthorSkeleton(); |
53
|
|
|
|
54
|
|
|
return $this->getResult(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Validates module author name |
59
|
|
|
* @return boolean |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
protected function validateModuleAuthorSkeleton() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$value = $this->getSubmitted('module.author'); |
64
|
|
|
|
65
|
|
|
if (empty($value)) { |
66
|
|
|
$vars = array('@field' => $this->language->text('Author')); |
67
|
|
|
$error = $this->language->text('@field is required', $vars); |
68
|
|
|
$this->setError('module.author', $error); |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Validates a module ID |
77
|
|
|
*/ |
78
|
|
|
protected function validateModuleIdSkeleton() |
79
|
|
|
{ |
80
|
|
|
$value = $this->getSubmitted('module.id'); |
81
|
|
|
|
82
|
|
View Code Duplication |
if (empty($value)) { |
|
|
|
|
83
|
|
|
$vars = array('@field' => $this->language->text('ID')); |
84
|
|
|
$error = $this->language->text('@field is required', $vars); |
85
|
|
|
$this->setError('module.id', $error); |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!$this->config->validModuleId($value)) { |
90
|
|
|
$vars = array('@field' => $this->language->text('ID')); |
91
|
|
|
$error = $this->language->text('@field has invalid value', $vars); |
92
|
|
|
$this->setError('module.id', $error); |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Validates a module version |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
protected function validateModuleVersionSkeleton() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$value = $this->getSubmitted('module.version'); |
105
|
|
|
|
106
|
|
|
if (empty($value)) { |
107
|
|
|
$vars = array('@field' => $this->language->text('Version')); |
108
|
|
|
$error = $this->language->text('@field is required', $vars); |
109
|
|
|
$this->setError('module.version', $error); |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Validates a module core compatibility |
118
|
|
|
*/ |
119
|
|
|
protected function validateModuleCoreSkeleton() |
120
|
|
|
{ |
121
|
|
|
$value = $this->getSubmitted('module.core'); |
122
|
|
|
|
123
|
|
View Code Duplication |
if (empty($value)) { |
|
|
|
|
124
|
|
|
$vars = array('@field' => $this->language->text('Core')); |
125
|
|
|
$error = $this->language->text('@field is required', $vars); |
126
|
|
|
$this->setError('module.core', $error); |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Check if the value starts with a number |
131
|
|
View Code Duplication |
if (preg_match('/^\d/', $value) !== 1) { |
|
|
|
|
132
|
|
|
$vars = array('@field' => $this->language->text('Core')); |
133
|
|
|
$error = $this->language->text('@field has invalid value', $vars); |
134
|
|
|
$this->setError('module.core', $error); |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.