-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from niden/1.x
- Loading branch information
Showing
9 changed files
with
407 additions
and
7 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
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,85 @@ | ||
<?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\Traits\Helper\Str; | ||
|
||
use function array_map; | ||
use function implode; | ||
use function lcfirst; | ||
use function preg_split; | ||
use function str_replace; | ||
use function ucfirst; | ||
|
||
use const PREG_SPLIT_DELIM_CAPTURE; | ||
use const PREG_SPLIT_NO_EMPTY; | ||
|
||
/** | ||
* Converts strings to upperCamelCase or lowerCamelCase | ||
*/ | ||
trait CamelizeTrait | ||
{ | ||
/** | ||
* @param string $text | ||
* @param string $delimiters | ||
* @param bool $lowerFirst | ||
* | ||
* @return string | ||
*/ | ||
public function toCamelize( | ||
string $text, | ||
string $delimiters = '\-_', | ||
bool $lowerFirst = false | ||
): string { | ||
$exploded = $this->processArray($text, $delimiters); | ||
|
||
$output = array_map( | ||
function ($element) { | ||
return ucfirst(mb_strtolower($element)); | ||
}, | ||
$exploded | ||
); | ||
|
||
$result = implode('', $output); | ||
|
||
if (true === $lowerFirst) { | ||
$result = lcfirst($result); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @param string $text | ||
* @param string|null $delimiters | ||
* | ||
* @return array | ||
*/ | ||
protected function processArray(string $text, string $delimiters = '\-_'): array | ||
{ | ||
/** | ||
* 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,39 @@ | ||
<?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\Traits\Helper\Str; | ||
|
||
/** | ||
* Converts strings to non camelized style | ||
*/ | ||
trait UncamelizeTrait | ||
{ | ||
/** | ||
* @param string $text | ||
* @param string $delimiters | ||
* | ||
* @return string | ||
*/ | ||
public function toUncamelize( | ||
string $text, | ||
string $delimiter = '_' | ||
): string { | ||
$text = (string) preg_replace( | ||
'/[A-Z]/', | ||
$delimiter . '\\0', | ||
lcfirst($text) | ||
); | ||
|
||
return mb_convert_case($text, MB_CASE_LOWER, 'UTF-8'); | ||
} | ||
} |
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,38 @@ | ||
<?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 | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Tests\Fixtures\Helper\Str; | ||
|
||
use Phalcon\Traits\Helper\Str\CamelizeTrait; | ||
|
||
class CamelizeFixture | ||
{ | ||
use CamelizeTrait; | ||
|
||
/** | ||
* Camelizes a string | ||
* | ||
* @param string $text | ||
* @param string $delimiters | ||
* @param bool $lowerFirst | ||
* | ||
* @return string | ||
*/ | ||
public function camelize( | ||
string $text, | ||
string $delimiters, | ||
bool $lowerFirst | ||
): string { | ||
return $this->toCamelize($text, $delimiters, $lowerFirst); | ||
} | ||
} |
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,36 @@ | ||
<?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 | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Tests\Fixtures\Helper\Str; | ||
|
||
use Phalcon\Traits\Helper\Str\UncamelizeTrait; | ||
|
||
class UncamelizeFixture | ||
{ | ||
use UncamelizeTrait; | ||
|
||
/** | ||
* Uncamelizes a string | ||
* | ||
* @param string $text | ||
* @param string $delimiters | ||
* | ||
* @return string | ||
*/ | ||
public function Uncamelize( | ||
string $text, | ||
string $delimiter = '_' | ||
): string { | ||
return $this->toUncamelize($text, $delimiter); | ||
} | ||
} |
Oops, something went wrong.