You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -49,15 +50,15 @@ public function addRequirements($requirements) {
49
50
50
51
/**
51
52
* @param string|int $requirement
52
-
* @throws FileQueryException Thrown when there provided requirement is neither a string or an integer
53
+
* @throws FileQueryRequirementsException Thrown when there provided requirement is neither a string or an integer
53
54
* @return bool Return true when added. Returns false when it was already part of the requirements
54
55
*/
55
56
publicfunctionaddRequirement($requirement) {
56
-
if (!is_string($requirement) || !is_int($requirement)) {
57
-
thrownewFileQueryException(sprintf('A requirement van only be of type integer or string. Provided requirement is of type "%s"', gettype($requirement)));
57
+
if (!(is_string($requirement) || is_int($requirement))) {
58
+
thrownewFileQueryRequirementsException(sprintf('A requirement van only be of type integer or string. Provided requirement is of type "%s"', gettype($requirement)));
58
59
}
59
60
if (!$this->requirementIsRegistered($requirement)) {
60
-
thrownewFileQueryException(sprintf('Trying to add a requirement, but it isn\'t registered. Provided requirement "%s"', $requirement));
61
+
thrownewFileQueryRequirementsException(sprintf('Trying to add a requirement, but it isn\'t registered. Provided requirement "%s"', $requirement));
61
62
}
62
63
if (!$this->hasRequirement($requirement)) {
63
64
$this->_requirements[] = $requirement;
@@ -101,8 +102,17 @@ public function hasRequirement($requirement) {
101
102
* @return bool Returns true if there is at least one requirement, otherwise it will return false
102
103
*/
103
104
publicfunctionhasRequirements() {
105
+
return$this->countRequirements() !== 0;
106
+
}
107
+
108
+
/**
109
+
* Count all configured requirements.
110
+
*
111
+
* @return int Returns the total number of requirements
112
+
*/
113
+
publicfunctioncountRequirements() {
104
114
$requirements = $this->requirements();
105
-
returncount($requirements) !== 0;
115
+
returncount($requirements);
106
116
}
107
117
108
118
/**
@@ -120,56 +130,91 @@ public function requirementIsRegistered($requirement) {
thrownewFileQueryRequirementsException(sprintf('Trying to register a requirement but the requirement isn\'t callable. Info "%s"', implode(', ', (array) $callable)));
133
+
if (is_string($callable) && !method_exists($this, $callable)) {
134
+
thrownewFileQueryRequirementsException(sprintf('Trying to register a requirement via a string but the requirement isn\'t callable. Method name "%s"', $callable));
135
+
}
136
+
elseif (is_array($callable)) {
137
+
$class = new \ReflectionClass($callable[0]);
138
+
if (!$class->hasMethod($callable[1])) {
139
+
thrownewFileQueryRequirementsException(sprintf('Trying to register a requirement but the requirement isn\'t callable. Class name "%s", method name "%s"', get_class($callable[0]), $callable[1]));
140
+
}
141
+
}
142
+
else {
143
+
thrownewFileQueryRequirementsException(sprintf('Trying to register a requirement but the requirement\'s callable isn\'t a known data-type. Must be a string or an array. Type given "%s"', gettype($callable)));
144
+
}
145
+
146
+
if (isset($this->_registeredRequirements[$id])) {
147
+
returnfalse;
125
148
}
126
149
$this->_registeredRequirements[$id] = $callable;
127
150
returntrue;
128
151
}
129
152
153
+
/**
154
+
* Count all registered requirements.
155
+
*
156
+
* @return int Returns the total number of registered requirements
157
+
*/
158
+
publicfunctioncountRegisteredRequirements() {
159
+
returncount($this->_registeredRequirements);
160
+
}
161
+
162
+
/**
163
+
* Check whether a requirements has already been registered
164
+
* @param string $id ID of the requirement
165
+
* @return bool Return true if the requirement is already registered
returnnewFileQueryRequirementsException(sprintf('At least 1 file must be available for file "%s" in child with an id of "%s". Please create the file in any of these locations: %s', $child->relativePath(), $child->childDir()->id(), implode($child->rawAbsolutePaths())));
returnnewFileQueryRequirementsException(sprintf('Last file "%s" not found in child "%s" but it is required', $child->relativePath(), $child->childDir()->id()));
204
+
}
205
+
// just check the first root dir (which is the last one because it was reversed)
if ($child->totalExistingPaths() != $child->files()->totalRootDirs()) {
172
-
returnnewFileQueryException(sprintf('All "%s" children must contain a file called "%s".', $child->childDir()->id(), $child->relativePath()));
216
+
if ($child->totalExistingPaths() != count($child->getRootDirs())) {
217
+
returnnewFileQueryRequirementsException(sprintf('All "%s" children must contain a file called "%s".', $child->childDir()->id(), $child->relativePath()));
173
218
}
174
219
returntrue;
175
220
}
@@ -179,8 +224,10 @@ protected function requirementAll(FilesQueryChild $child) {
179
224
*
180
225
* @param FilesQueryChild $queryChild
181
226
* @param bool $throwExceptionOnFail
182
-
* @throws \Exception
183
-
* @return mixed Returns true if all requirements are met. Otherwise returns an un-thrown exception if 'throwExceptionOnFail' is set to false or the response from the requirement
227
+
* @throws \Exception When $throwExceptionOnFail is set to true and one of the requirements fails, it will throw
228
+
* the exception from that fail. Otherwise this exception will be returned
229
+
* @return mixed Returns true if all requirements are met. Otherwise returns an un-thrown exception
230
+
* if 'throwExceptionOnFail' is set to false or the response from the requirement
0 commit comments