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

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