Skip to content

Commit

Permalink
fix(support\helper\str): Added third parameter to Camelize to lowerCa…
Browse files Browse the repository at this point in the history
…seFirst if need be; Added PascalCase, SnakeCase and KebabCase helpers

Related phalcon/cphalcon#15850

Refs phalcon#6.0.x
  • Loading branch information
niden committed Jan 2, 2022
1 parent 9b1502b commit a059a66
Show file tree
Hide file tree
Showing 8 changed files with 459 additions and 44 deletions.
35 changes: 17 additions & 18 deletions src/Support/Helper/Str/Camelize.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,41 @@

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 camelize style
* Converts strings to upperCamelCase or lowerCamelCase
*/
class Camelize
class Camelize extends PascalCase
{
/**
* @param string $text
* @param string|null $delimiters
* @param bool $lowerFirst
*
* @return string
*/
public function __invoke(
string $text,
string $delimiters = null
string $delimiters = null,
bool $lowerFirst = false
): string {
$delimiters = $delimiters ?: '_-';
$exploded = preg_split(
'/[' . $delimiters . ']+/',
$text,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
$output = array_map(
function ($element) {
return ucfirst(mb_strtolower($element));
},
$exploded
);

return implode('', $output);
$result = parent::__invoke($text, $delimiters);

if (true === $lowerFirst) {
$result = lcfirst($result);
}

return $result;
}
}
50 changes: 50 additions & 0 deletions src/Support/Helper/Str/KebabCase.php
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);
}
}
82 changes: 82 additions & 0 deletions src/Support/Helper/Str/PascalCase.php
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;
}
}
50 changes: 50 additions & 0 deletions src/Support/Helper/Str/SnakeCase.php
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);
}
}
54 changes: 28 additions & 26 deletions tests/unit/Support/Helper/Str/CamelizeCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
use Phalcon\Support\Helper\Str\Camelize;
use UnitTester;

/**
* Class CamelizeCest
*
* @package Phalcon\Tests\Unit\Support\Helper\Str
*/
class CamelizeCest
{
/**
Expand All @@ -43,8 +38,9 @@ public function supportHelperStrCamelize(UnitTester $I, Example $example)
$value = $example[0];
$expected = $example[1];
$delimiter = $example[2];
$lowercase = $example[3];

$actual = $object($value, $delimiter);
$actual = $object($value, $delimiter, $lowercase);
$I->assertEquals($expected, $actual);
}

Expand All @@ -54,26 +50,32 @@ public function supportHelperStrCamelize(UnitTester $I, Example $example)
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', 'CAMELIZE', null],
['Camelize', 'Camelize', null],
['camel_ize', 'CamelIze', null],
['CameLize', 'Camelize', null],
['c_a-m_e-l_i-z_e', 'CAMELIZE', null],
['came_li_ze', 'CameLiZe', null],
['=_camelize', '=Camelize', '_'],
['camelize', 'Camelize', '_'],
['came_li_ze', 'CameLiZe', '_'],
['came#li#ze', 'CameLiZe', '#'],
['came li ze', 'CameLiZe', ' '],
['came.li^ze', 'CameLiZe', '.^'],
['c_a-m_e-l_i-z_e', 'CAMELIZE', '-_'],
['came.li.ze', 'CameLiZe', '.'],
['came-li-ze', 'CameLiZe', '-'],
['c+a+m+e+l+i+z+e', 'CAMELIZE', '+'],
['camelize', 'Camelize', null, false],
['CameLiZe', 'Camelize', null, false],
['cAmeLize', 'Camelize', null, false],
['123camelize', '123camelize', null, false],
['c_a_m_e_l_i_z_e', 'CAMELIZE', null, false],
['Camelize', 'Camelize', null, false],
['camel_ize', 'CamelIze', null, false],
['CameLize', 'Camelize', null, false],
['c_a-m_e-l_i-z_e', 'CAMELIZE', null, false],
['came_li_ze', 'CameLiZe', null, false],
['=_camelize', '=Camelize', '_', false],
['camelize', 'Camelize', '_', false],
['came_li_ze', 'CameLiZe', '_', false],
['came#li#ze', 'CameLiZe', '#', false],
['came li ze', 'CameLiZe', ' ', false],
['came.li^ze', 'CameLiZe', '.^', false],
['c_a-m_e-l_i-z_e', 'CAMELIZE', '-_', false],
['came.li.ze', 'CameLiZe', '.', false],
['came-li-ze', 'CameLiZe', '-', false],
['c+a+m+e+l+i+z+e', 'CAMELIZE', '+', false],
['customer-session', 'CustomerSession', null, false],
['customer Session', 'CustomerSession', ' -_', false],
['customer-Session', 'CustomerSession', ' -_', false],
['customer-session', 'customerSession', null, true],
['customer Session', 'customerSession', ' -_', true],
['customer-Session', 'customerSession', ' -_', true],
];
}
}
78 changes: 78 additions & 0 deletions tests/unit/Support/Helper/Str/KebabCaseCest.php
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', ' -_'],
];
}
}
Loading

0 comments on commit a059a66

Please sign in to comment.