{
const REGEX_CLASS_NAME = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/';
const REGEX_COMMAND_CLASS_NAME = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+Command$/';
+ const REGEX_CONTROLLER_CLASS_NAME = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+Controller$/';
const REGEX_MACHINE_NAME = '/^[a-z0-9_]+$/';
// This REGEX remove spaces between words
const REGEX_REMOVE_SPACES = '/[\\s+]/';
}
}
+ public function validateControllerName($class_name)
+ {
+ if (preg_match(self::REGEX_CONTROLLER_CLASS_NAME, $class_name)) {
+ return $class_name;
+ } elseif (preg_match(self::REGEX_CLASS_NAME, $class_name)) {
+ throw new \InvalidArgumentException(
+ sprintf(
+ 'Controller name "%s" is invalid, it must end with the word \'Controller\'',
+ $class_name
+ )
+ );
+ } else {
+ throw new \InvalidArgumentException(
+ sprintf(
+ 'Controller name "%s" is invalid, it must starts with a letter or underscore, followed by any number of letters, numbers, or underscores and then with the word \'Controller\'.',
+ $class_name
+ )
+ );
+ }
+ }
+
public function validateMachineName($machine_name)
{
if (preg_match(self::REGEX_MACHINE_NAME, $machine_name)) {
public function validateModulePath($module_path, $create = false)
{
- if (!is_dir($module_path)) {
- if ($create && mkdir($module_path, 0755, true)) {
- return $module_path;
- }
+ if (strlen($module_path) > 1 && $module_path[strlen($module_path)-1] == "/") {
+ $module_path = substr($module_path, 0, -1);
+ }
- throw new \InvalidArgumentException(
- sprintf(
- 'Module path "%s" is invalid. You need to provide a valid path.',
- $module_path
- )
- );
+ if (is_dir($module_path)) {
+ chmod($module_path, 0755);
+ return $module_path;
}
- return $module_path;
+
+ if ($create && mkdir($module_path, 0755, true)) {
+ return $module_path;
+ }
+
+ throw new \InvalidArgumentException(
+ sprintf(
+ 'Path "%s" is invalid. You need to provide a valid path.',
+ $module_path
+ )
+ );
}
public function validateMachineNameList($list)
{
$list_checked = [
- 'success' => [],
- 'fail' => [],
+ 'success' => [],
+ 'fail' => [],
];
if (empty($list)) {
return $extensions;
}
+
+ /**
+ * Validate if http methods exist.
+ *
+ * @param array $httpMethods Array http methods.
+ * @param array $availableHttpMethods Array of available http methods.
+ *
+ * @return string
+ */
+ public function validateHttpMethods($httpMethods, $availableHttpMethods)
+ {
+ if (empty($httpMethods)) {
+ return null;
+ }
+
+ $missing_methods = array_diff(array_values($httpMethods), array_keys($availableHttpMethods));
+ if (!empty($missing_methods)) {
+ throw new \InvalidArgumentException(sprintf('HTTP methods "%s" are invalid.', implode(', ', $missing_methods)));
+ }
+
+ return $httpMethods;
+ }
+
+ /**
+ * Validates role existence or non existence.
+ *
+ * @param string $role
+ * Role machine name.
+ * @param array $roles
+ * Array of available roles.
+ * @param bool $checkExistence
+ * To check existence or non existence.
+ *
+ * @return string|null
+ * Role machine name.
+ */
+ private function validateRole($role, $roles, $checkExistence = true)
+ {
+ if (empty($roles)) {
+ return null;
+ }
+
+ $roleExists = array_key_exists($role, $roles);
+ $condition = $checkExistence ? !$roleExists : $roleExists;
+ if ($condition) {
+ $errorMessage = $checkExistence ? "Role %s doesn't exist" : 'Role %s already exists';
+ throw new \InvalidArgumentException(sprintf($errorMessage, $role));
+ }
+
+ return $role;
+ }
+
+ /**
+ * Validate if the role already exists.
+ *
+ * @param string $role
+ * Role machine name.
+ * @param array $roles
+ * Array of available roles.
+ *
+ * @return string|null
+ * Role machine name.
+ */
+ public function validateRoleExistence($role, $roles) {
+ return $this->validateRole($role, $roles, true);
+ }
+
+ /**
+ * Validate if the role doesn't exist.
+ *
+ * @param string $role
+ * Role machine name.
+ * @param array $roles
+ * Array of available roles.
+ *
+ * @return string|null
+ * Role machine name.
+ */
+ public function validateRoleNotExistence($role, $roles) {
+ return $this->validateRole($role, $roles, false);
+ }
}