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