X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fdrupal%2Fconsole%2Fsrc%2FUtils%2FValidator.php;fp=vendor%2Fdrupal%2Fconsole%2Fsrc%2FUtils%2FValidator.php;h=12b522786c83bbfefc34c80b38dc95f3dc8c8b2a;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/drupal/console/src/Utils/Validator.php b/vendor/drupal/console/src/Utils/Validator.php new file mode 100644 index 000000000..12b522786 --- /dev/null +++ b/vendor/drupal/console/src/Utils/Validator.php @@ -0,0 +1,262 @@ +extensionManager = $extensionManager; + } + + public function validateModuleName($module) + { + if (!empty($module)) { + return $module; + } else { + throw new \InvalidArgumentException(sprintf('Module name "%s" is invalid.', $module)); + } + } + + public function validateClassName($class_name) + { + if (preg_match(self::REGEX_CLASS_NAME, $class_name)) { + return $class_name; + } else { + throw new \InvalidArgumentException( + sprintf( + 'Class name "%s" is invalid, it must starts with a letter or underscore, followed by any number of letters, numbers, or underscores.', + $class_name + ) + ); + } + } + + public function validateBundleTitle($bundle_title) + { + if (!empty($bundle_title)) { + return $bundle_title; + } else { + throw new \InvalidArgumentException(sprintf('Bundle title "%s" is invalid.', $bundle_title)); + } + } + + public function validateCommandName($class_name) + { + if (preg_match(self::REGEX_COMMAND_CLASS_NAME, $class_name)) { + return $class_name; + } elseif (preg_match(self::REGEX_CLASS_NAME, $class_name)) { + throw new \InvalidArgumentException( + sprintf( + 'Command name "%s" is invalid, it must end with the word \'Command\'', + $class_name + ) + ); + } else { + throw new \InvalidArgumentException( + sprintf( + 'Command 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 \'Command\'.', + $class_name + ) + ); + } + } + + public function validateMachineName($machine_name) + { + if (preg_match(self::REGEX_MACHINE_NAME, $machine_name)) { + return $machine_name; + } else { + throw new \InvalidArgumentException( + sprintf( + 'Machine name "%s" is invalid, it must contain only lowercase letters, numbers and underscores.', + $machine_name + ) + ); + } + } + + public function validateModulePath($module_path, $create = false) + { + if (!is_dir($module_path)) { + if ($create && mkdir($module_path, 0755, true)) { + return $module_path; + } + + throw new \InvalidArgumentException( + sprintf( + 'Module path "%s" is invalid. You need to provide a valid path.', + $module_path + ) + ); + } + + return $module_path; + } + + public function validateModuleDependencies($dependencies) + { + $dependencies_checked = [ + 'success' => [], + 'fail' => [], + ]; + + if (empty($dependencies)) { + return []; + } + + $dependencies = explode(',', $this->removeSpaces($dependencies)); + foreach ($dependencies as $key => $module) { + if (!empty($module)) { + if (preg_match(self::REGEX_MACHINE_NAME, $module)) { + $dependencies_checked['success'][] = $module; + } else { + $dependencies_checked['fail'][] = $module; + } + } + } + + return $dependencies_checked; + } + + /** + * Validate if service name exist. + * + * @param string $service Service name + * @param array $services Array of services + * + * @return string + */ + public function validateServiceExist($service, $services) + { + if ($service == '') { + return null; + } + + if (!in_array($service, array_values($services))) { + throw new \InvalidArgumentException(sprintf('Service "%s" is invalid.', $service)); + } + + return $service; + } + + /** + * Validate if service name exist. + * + * @param string $service Service name + * @param array $services Array of services + * + * @return string + */ + public function validatePluginManagerServiceExist($service, $services) + { + if ($service == '') { + return null; + } + + if (!in_array($service, array_values($services))) { + throw new \InvalidArgumentException(sprintf('Plugin "%s" is invalid.', $service)); + } + + return $service; + } + + /** + * Validate if event name exist. + * + * @param string $event Event name + * @param array $events Array of events + * + * @return string + */ + public function validateEventExist($event, $events) + { + if ($event == '') { + return null; + } + + if (!in_array($event, array_values($events))) { + throw new \InvalidArgumentException(sprintf('Event "%s" is invalid.', $event)); + } + + return $event; + } + + /** + * Validates if class name have spaces between words. + * + * @param string $name + * + * @return string + */ + public function validateSpaces($name) + { + $string = $this->removeSpaces($name); + if ($string == $name) { + return $name; + } else { + throw new \InvalidArgumentException( + sprintf( + 'The name "%s" is invalid, spaces between words are not allowed.', + $name + ) + ); + } + } + + public function removeSpaces($name) + { + return preg_replace(self::REGEX_REMOVE_SPACES, '', $name); + } + + /** + * @param $moduleList + * @return array + */ + public function getMissingModules($moduleList) + { + $modules = $this->extensionManager->discoverModules() + ->showInstalled() + ->showUninstalled() + ->showNoCore() + ->showCore() + ->getList(true); + + return array_diff($moduleList, $modules); + } + + /** + * @param $moduleList + * @return array + */ + public function getUninstalledModules($moduleList) + { + $modules = $this->extensionManager->discoverModules() + ->showInstalled() + ->showNoCore() + ->showCore() + ->getList(true); + + return array_diff($moduleList, $modules); + } +}