1
|
|
|
<?php namespace CMPayments\SchemaValidator\Validators; |
2
|
|
|
|
3
|
|
|
use CMPayments\Cache\Cache; |
4
|
|
|
use CMPayments\SchemaValidator\BaseValidator; |
5
|
|
|
use CMPayments\SchemaValidator\Exceptions\ValidateException; |
6
|
|
|
use CMPayments\SchemaValidator\Exceptions\ValidateSchemaException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ErrorTrait |
10
|
|
|
* |
11
|
|
|
* @package CMPayments\SchemaValidator\Validators |
12
|
|
|
* @Author Boy Wijnmaalen <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
trait ErrorTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var null|Cache |
18
|
|
|
*/ |
19
|
|
|
protected $cache; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $errors = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $stringifiedOrdinals = ['th', 'st', 'nd', 'rd']; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $prepositionDefault = 'a'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $prepositions = [ |
40
|
|
|
BaseValidator::_ARRAY => 'an', |
41
|
|
|
BaseValidator::BOOLEAN => 'a', |
42
|
|
|
BaseValidator::CLOSURE => 'a', |
43
|
|
|
BaseValidator::INTEGER => 'an', |
44
|
|
|
BaseValidator::NUMBER => 'a', |
45
|
|
|
BaseValidator::OBJECT => 'an', |
46
|
|
|
BaseValidator::STRING => 'a', |
47
|
|
|
BaseValidator::_NULL => '' |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function getErrors() |
54
|
|
|
{ |
55
|
|
|
return $this->errors; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param int $code |
60
|
|
|
* @param array $args |
61
|
|
|
*/ |
62
|
|
|
protected function addError($code, array $args = []) |
63
|
|
|
{ |
64
|
|
|
$message = (new ValidateException($code, $args))->getMessage(); |
65
|
|
|
$this->errors[] = compact('code', 'args', 'message'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns boolean on whether the JSON is valid or not |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function isValid() |
74
|
|
|
{ |
75
|
|
|
return (count($this->getErrors()) === 0); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the preposition for a specific type |
80
|
|
|
* |
81
|
|
|
* @param $type |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
* @throws ValidateSchemaException |
85
|
|
|
*/ |
86
|
|
|
public function getPreposition($type) |
87
|
|
|
{ |
88
|
|
|
$type = strtolower($type); |
89
|
|
|
|
90
|
|
|
if (!isset($this->prepositions[$type])) { |
91
|
|
|
|
92
|
|
|
// output exception when $this->config['debug'] === true |
|
|
|
|
93
|
|
|
if (($this->cache instanceof Cache) && $this->cache->getDebug()) { |
94
|
|
|
|
95
|
|
|
throw new ValidateSchemaException(ValidateSchemaException::ERROR_INPUT_IS_NOT_A_VALID_PREPOSITION, $type); |
|
|
|
|
96
|
|
|
} else { |
97
|
|
|
return $this->prepositionDefault; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->prepositions[$type]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns a valid conjugation of the verb 'to be' |
106
|
|
|
* |
107
|
|
|
* @param integer $count |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function conjugationToBe($count) |
112
|
|
|
{ |
113
|
|
|
// 3rd singular or 3rd plural |
114
|
|
|
return ($count === 1) ? 'is' : 'are'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns a valid representation of 'items' (or other value) |
119
|
|
|
* |
120
|
|
|
* @param integer $count |
121
|
|
|
* @param string $single |
122
|
|
|
* @param string $plural |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function conjugationObject($count, $single = 'item', $plural = 'items') |
127
|
|
|
{ |
128
|
|
|
return ($count === 1) ? $single : $plural; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Converts a number to a ordinal |
133
|
|
|
* |
134
|
|
|
* @param int $number |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function numberToOrdinal($number) |
139
|
|
|
{ |
140
|
|
|
if (in_array(($lastDigit = (int)substr($number, -1)), [1, 2, 3])) { |
141
|
|
|
|
142
|
|
|
return $number . $this->stringifiedOrdinals[$lastDigit]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $number . $this->stringifiedOrdinals[0]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.