Skip to content

Commit

Permalink
Merge pull request #15851 from niden/T15850-camelize-lowercase
Browse files Browse the repository at this point in the history
T15850 camelize lowercase
  • Loading branch information
niden authored Jan 3, 2022
2 parents a6c3adb + 5b223c6 commit 920db74
Show file tree
Hide file tree
Showing 48 changed files with 537 additions and 141 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
- `Phalcon\Mvc\View\Engine\Volt\Compiler::functionCall()` to check for container presence before checking the `tag` service [#15842](https://github.com/phalcon/cphalcon/issues/15842)
- `Phalcon\Di\FactoryDefault()` to set `assets` and `tag` as shared services [#15847](https://github.com/phalcon/cphalcon/issues/15847)
- `Phalcon\Forms\Element\AbstractElement::getLocalTagFactory()` to return the tagFactory from itself, the form, the DI or a new instance [#15847](https://github.com/phalcon/cphalcon/issues/15847)
- Changed references to `sha1` with `hash("sha256", $data)` to ensure that there are no collisions from the hashing algorithm [#15844](https://github.com/phalcon/cphalcon/issues/15844)
- Changed `Phalcon\Support\Helper\Str\Camelize` to accept a third boolean parameter indicating whether the result will have the first letter capitalized or not [#15850](https://github.com/phalcon/cphalcon/issues/15850)

## Added
- Added `Phalcon\Support\Helper\Str\KebabCase`, `Phalcon\Support\Helper\Str\PascalCase` and `Phalcon\Support\Helper\Str\SnakeCase` helpers [#15850](https://github.com/phalcon/cphalcon/issues/15850)

# [5.0.0beta1](https://github.com/phalcon/cphalcon/releases/tag/v5.0.0beta1) (2021-12-24)

Expand Down
2 changes: 1 addition & 1 deletion phalcon/Assets/Asset.zep
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Asset implements AssetInterface

let key = this->getType() . ":" . this->getPath();

return sha1(key);
return hash("sha256", key);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion phalcon/Assets/Inline.zep
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class $Inline implements AssetInterface

let key = this->getType() . ":" . this->getContent();

return sha1(key);
return hash("sha256", key);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions phalcon/Autoload/Loader.zep
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Loader extends AbstractEventsAware
*/
public function __construct(bool isDebug = false)
{
let this->extensions[sha1("php")] = "php",
let this->extensions[hash("sha256", "php")] = "php",
this->isDebug = isDebug;
}

Expand Down Expand Up @@ -119,7 +119,7 @@ class Loader extends AbstractEventsAware
*/
public function addDirectory(string directory) -> <Loader>
{
let this->directories[sha1(directory)] = directory;
let this->directories[hash("sha256", directory)] = directory;

return this;
}
Expand All @@ -133,7 +133,7 @@ class Loader extends AbstractEventsAware
*/
public function addExtension(string extension) -> <Loader>
{
let this->extensions[sha1(extension)] = extension;
let this->extensions[hash("sha256", extension)] = extension;

return this;
}
Expand All @@ -147,7 +147,7 @@ class Loader extends AbstractEventsAware
*/
public function addFile(string file) -> <Loader>
{
let this->files[sha1(file)] = file;
let this->files[hash("sha256", file)] = file;

return this;
}
Expand Down Expand Up @@ -409,7 +409,7 @@ class Loader extends AbstractEventsAware

if (!merge) {
let this->extensions = [],
this->extensions[sha1("php")] = "php";
this->extensions[hash("sha256", "php")] = "php";
}

for extension in extensions {
Expand Down Expand Up @@ -737,7 +737,7 @@ class Loader extends AbstractEventsAware
for directory in directories {
let directory = rtrim(directory, dirSeparator) . dirSeparator;

let results[sha1(directory)] = directory;
let results[hash("sha256", directory)] = directory;
}

return results;
Expand Down
34 changes: 11 additions & 23 deletions phalcon/Support/Helper/Str/Camelize.zep
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,30 @@
namespace Phalcon\Support\Helper\Str;

/**
* 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 {
var delims, exploded, output;
var result;

if !delimiters {
let delims = "_-";
} else {
let delims = delimiters;
}

let exploded = preg_split(
"/[" . delims . "]+/",
text,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
let result = parent::__invoke(text, delimiters);

let output = array_map(
function (element) {
return ucfirst(mb_strtolower(element));
},
exploded
);
if (lowerFirst === true) {
let result = lcfirst(result);
}

return implode("", output);
return result;
}
}
34 changes: 34 additions & 0 deletions phalcon/Support/Helper/Str/KebabCase.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

/**
* 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.
*/

namespace Phalcon\Support\Helper\Str;

/**
* 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 {
var output;

let output = this->processArray(text, delimiters);

return implode("_", output);
}
}
75 changes: 75 additions & 0 deletions phalcon/Support/Helper/Str/PascalCase.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

/**
* 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.
*/

namespace Phalcon\Support\Helper\Str;

/**
* 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 {
var exploded, output;

let exploded = this->processArray(text, delimiters);

let 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
{
var result;

if delimiters === null {
let 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
*/
if strpos(delimiters, "\\-") !== false || strpos(delimiters, "-") !== false {
let delimiters = str_replace(["\\-", "-"], ["", ""], delimiters),
delimiters = "-" . $delimiters;
}

let result = preg_split(
"/[" . delimiters . "]+/",
text,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);

return (false === result) ? [] : result;
}
}
34 changes: 34 additions & 0 deletions phalcon/Support/Helper/Str/SnakeCase.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

/**
* 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.
*/

namespace Phalcon\Support\Helper\Str;

/**
* 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 {
var output;

let output = this->processArray(text, delimiters);

return implode("-", output);
}
}
8 changes: 8 additions & 0 deletions phalcon/Support/HelperFactory.zep
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use Phalcon\Factory\AbstractFactory;
* @method string decode(string $data, bool $associative = false, int $depth = 512, int $options = 0)
* @method string encode($data, int $options = 0, int $depth = 512)
* @method bool between(int $value, int $start, int $end)
* @method string camelize(string $text, string $delimiters = null, bool $lowerFirst = false)
* @method string concat(string $delimiter, string $first, string $second, string ...$arguments)
* @method int countVowels(string $text)
* @method string decapitalize(string $text, bool $upperRest = false, string $encoding = 'UTF-8')
Expand All @@ -56,14 +57,18 @@ use Phalcon\Factory\AbstractFactory;
* @method bool isLower(string $text, string $encoding = 'UTF-8')
* @method bool isPalindrome(string $text)
* @method bool isUpper(string $text, string $encoding = 'UTF-8')
* @method string kebabCase(string $text, string $delimiters = null)
* @method int len(string $text, string $encoding = 'UTF-8')
* @method string lower(string $text, string $encoding = 'UTF-8')
* @method string pascalCase(string $text, string $delimiters = null)
* @method string prefix($text, string $prefix)
* @method string random(int $type = 0, int $length = 8)
* @method string reduceSlashes(string $text)
* @method bool startsWith(string $haystack, string $needle, bool $ignoreCase = true)
* @method string snakeCase(string $text, string $delimiters = null)
* @method string suffix($text, string $suffix)
* @method string ucwords(string $text, string $encoding = 'UTF-8')
* @method string uncamelize(string $text, string $delimiters = '_')
* @method string underscore(string $text)
* @method string upper(string $text, string $encoding = 'UTF-8')
*/
Expand Down Expand Up @@ -167,11 +172,14 @@ class HelperFactory extends AbstractFactory
"isLower" : "Phalcon\\Support\\Helper\\Str\\IsLower",
"isPalindrome" : "Phalcon\\Support\\Helper\\Str\\IsPalindrome",
"isUpper" : "Phalcon\\Support\\Helper\\Str\\IsUpper",
"kebabCase" : "Phalcon\\Support\\Helper\\Str\\KebabCase",
"len" : "Phalcon\\Support\\Helper\\Str\\Len",
"lower" : "Phalcon\\Support\\Helper\\Str\\Lower",
"pascalCase" : "Phalcon\\Support\\Helper\\Str\\PascalCase",
"prefix" : "Phalcon\\Support\\Helper\\Str\\Prefix",
"random" : "Phalcon\\Support\\Helper\\Str\\Random",
"reduceSlashes" : "Phalcon\\Support\\Helper\\Str\\ReduceSlashes",
"snakeCase" : "Phalcon\\Support\\Helper\\Str\\SnakeCase",
"startsWith" : "Phalcon\\Support\\Helper\\Str\\StartsWith",
"suffix" : "Phalcon\\Support\\Helper\\Str\\Suffix",
"ucwords" : "Phalcon\\Support\\Helper\\Str\\Ucwords",
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Assets/Asset/Css/GetAssetKeyCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Phalcon\Assets\Asset\Css;
use UnitTester;

use function hash;

/**
* Class GetAssetKeyCest
*
Expand All @@ -38,7 +40,7 @@ public function assetsAssetCssGetAssetKey(UnitTester $I)
$path = 'css/docs.css';

$asset = new Css($path);
$assetKey = sha1('css:' . $path);
$assetKey = hash("sha256", 'css:' . $path);
$actual = $asset->getAssetKey();

$I->assertEquals($assetKey, $actual);
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Assets/Asset/GetAssetKeyCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Phalcon\Assets\Asset;
use UnitTester;

use function hash;

/**
* Class GetAssetKeyCest
*
Expand All @@ -41,7 +43,7 @@ public function assetsAssetGetAssetKey(UnitTester $I, Example $example)

$asset = new Asset($example['type'], $example['path']);

$assetKey = sha1($example['type'] . ':' . $example['path']);
$assetKey = hash("sha256", $example['type'] . ':' . $example['path']);
$actual = $asset->getAssetKey();
$I->assertEquals($assetKey, $actual);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Assets/Asset/Js/GetAssetKeyCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Phalcon\Assets\Asset\Js;
use UnitTester;

use function hash;

/**
* Class GetAssetKeyCest
*
Expand All @@ -37,7 +39,7 @@ public function assetsAssetJsGetAssetKey(UnitTester $I)

$path = 'js/jquery.js';
$asset = new Js($path);
$expected = sha1('js:' . $path);
$expected = hash("sha256", 'js:' . $path);
$actual = $asset->getAssetKey();

$I->assertEquals($expected, $actual);
Expand Down
1 change: 0 additions & 1 deletion tests/unit/Assets/Collection/AddInlineCssCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Phalcon\Tests\Unit\Assets\Collection;

use Phalcon\Assets\Collection;
use Phalcon\Assets\Inline\Css;
use UnitTester;

/**
Expand Down
1 change: 0 additions & 1 deletion tests/unit/Assets/Collection/AddInlineJsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Phalcon\Tests\Unit\Assets\Collection;

use Phalcon\Assets\Collection;
use Phalcon\Assets\Inline\Js;
use UnitTester;

/**
Expand Down
Loading

0 comments on commit 920db74

Please sign in to comment.