Browse Source

Fix #18832 - Inflector::camel2words() adding extra spaces (#18833)

tags/2.0.44
Brandon Kelly 3 years ago committed by GitHub
parent
commit
63e93ba243
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/CHANGELOG.md
  2. 10
      framework/helpers/BaseInflector.php
  3. 4
      tests/framework/helpers/InflectorTest.php

1
framework/CHANGELOG.md

@ -7,6 +7,7 @@ Yii Framework 2 Change Log
- Enh #18826: Add ability to turn the sorting off for a clicked column in GridView with multisort (ditibal) - Enh #18826: Add ability to turn the sorting off for a clicked column in GridView with multisort (ditibal)
- Bug #18646: Remove stale identity data from session if `IdentityInterface::findIdentity()` returns `null` (mikehaertl) - Bug #18646: Remove stale identity data from session if `IdentityInterface::findIdentity()` returns `null` (mikehaertl)
- Bug #18832: Fix `Inflector::camel2words()` adding extra spaces (brandonkelly)
2.0.43 August 09, 2021 2.0.43 August 09, 2021

10
framework/helpers/BaseInflector.php

@ -371,11 +371,11 @@ class BaseInflector
*/ */
public static function camel2words($name, $ucwords = true) public static function camel2words($name, $ucwords = true)
{ {
$label = mb_strtolower(trim(str_replace([ // Add a space before any uppercase letter preceded by a lowercase letter (xY => x Y)
'-', // and any uppercase letter preceded by an uppercase letter and followed by a lowercase letter (XYz => X Yz)
'_', $label = preg_replace('/(?<=\p{Ll})\p{Lu}|(?<=\p{L})\p{Lu}(?=\p{Ll})/u', ' \0', $name);
'.',
], ' ', preg_replace('/(?<!\p{Lu})(\p{Lu})|(\p{Lu})(?=\p{Ll})/u', ' \0', $name))), self::encoding()); $label = mb_strtolower(trim(str_replace(['-', '_', '.'], ' ', $label)), self::encoding());
return $ucwords ? StringHelper::mb_ucwords($label, self::encoding()) : $label; return $ucwords ? StringHelper::mb_ucwords($label, self::encoding()) : $label;
} }

4
tests/framework/helpers/InflectorTest.php

@ -117,6 +117,10 @@ class InflectorTest extends TestCase
$this->assertEquals('Generate Csrf', Inflector::camel2words('generateCSRF')); $this->assertEquals('Generate Csrf', Inflector::camel2words('generateCSRF'));
$this->assertEquals('Generate Csrf Token', Inflector::camel2words('generateCSRFToken')); $this->assertEquals('Generate Csrf Token', Inflector::camel2words('generateCSRFToken'));
$this->assertEquals('Csrf Token Generator', Inflector::camel2words('CSRFTokenGenerator')); $this->assertEquals('Csrf Token Generator', Inflector::camel2words('CSRFTokenGenerator'));
$this->assertEquals('Foo Bar', Inflector::camel2words('foo bar'));
$this->assertEquals('Foo Bar', Inflector::camel2words('foo BAR'));
$this->assertEquals('Foo Bar', Inflector::camel2words('Foo Bar'));
$this->assertEquals('Foo Bar', Inflector::camel2words('FOO BAR'));
} }
public function testCamel2id() public function testCamel2id()

Loading…
Cancel
Save