Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / chi-teck / drupal-code-generator / src / Utils.php
index feea5cb47883814991de7490cefb7f1a5ed6c1ce..c27798e624ebb33abf0839a04ac150998d0734fb 100644 (file)
@@ -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;
+  }
+
 }