ripper1355

Inflector.php

Feb 12th, 2021
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.80 KB | None | 0 0
  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.  
  20. namespace Doctrine\Common\Inflector;
  21.  
  22. use Doctrine\Inflector\Inflector as InflectorObject;
  23. use Doctrine\Inflector\InflectorFactory;
  24. use Doctrine\Inflector\LanguageInflectorFactory;
  25. use Doctrine\Inflector\Rules\Pattern;
  26. use Doctrine\Inflector\Rules\Patterns;
  27. use Doctrine\Inflector\Rules\Ruleset;
  28. use Doctrine\Inflector\Rules\Substitution;
  29. use Doctrine\Inflector\Rules\Substitutions;
  30. use Doctrine\Inflector\Rules\Transformation;
  31. use Doctrine\Inflector\Rules\Transformations;
  32. use Doctrine\Inflector\Rules\Word;
  33. use InvalidArgumentException;
  34. use function array_keys;
  35. use function array_map;
  36. use function array_unshift;
  37. use function array_values;
  38. use function sprintf;
  39. use function trigger_error;
  40. use const E_USER_DEPRECATED;
  41.  
  42. /**
  43.  * @deprecated
  44.  */
  45. class Inflector
  46. {
  47.     /**
  48.      * @var LanguageInflectorFactory|null
  49.      */
  50.     private static $factory;
  51.  
  52.     /** @var InflectorObject|null */
  53.     private static $instance;
  54.  
  55.     private static function getInstance() : InflectorObject
  56.     {
  57.         if (self::$factory === null) {
  58.             self::$factory = self::createFactory();
  59.         }
  60.  
  61.         if (self::$instance === null) {
  62.             self::$instance = self::$factory->build();
  63.         }
  64.  
  65.         return self::$instance;
  66.     }
  67.  
  68.     private static function createFactory() : LanguageInflectorFactory
  69.     {
  70.         return InflectorFactory::create();
  71.     }
  72.  
  73.     /**
  74.      * Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'.
  75.      *
  76.      * @deprecated
  77.      */
  78.     public static function tableize(string $word) : string
  79.     {
  80.         @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);
  81.  
  82.         return self::getInstance()->tableize($word);
  83.     }
  84.  
  85.     /**
  86.      * Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'.
  87.      */
  88.     public static function classify(string $word) : string
  89.     {
  90.         @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);
  91.  
  92.         return self::getInstance()->classify($word);
  93.     }
  94.  
  95.     /**
  96.      * Camelizes a word. This uses the classify() method and turns the first character to lowercase.
  97.      *
  98.      * @deprecated
  99.      */
  100.     public static function camelize(string $word) : string
  101.     {
  102.         @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);
  103.  
  104.         return self::getInstance()->camelize($word);
  105.     }
  106.  
  107.     /**
  108.      * Uppercases words with configurable delimiters between words.
  109.      *
  110.      * Takes a string and capitalizes all of the words, like PHP's built-in
  111.      * ucwords function. This extends that behavior, however, by allowing the
  112.      * word delimiters to be configured, rather than only separating on
  113.      * whitespace.
  114.      *
  115.      * Here is an example:
  116.      * <code>
  117.      * <?php
  118.      * $string = 'top-o-the-morning to all_of_you!';
  119.      * echo \Doctrine\Common\Inflector\Inflector::ucwords($string);
  120.      * // Top-O-The-Morning To All_of_you!
  121.      *
  122.      * echo \Doctrine\Common\Inflector\Inflector::ucwords($string, '-_ ');
  123.      * // Top-O-The-Morning To All_Of_You!
  124.      * ?>
  125.      * </code>
  126.      *
  127.      * @param string $string The string to operate on.
  128.      * @param string $delimiters A list of word separators.
  129.      *
  130.      * @return string The string with all delimiter-separated words capitalized.
  131.      *
  132.      * @deprecated
  133.      */
  134.     public static function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-") : string
  135.     {
  136.         @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);
  137.  
  138.         return ucwords($string, $delimiters);
  139.     }
  140.  
  141.     /**
  142.      * Clears Inflectors inflected value caches, and resets the inflection
  143.      * rules to the initial values.
  144.      *
  145.      * @deprecated
  146.      */
  147.     public static function reset() : void
  148.     {
  149.         @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);
  150.  
  151.         self::$factory = null;
  152.         self::$instance = null;
  153.     }
  154.  
  155.     /**
  156.      * Adds custom inflection $rules, of either 'plural' or 'singular' $type.
  157.      *
  158.      * ### Usage:
  159.      *
  160.      * {{{
  161.      * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
  162.      * Inflector::rules('plural', array(
  163.      *     'rules' => array('/^(inflect)ors$/i' => '\1ables'),
  164.      *     'uninflected' => array('dontinflectme'),
  165.      *     'irregular' => array('red' => 'redlings')
  166.      * ));
  167.      * }}}
  168.      *
  169.      * @param string  $type         The type of inflection, either 'plural' or 'singular'
  170.      * @param array|iterable $rules An array of rules to be added.
  171.      * @param boolean $reset        If true, will unset default inflections for all
  172.      *                              new rules that are being defined in $rules.
  173.      *
  174.      * @return void
  175.      *
  176.      * @deprecated
  177.      */
  178.     public static function rules(string $type, iterable $rules, bool $reset = false) : void
  179.     {
  180.         @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);
  181.  
  182.         if (self::$factory === null) {
  183.             self::$factory = self::createFactory();
  184.         }
  185.  
  186.         self::$instance = null;
  187.  
  188.         switch ($type) {
  189.             case 'singular':
  190.                 self::$factory->withSingularRules(self::buildRuleset($rules), $reset);
  191.                 break;
  192.             case 'plural':
  193.                 self::$factory->withPluralRules(self::buildRuleset($rules), $reset);
  194.                 break;
  195.             default:
  196.                 throw new InvalidArgumentException(sprintf('Cannot define custom inflection rules for type "%s".', $type));
  197.         }
  198.     }
  199.  
  200.     private static function buildRuleset(iterable $rules) : Ruleset
  201.     {
  202.         $regular = [];
  203.         $irregular = [];
  204.         $uninflected = [];
  205.  
  206.         foreach ($rules as $rule => $pattern) {
  207.             if ( ! is_array($pattern)) {
  208.                 $regular[$rule] = $pattern;
  209.  
  210.                 continue;
  211.             }
  212.  
  213.             switch ($rule) {
  214.                 case 'uninflected':
  215.                     $uninflected = $pattern;
  216.                     break;
  217.                 case 'irregular':
  218.                     $irregular = $pattern;
  219.                     break;
  220.                 case 'rules':
  221.                     $regular = $pattern;
  222.                     break;
  223.             }
  224.         }
  225.  
  226.         return new Ruleset(
  227.             new Transformations(...array_map(
  228.                 static function (string $pattern, string $replacement) : Transformation {
  229.                     return new Transformation(new Pattern($pattern), $replacement);
  230.                 },
  231.                 array_keys($regular),
  232.                 array_values($regular)
  233.             )),
  234.             new Patterns(...array_map(
  235.                 static function (string $pattern) : Pattern {
  236.                     return new Pattern($pattern);
  237.                 },
  238.                 $uninflected
  239.             )),
  240.             new Substitutions(...array_map(
  241.                 static function (string $word, string $to) : Substitution {
  242.                     return new Substitution(new Word($word), new Word($to));
  243.                 },
  244.                 array_keys($irregular),
  245.                 array_values($irregular)
  246.             ))
  247.         );
  248.     }
  249.  
  250.     /**
  251.      * Returns a word in plural form.
  252.      *
  253.      * @param string $word The word in singular form.
  254.      *
  255.      * @return string The word in plural form.
  256.      *
  257.      * @deprecated
  258.      */
  259.     public static function pluralize(string $word) : string
  260.     {
  261.         @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);
  262.  
  263.         return self::getInstance()->pluralize($word);
  264.     }
  265.  
  266.     /**
  267.      * Returns a word in singular form.
  268.      *
  269.      * @param string $word The word in plural form.
  270.      *
  271.      * @return string The word in singular form.
  272.      *
  273.      * @deprecated
  274.      */
  275.     public static function singularize(string $word) : string
  276.     {
  277.         @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);
  278.  
  279.         return self::getInstance()->singularize($word);
  280.     }
  281. }
  282.  
Advertisement
Add Comment
Please, Sign In to add comment