vendor/doctrine/inflector/lib/Doctrine/Common/Inflector/Inflector.php line 259

Open in your IDE?
  1. <?php
  2. /*
  3.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  *
  15.  * This software consists of voluntary contributions made by many individuals
  16.  * and is licensed under the MIT license. For more information, see
  17.  * <http://www.doctrine-project.org>.
  18.  */
  19. namespace Doctrine\Common\Inflector;
  20. use Doctrine\Inflector\Inflector as InflectorObject;
  21. use Doctrine\Inflector\InflectorFactory;
  22. use Doctrine\Inflector\LanguageInflectorFactory;
  23. use Doctrine\Inflector\Rules\Pattern;
  24. use Doctrine\Inflector\Rules\Patterns;
  25. use Doctrine\Inflector\Rules\Ruleset;
  26. use Doctrine\Inflector\Rules\Substitution;
  27. use Doctrine\Inflector\Rules\Substitutions;
  28. use Doctrine\Inflector\Rules\Transformation;
  29. use Doctrine\Inflector\Rules\Transformations;
  30. use Doctrine\Inflector\Rules\Word;
  31. use InvalidArgumentException;
  32. use function array_keys;
  33. use function array_map;
  34. use function array_unshift;
  35. use function array_values;
  36. use function sprintf;
  37. use function trigger_error;
  38. use const E_USER_DEPRECATED;
  39. /**
  40.  * @deprecated
  41.  */
  42. class Inflector
  43. {
  44.     /**
  45.      * @var LanguageInflectorFactory|null
  46.      */
  47.     private static $factory;
  48.     /** @var InflectorObject|null */
  49.     private static $instance;
  50.     private static function getInstance() : InflectorObject
  51.     {
  52.         if (self::$factory === null) {
  53.             self::$factory self::createFactory();
  54.         }
  55.         if (self::$instance === null) {
  56.             self::$instance self::$factory->build();
  57.         }
  58.         return self::$instance;
  59.     }
  60.     private static function createFactory() : LanguageInflectorFactory
  61.     {
  62.         return InflectorFactory::create();
  63.     }
  64.     /**
  65.      * Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'.
  66.      *
  67.      * @deprecated
  68.      */
  69.     public static function tableize(string $word) : string
  70.     {
  71.         @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in doctrine/inflector 2.0. Please update to the new Inflector API.'__METHOD__), E_USER_DEPRECATED);
  72.         return self::getInstance()->tableize($word);
  73.     }
  74.     /**
  75.      * Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'.
  76.      */
  77.     public static function classify(string $word) : string
  78.     {
  79.         @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in doctrine/inflector 2.0. Please update to the new Inflector API.'__METHOD__), E_USER_DEPRECATED);
  80.         return self::getInstance()->classify($word);
  81.     }
  82.     /**
  83.      * Camelizes a word. This uses the classify() method and turns the first character to lowercase.
  84.      *
  85.      * @deprecated
  86.      */
  87.     public static function camelize(string $word) : string
  88.     {
  89.         @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in doctrine/inflector 2.0. Please update to the new Inflector API.'__METHOD__), E_USER_DEPRECATED);
  90.         return self::getInstance()->camelize($word);
  91.     }
  92.     /**
  93.      * Uppercases words with configurable delimiters between words.
  94.      *
  95.      * Takes a string and capitalizes all of the words, like PHP's built-in
  96.      * ucwords function. This extends that behavior, however, by allowing the
  97.      * word delimiters to be configured, rather than only separating on
  98.      * whitespace.
  99.      *
  100.      * Here is an example:
  101.      * <code>
  102.      * <?php
  103.      * $string = 'top-o-the-morning to all_of_you!';
  104.      * echo \Doctrine\Common\Inflector\Inflector::ucwords($string);
  105.      * // Top-O-The-Morning To All_of_you!
  106.      *
  107.      * echo \Doctrine\Common\Inflector\Inflector::ucwords($string, '-_ ');
  108.      * // Top-O-The-Morning To All_Of_You!
  109.      * ?>
  110.      * </code>
  111.      *
  112.      * @param string $string The string to operate on.
  113.      * @param string $delimiters A list of word separators.
  114.      *
  115.      * @return string The string with all delimiter-separated words capitalized.
  116.      *
  117.      * @deprecated
  118.      */
  119.     public static function ucwords(string $stringstring $delimiters " \n\t\r\0\x0B-") : string
  120.     {
  121.         @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in doctrine/inflector 2.0. Please use the "ucwords" function instead.'__METHOD__), E_USER_DEPRECATED);
  122.         return ucwords($string$delimiters);
  123.     }
  124.     /**
  125.      * Clears Inflectors inflected value caches, and resets the inflection
  126.      * rules to the initial values.
  127.      *
  128.      * @deprecated
  129.      */
  130.     public static function reset() : void
  131.     {
  132.         @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in doctrine/inflector 2.0. Please update to the new Inflector API.'__METHOD__), E_USER_DEPRECATED);
  133.         self::$factory null;
  134.         self::$instance null;
  135.     }
  136.     /**
  137.      * Adds custom inflection $rules, of either 'plural' or 'singular' $type.
  138.      *
  139.      * ### Usage:
  140.      *
  141.      * {{{
  142.      * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
  143.      * Inflector::rules('plural', array(
  144.      *     'rules' => array('/^(inflect)ors$/i' => '\1ables'),
  145.      *     'uninflected' => array('dontinflectme'),
  146.      *     'irregular' => array('red' => 'redlings')
  147.      * ));
  148.      * }}}
  149.      *
  150.      * @param string  $type         The type of inflection, either 'plural' or 'singular'
  151.      * @param array|iterable $rules An array of rules to be added.
  152.      * @param boolean $reset        If true, will unset default inflections for all
  153.      *                              new rules that are being defined in $rules.
  154.      *
  155.      * @return void
  156.      *
  157.      * @deprecated
  158.      */
  159.     public static function rules(string $typeiterable $rulesbool $reset false) : void
  160.     {
  161.         @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in doctrine/inflector 2.0. Please update to the new Inflector API.'__METHOD__), E_USER_DEPRECATED);
  162.         if (self::$factory === null) {
  163.             self::$factory self::createFactory();
  164.         }
  165.         self::$instance null;
  166.         switch ($type) {
  167.             case 'singular':
  168.                 self::$factory->withSingularRules(self::buildRuleset($rules), $reset);
  169.                 break;
  170.             case 'plural':
  171.                 self::$factory->withPluralRules(self::buildRuleset($rules), $reset);
  172.                 break;
  173.             default:
  174.                 throw new InvalidArgumentException(sprintf('Cannot define custom inflection rules for type "%s".'$type));
  175.         }
  176.     }
  177.     private static function buildRuleset(iterable $rules) : Ruleset
  178.     {
  179.         $regular = [];
  180.         $irregular = [];
  181.         $uninflected = [];
  182.         foreach ($rules as $rule => $pattern) {
  183.             if ( ! is_array($pattern)) {
  184.                 $regular[$rule] = $pattern;
  185.                 continue;
  186.             }
  187.             switch ($rule) {
  188.                 case 'uninflected':
  189.                     $uninflected $pattern;
  190.                     break;
  191.                 case 'irregular':
  192.                     $irregular $pattern;
  193.                     break;
  194.                 case 'rules':
  195.                     $regular $pattern;
  196.                     break;
  197.             }
  198.         }
  199.         return new Ruleset(
  200.             new Transformations(...array_map(
  201.                 static function (string $patternstring $replacement) : Transformation {
  202.                     return new Transformation(new Pattern($pattern), $replacement);
  203.                 },
  204.                 array_keys($regular),
  205.                 array_values($regular)
  206.             )),
  207.             new Patterns(...array_map(
  208.                 static function (string $pattern) : Pattern {
  209.                     return new Pattern($pattern);
  210.                 },
  211.                 $uninflected
  212.             )),
  213.             new Substitutions(...array_map(
  214.                 static function (string $wordstring $to) : Substitution {
  215.                     return new Substitution(new Word($word), new Word($to));
  216.                 },
  217.                 array_keys($irregular),
  218.                 array_values($irregular)
  219.             ))
  220.         );
  221.     }
  222.     /**
  223.      * Returns a word in plural form.
  224.      *
  225.      * @param string $word The word in singular form.
  226.      *
  227.      * @return string The word in plural form.
  228.      *
  229.      * @deprecated
  230.      */
  231.     public static function pluralize(string $word) : string
  232.     {
  233.         @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in doctrine/inflector 2.0. Please update to the new Inflector API.'__METHOD__), E_USER_DEPRECATED);
  234.         return self::getInstance()->pluralize($word);
  235.     }
  236.     /**
  237.      * Returns a word in singular form.
  238.      *
  239.      * @param string $word The word in plural form.
  240.      *
  241.      * @return string The word in singular form.
  242.      *
  243.      * @deprecated
  244.      */
  245.     public static function singularize(string $word) : string
  246.     {
  247.         @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in doctrine/inflector 2.0. Please update to the new Inflector API.'__METHOD__), E_USER_DEPRECATED);
  248.         return self::getInstance()->singularize($word);
  249.     }
  250. }