vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/UnderscoreNamingStrategy.php line 62

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\ORM\Mapping;
  20. use function preg_replace;
  21. use function strpos;
  22. use function strrpos;
  23. use function strtolower;
  24. use function strtoupper;
  25. use function substr;
  26. use function trigger_error;
  27. use const CASE_LOWER;
  28. use const CASE_UPPER;
  29. use const E_USER_DEPRECATED;
  30. /**
  31.  * Naming strategy implementing the underscore naming convention.
  32.  * Converts 'MyEntity' to 'my_entity' or 'MY_ENTITY'.
  33.  *
  34.  * @link    www.doctrine-project.org
  35.  */
  36. class UnderscoreNamingStrategy implements NamingStrategy
  37. {
  38.     private const DEFAULT_PATTERN      '/(?<=[a-z])([A-Z])/';
  39.     private const NUMBER_AWARE_PATTERN '/(?<=[a-z0-9])([A-Z])/';
  40.     /** @var int */
  41.     private $case;
  42.     /** @var string */
  43.     private $pattern;
  44.     /**
  45.      * Underscore naming strategy construct.
  46.      *
  47.      * @param int $case CASE_LOWER | CASE_UPPER
  48.      */
  49.     public function __construct($case CASE_LOWERbool $numberAware false)
  50.     {
  51.         if (! $numberAware) {
  52.             @trigger_error(
  53.                 'Creating ' self::class . ' without making it number aware is deprecated and will be removed in Doctrine ORM 3.0.',
  54.                 E_USER_DEPRECATED
  55.             );
  56.         }
  57.         $this->case    $case;
  58.         $this->pattern $numberAware self::NUMBER_AWARE_PATTERN self::DEFAULT_PATTERN;
  59.     }
  60.     /**
  61.      * @return int CASE_LOWER | CASE_UPPER
  62.      */
  63.     public function getCase()
  64.     {
  65.         return $this->case;
  66.     }
  67.     /**
  68.      * Sets string case CASE_LOWER | CASE_UPPER.
  69.      * Alphabetic characters converted to lowercase or uppercase.
  70.      *
  71.      * @param int $case
  72.      *
  73.      * @return void
  74.      */
  75.     public function setCase($case)
  76.     {
  77.         $this->case $case;
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function classToTableName($className)
  83.     {
  84.         if (strpos($className'\\') !== false) {
  85.             $className substr($classNamestrrpos($className'\\') + 1);
  86.         }
  87.         return $this->underscore($className);
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function propertyToColumnName($propertyName$className null)
  93.     {
  94.         return $this->underscore($propertyName);
  95.     }
  96.     /**
  97.      * {@inheritdoc}
  98.      */
  99.     public function embeddedFieldToColumnName($propertyName$embeddedColumnName$className null$embeddedClassName null)
  100.     {
  101.         return $this->underscore($propertyName) . '_' $embeddedColumnName;
  102.     }
  103.     /**
  104.      * {@inheritdoc}
  105.      */
  106.     public function referenceColumnName()
  107.     {
  108.         return $this->case === CASE_UPPER ?  'ID' 'id';
  109.     }
  110.     /**
  111.      * {@inheritdoc}
  112.      */
  113.     public function joinColumnName($propertyName$className null)
  114.     {
  115.         return $this->underscore($propertyName) . '_' $this->referenceColumnName();
  116.     }
  117.     /**
  118.      * {@inheritdoc}
  119.      */
  120.     public function joinTableName($sourceEntity$targetEntity$propertyName null)
  121.     {
  122.         return $this->classToTableName($sourceEntity) . '_' $this->classToTableName($targetEntity);
  123.     }
  124.     /**
  125.      * {@inheritdoc}
  126.      */
  127.     public function joinKeyColumnName($entityName$referencedColumnName null)
  128.     {
  129.         return $this->classToTableName($entityName) . '_' .
  130.                 ($referencedColumnName ?: $this->referenceColumnName());
  131.     }
  132.     private function underscore(string $string): string
  133.     {
  134.         $string preg_replace($this->pattern'_$1'$string);
  135.         if ($this->case === CASE_UPPER) {
  136.             return strtoupper($string);
  137.         }
  138.         return strtolower($string);
  139.     }
  140. }