forked from phalcon/phalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(support\helper\str): Added third parameter to Camelize to lowerCa…
…seFirst if need be; Added PascalCase, SnakeCase and KebabCase helpers Related phalcon/cphalcon#15850 Refs phalcon#6.0.x
- Loading branch information
Showing
8 changed files
with
459 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Phalcon. | ||
* | ||
* (c) Phalcon Team <team@phalcon.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Support\Helper\Str; | ||
|
||
use function array_map; | ||
use function implode; | ||
use function lcfirst; | ||
use function mb_strtolower; | ||
use function preg_replace; | ||
use function preg_split; | ||
use function str_replace; | ||
use function ucfirst; | ||
|
||
use function ucwords; | ||
|
||
use const PREG_SPLIT_DELIM_CAPTURE; | ||
use const PREG_SPLIT_NO_EMPTY; | ||
|
||
/** | ||
* Converts strings to kebab-case style | ||
*/ | ||
class KebabCase extends PascalCase | ||
{ | ||
/** | ||
* @param string $text | ||
* @param string|null $delimiters | ||
* | ||
* @return string | ||
*/ | ||
public function __invoke( | ||
string $text, | ||
string $delimiters = null | ||
): string | ||
{ | ||
$output = $this->processArray($text, $delimiters); | ||
|
||
return implode('_', $output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Phalcon. | ||
* | ||
* (c) Phalcon Team <team@phalcon.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Support\Helper\Str; | ||
|
||
use function array_map; | ||
use function implode; | ||
use function lcfirst; | ||
use function mb_strtolower; | ||
use function preg_replace; | ||
use function preg_split; | ||
use function str_replace; | ||
use function ucfirst; | ||
|
||
use function ucwords; | ||
|
||
use const PREG_SPLIT_DELIM_CAPTURE; | ||
use const PREG_SPLIT_NO_EMPTY; | ||
|
||
/** | ||
* Converts strings to PascalCase style | ||
*/ | ||
class PascalCase | ||
{ | ||
/** | ||
* @param string $text | ||
* @param string|null $delimiters | ||
* | ||
* @return string | ||
*/ | ||
public function __invoke( | ||
string $text, | ||
string $delimiters = null | ||
): string { | ||
$exploded = $this->processArray($text, $delimiters); | ||
|
||
$output = array_map( | ||
function ($element) { | ||
return ucfirst(mb_strtolower($element)); | ||
}, | ||
$exploded | ||
); | ||
|
||
return implode('', $output); | ||
} | ||
|
||
/** | ||
* @param string $text | ||
* @param string|null $delimiters | ||
* | ||
* @return array | ||
*/ | ||
protected function processArray(string $text, string $delimiters = null): array | ||
{ | ||
$delimiters = $delimiters ?: '\-_'; | ||
/** | ||
* Escape the `-` if it exists so that it does not get interpreted | ||
* as a range. First remove any escaping for the `-` if present and then | ||
* add it again - just to be on the safe side | ||
*/ | ||
$delimiters = str_replace(['\-', '-'], ['-', '\-'], $delimiters); | ||
|
||
$result = preg_split( | ||
'/[' . $delimiters . ']+/', | ||
$text, | ||
-1, | ||
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY | ||
); | ||
|
||
return (false === $result) ? [] : $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Phalcon. | ||
* | ||
* (c) Phalcon Team <team@phalcon.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Support\Helper\Str; | ||
|
||
use function array_map; | ||
use function implode; | ||
use function lcfirst; | ||
use function mb_strtolower; | ||
use function preg_replace; | ||
use function preg_split; | ||
use function str_replace; | ||
use function ucfirst; | ||
|
||
use function ucwords; | ||
|
||
use const PREG_SPLIT_DELIM_CAPTURE; | ||
use const PREG_SPLIT_NO_EMPTY; | ||
|
||
/** | ||
* Converts strings to snake_case style | ||
*/ | ||
class SnakeCase extends PascalCase | ||
{ | ||
/** | ||
* @param string $text | ||
* @param string|null $delimiters | ||
* | ||
* @return string | ||
*/ | ||
public function __invoke( | ||
string $text, | ||
string $delimiters = null | ||
): string | ||
{ | ||
$output = $this->processArray($text, $delimiters); | ||
|
||
return implode('-', $output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Phalcon Framework. | ||
* | ||
* (c) Phalcon Team <team@phalcon.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE.txt | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Tests\Unit\Support\Helper\Str; | ||
|
||
use Codeception\Example; | ||
use Phalcon\Support\Helper\Str\KebabCase; | ||
use Phalcon\Support\Helper\Str\SnakeCase; | ||
use UnitTester; | ||
|
||
class KebabCaseCest | ||
{ | ||
/** | ||
* Tests Phalcon\Support\Helper\Str :: kebabCase() | ||
* | ||
* @dataProvider getSources | ||
* | ||
* @param UnitTester $I | ||
* @param Example $example | ||
* | ||
* @author Phalcon Team <team@phalcon.io> | ||
* @since 2022-01-02 | ||
*/ | ||
public function supportHelperStrKebabCase(UnitTester $I, Example $example) | ||
{ | ||
$I->wantToTest('Support\Helper\Str - kebabCase() - ' . $example[0]); | ||
|
||
$object = new KebabCase(); | ||
$value = $example[0]; | ||
$expected = $example[1]; | ||
$delimiter = $example[2]; | ||
|
||
$actual = $object($value, $delimiter); | ||
$I->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private function getSources(): array | ||
{ | ||
return [ | ||
['Camelize', 'Camelize', null], | ||
['CameLiZe', 'CameLiZe', null], | ||
['Camelize', 'Camelize', null], | ||
['123camelize', '123camelize', null], | ||
['c_a_m_e_l_i_z_e', 'c_a_m_e_l_i_z_e', null], | ||
['Camelize', 'Camelize', null], | ||
['camel_ize', 'camel_ize', null], | ||
['CameLize', 'CameLize', null], | ||
['c_a-m_e-l_i-z_e', 'c_a_m_e_l_i_z_e', null], | ||
['came_li_ze', 'came_li_ze', null], | ||
['=_camelize', '=_camelize', '_'], | ||
['camelize', 'camelize', '_'], | ||
['came_li_ze', 'came_li_ze', '_'], | ||
['came#li#ze', 'came_li_ze', '#'], | ||
['came li ze', 'came_li_ze', ' '], | ||
['came.li^ze', 'came_li_ze', '.^'], | ||
['c_a-m_e-l_i-z_e', 'c_a_m_e_l_i_z_e', '-_'], | ||
['came.li.ze', 'came_li_ze', '.'], | ||
['came-li-ze', 'came_li_ze', '-'], | ||
['c+a+m+e+l+i+z+e', 'c_a_m_e_l_i_z_e', '+'], | ||
['customer-session', 'customer_session', null], | ||
['customer Session', 'customer_Session', ' -_'], | ||
['customer-Session', 'customer_Session', ' -_'], | ||
]; | ||
} | ||
} |
Oops, something went wrong.