vendor/api-platform/core/src/Metadata/Util/CamelCaseToSnakeCaseNameConverter.php line 41

  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. /*
  12.  * This file is part of the Symfony package.
  13.  *
  14.  * (c) Fabien Potencier <fabien@symfony.com>
  15.  *
  16.  * For the full copyright and license information, please view the LICENSE
  17.  * file that was distributed with this source code.
  18.  */
  19. namespace ApiPlatform\Metadata\Util;
  20. /**
  21.  * CamelCase to Underscore name converter.
  22.  *
  23.  * @internal
  24.  *
  25.  * @author Kévin Dunglas <dunglas@gmail.com>
  26.  */
  27. class CamelCaseToSnakeCaseNameConverter
  28. {
  29.     private $attributes;
  30.     private $lowerCamelCase;
  31.     /**
  32.      * @param array|null $attributes     The list of attributes to rename or null for all attributes
  33.      * @param bool       $lowerCamelCase Use lowerCamelCase style
  34.      */
  35.     public function __construct(array $attributes nullbool $lowerCamelCase true)
  36.     {
  37.         $this->attributes $attributes;
  38.         $this->lowerCamelCase $lowerCamelCase;
  39.     }
  40.     public function normalize(string $propertyName): string
  41.     {
  42.         if (null === $this->attributes || \in_array($propertyName$this->attributestrue)) {
  43.             return strtolower(preg_replace('/[A-Z]/''_\\0'lcfirst($propertyName)));
  44.         }
  45.         return $propertyName;
  46.     }
  47.     public function denormalize(string $propertyName): string
  48.     {
  49.         $camelCasedName preg_replace_callback('/(^|_|\.)+(.)/', function ($match) {
  50.             return ('.' === $match[1] ? '_' '').strtoupper($match[2]);
  51.         }, $propertyName);
  52.         if ($this->lowerCamelCase) {
  53.             $camelCasedName lcfirst($camelCasedName);
  54.         }
  55.         if (null === $this->attributes || \in_array($camelCasedName$this->attributestrue)) {
  56.             return $camelCasedName;
  57.         }
  58.         return $propertyName;
  59.     }
  60. }