X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fchi-teck%2Fdrupal-code-generator%2Fsrc%2FUtils.php;fp=vendor%2Fchi-teck%2Fdrupal-code-generator%2Fsrc%2FUtils.php;h=c27798e624ebb33abf0839a04ac150998d0734fb;hp=feea5cb47883814991de7490cefb7f1a5ed6c1ce;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/vendor/chi-teck/drupal-code-generator/src/Utils.php b/vendor/chi-teck/drupal-code-generator/src/Utils.php index feea5cb47..c27798e62 100644 --- a/vendor/chi-teck/drupal-code-generator/src/Utils.php +++ b/vendor/chi-teck/drupal-code-generator/src/Utils.php @@ -34,6 +34,13 @@ class Utils { ), '_'); } + /** + * Transforms a camelized sting to machine name. + */ + public static function camel2machine($input) { + return self::human2machine(preg_replace('/[A-Z]/', ' \0', $input)); + } + /** * Camelize a string. */ @@ -63,12 +70,22 @@ class Utils { * @see http://php.net/manual/en/language.oop5.basic.php */ public static function validateClassName($value) { - if (!preg_match('/^[A-Z][a-zA-Z0-0][a-zA-Z0-9]*$/', $value)) { + if (!preg_match('/^[A-Z][a-zA-Z0-9]+$/', $value)) { throw new \UnexpectedValueException('The value is not correct class name.'); } return $value; } + /** + * Service name validator. + */ + public static function validateServiceName($value) { + if ($value !== '' && !preg_match('/^[a-z][a-z0-9_\.]*[a-z0-9]$/', $value)) { + throw new \UnexpectedValueException('The value is not correct service name.'); + } + return $value; + } + /** * Required value validator. */ @@ -154,7 +171,7 @@ class Utils { $extension_root = FALSE; for ($i = 1; $i <= 5; $i++) { $info_file = $directory . '/' . basename($directory) . '.info'; - if (file_exists($info_file) || file_exists($info_file . '.yml')) { + if ((file_exists($info_file) && basename($directory) !== 'drush') || file_exists($info_file . '.yml')) { $extension_root = $directory; break; } @@ -198,4 +215,44 @@ class Utils { return str_replace(array_keys($tokens), array_values($tokens), $text); } + /** + * Pluralizes a noun. + * + * @param string $string + * A noun to pluralize. + * + * @return string + * The pluralized noun. + */ + public static function pluralize($string) { + switch (substr($string, -1)) { + case 'y': + return substr($string, 0, -1) . 'ies'; + + case 's': + return $string . 'es'; + + default: + return $string . 's'; + } + } + + /** + * Prepares choices. + * + * @param array $raw_choices + * The choices to be prepared. + * + * @return array + * The prepared choices. + */ + public static function prepareChoices(array $raw_choices) { + // The $raw_choices can be an associative array. + $choices = array_values($raw_choices); + // Start choices list form '1'. + array_unshift($choices, NULL); + unset($choices[0]); + return $choices; + } + }