diff vendor/chi-teck/drupal-code-generator/src/Utils.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
line wrap: on
line diff
--- a/vendor/chi-teck/drupal-code-generator/src/Utils.php	Thu Feb 28 11:14:44 2019 +0000
+++ b/vendor/chi-teck/drupal-code-generator/src/Utils.php	Thu Feb 28 13:11:55 2019 +0000
@@ -35,6 +35,13 @@
   }
 
   /**
+   * Transforms a camelized sting to machine name.
+   */
+  public static function camel2machine($input) {
+    return self::human2machine(preg_replace('/[A-Z]/', ' \0', $input));
+  }
+
+  /**
    * Camelize a string.
    */
   public static function camelize($string, $upper_camel = TRUE) {
@@ -70,6 +77,16 @@
   }
 
   /**
+   * 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.
    */
   public static function validateRequired($value) {
@@ -154,7 +171,7 @@
     $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 @@
     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;
+  }
+
 }