comparison vendor/symfony/serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
16 * 16 *
17 * @author Kévin Dunglas <dunglas@gmail.com> 17 * @author Kévin Dunglas <dunglas@gmail.com>
18 */ 18 */
19 class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface 19 class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
20 { 20 {
21 /**
22 * @var array|null
23 */
24 private $attributes; 21 private $attributes;
25
26 /**
27 * @var bool
28 */
29 private $lowerCamelCase; 22 private $lowerCamelCase;
30 23
31 /** 24 /**
32 * @param null|array $attributes The list of attributes to rename or null for all attributes 25 * @param null|array $attributes The list of attributes to rename or null for all attributes
33 * @param bool $lowerCamelCase Use lowerCamelCase style 26 * @param bool $lowerCamelCase Use lowerCamelCase style
41 /** 34 /**
42 * {@inheritdoc} 35 * {@inheritdoc}
43 */ 36 */
44 public function normalize($propertyName) 37 public function normalize($propertyName)
45 { 38 {
46 if (null === $this->attributes || in_array($propertyName, $this->attributes)) { 39 if (null === $this->attributes || \in_array($propertyName, $this->attributes)) {
47 $lcPropertyName = lcfirst($propertyName); 40 return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
48 $snakeCasedName = '';
49
50 $len = strlen($lcPropertyName);
51 for ($i = 0; $i < $len; ++$i) {
52 if (ctype_upper($lcPropertyName[$i])) {
53 $snakeCasedName .= '_'.strtolower($lcPropertyName[$i]);
54 } else {
55 $snakeCasedName .= strtolower($lcPropertyName[$i]);
56 }
57 }
58
59 return $snakeCasedName;
60 } 41 }
61 42
62 return $propertyName; 43 return $propertyName;
63 } 44 }
64 45
73 54
74 if ($this->lowerCamelCase) { 55 if ($this->lowerCamelCase) {
75 $camelCasedName = lcfirst($camelCasedName); 56 $camelCasedName = lcfirst($camelCasedName);
76 } 57 }
77 58
78 if (null === $this->attributes || in_array($camelCasedName, $this->attributes)) { 59 if (null === $this->attributes || \in_array($camelCasedName, $this->attributes)) {
79 return $camelCasedName; 60 return $camelCasedName;
80 } 61 }
81 62
82 return $propertyName; 63 return $propertyName;
83 } 64 }