Skip to content

Commit

Permalink
[#13439] - Adjusted Helper classes
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed May 9, 2019
1 parent 4fd6ffc commit 361181d
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 5 deletions.
43 changes: 40 additions & 3 deletions phalcon/Helper/Arr.zep
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Arr
* @param array $collection
* @param int $size
* @param bool $preserveKeys
*
* @return array
*/
final public static function chunk(array! collection, int size, bool preserveKeys = false) -> array
{
Expand All @@ -37,6 +39,8 @@ class Arr
*
* @param array $collection
* @param callable $method
*
* @return mixed
*/
final public static function first(array! collection, var method = null) -> var
{
Expand All @@ -53,6 +57,8 @@ class Arr
*
* @param array $collection
* @param callable $method
*
* @return mixed
*/
final public static function firstKey(array! collection, var method = null) -> var
{
Expand All @@ -74,6 +80,8 @@ class Arr
*
* @param array $collection
* @param bool $deep
*
* @return array
*/
final public static function flatten(array! collection, bool deep = false) -> array
{
Expand Down Expand Up @@ -114,6 +122,8 @@ class Arr
*
* @param array $collection
* @param callable $method
*
* @return array
*/
final public static function group(array! collection, var method) -> array
{
Expand All @@ -139,6 +149,11 @@ class Arr

/**
* Helper method to get an array element or a default
*
* @param array $collection
* @param mixed $index
*
* return bool
*/
final public static function has(array! collection, var index) -> bool
{
Expand All @@ -164,6 +179,8 @@ class Arr
*
* @param array $collection
* @param callable $method
*
* return mixed
*/
final public static function last(array! collection, var method = null) -> var
{
Expand All @@ -184,6 +201,8 @@ class Arr
*
* @param array $collection
* @param callable $method
*
* @return mixed
*/
final public static function lastKey(array! collection, var method = null) -> var
{
Expand All @@ -203,9 +222,9 @@ class Arr
/**
* Sorts a collection of arrays or objects by key
*
* @param array $collection
* @param [type] $attr
* @param [type] $order
* @param array $collection
* @param mixed $attr
* @param string $order
*
* @return array
*/
Expand Down Expand Up @@ -239,6 +258,8 @@ class Arr
*
* @param array $collection
* @param string $element
*
* @return array
*/
final public static function pluck(array! collection, string element) -> array
{
Expand All @@ -259,6 +280,12 @@ class Arr

/**
* Helper method to set an array element
*
* @param array $collection
* @param mixed $value
* @param mixed $index
*
* @return array
*/
final public static function set(array! collection, var value, var index = null) -> array
{
Expand All @@ -276,6 +303,8 @@ class Arr
*
* @param array $collection
* @param int $elements
*
* @return array
*/
final public static function sliceLeft(array! collection, int elements = 1) -> array
{
Expand All @@ -287,6 +316,8 @@ class Arr
*
* @param array $collection
* @param int $elements
*
* @return array
*/
final public static function sliceRight(array! collection, int elements = 1) -> array
{
Expand All @@ -298,6 +329,8 @@ class Arr
* values as another
*
* @param array $collection
*
* @return array
*/
final public static function split(array! collection) -> array
{
Expand All @@ -310,6 +343,8 @@ class Arr
*
* @param array $collection
* @param callable $method
*
* @return bool
*/
final public static function validateAll(array! collection, var method) -> bool
{
Expand All @@ -322,6 +357,8 @@ class Arr
*
* @param array $collection
* @param callable $method
*
* @return bool
*/
final public static function validateAny(array! collection, var method) -> bool
{
Expand Down
97 changes: 95 additions & 2 deletions phalcon/Helper/Str.zep
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class Str
* echo Str::camelize("co_co-bon_go", "-"); // Co_coBon_go
* echo Str::camelize("co_co-bon_go", "_-"); // CoCoBonGo
* </code>
*
* @param string $text
* @param mixed $delimiter
*
* @return string
*/
final public static function camelize(string! text, var delimiter = null) -> string
{
Expand All @@ -57,13 +62,14 @@ class Str
* );
*
* echo $str; // /tmp/folder_1/folder_2/folder_3/
* echo $str;
* </code>
*
* @param string separator
* @param string a
* @param string b
* @param string ...N
*
* @return string
*/
final public static function concat() -> string
{
Expand Down Expand Up @@ -123,6 +129,7 @@ class Str
*
* @param string $string
* @param bool $upperRest
* @param string $encoding
*
* @return string
*/
Expand Down Expand Up @@ -179,6 +186,13 @@ class Str
* "/"
* );
* </code>
*
* @param string $text
* @param string $leftDelimiter
* @param string $rightDelimiter
* @param string $separator
*
* @return string
*/
final public static function dynamic(
string! text,
Expand Down Expand Up @@ -231,6 +245,12 @@ class Str
* echo Str::endsWith("Hello", "LLO", false); // false
* echo Str::endsWith("Hello", "LLO"); // true
* </code>
*
* @param string $text
* @param string $end
* @param bool $ignoreCase
*
* @return bool
*/
final public static function endsWith(string text, string end, bool ignoreCase = true) -> bool
{
Expand Down Expand Up @@ -260,6 +280,34 @@ class Str
}
}

/**
* Accepts a file name (without extension) and returns a calculated folder structure with the
* filename in the end
*
* <code>
* use Phalcon\Helper\Str;
*
* echo Str::folderFromFile("file1234.jpg"); // /fi/le/12/file1234.jpg
* </code>
*
* @param string $file
*
* @return string
*/
final public static function folderFromFile(string! file) -> string
{
var name, start;

let name = pathinfo(file, PATHINFO_FILENAME),
start = substr(name, 0, -2);

if !start {
let start = substr(name, 0, 1);
}

return implode("/", str_split(start, 2)) . "/";
}

/**
* Makes an underscored or dashed phrase human-readable
*
Expand All @@ -269,6 +317,10 @@ class Str
* echo Str::humanize("start-a-horse"); // "start a horse"
* echo Str::humanize("five_cats"); // "five cats"
* </code>
*
* @param string $text
*
* @return string
*/
final public static function humanize(string! text) -> string
{
Expand Down Expand Up @@ -302,6 +354,11 @@ class Str
* echo Str::increment("a"); // "a_1"
* echo Str::increment("a_1"); // "a_2"
* </code>
*
* @param string $text
* @param string $separator
*
* @return string
*/
final public static function increment(string text, string separator = "_") -> string
{
Expand Down Expand Up @@ -335,7 +392,8 @@ class Str
/**
* Returns true if the given string is lower case, false otherwise.
*
* @param string text
* @param string $text
* @param string $encoding
*
* @return bool
*/
Expand Down Expand Up @@ -364,6 +422,7 @@ class Str
* Returns true if the given string is upper case, false otherwise.
*
* @param string text
* @param string encoding
*
* @return bool
*/
Expand All @@ -383,6 +442,11 @@ class Str
* <code>
* echo Phalcon\Helper\Str::lower("HELLO"); // hello
* </code>
*
* @param string $text
* @param string $encoding
*
* @return string
*/
final public static function lower(string! text, string! encoding = "UTF-8") -> string
{
Expand All @@ -406,6 +470,11 @@ class Str
*
* echo Str::random(Str::RANDOM_ALNUM); // "aloiwkqz"
* </code>
*
* @param int $type
* @param int $length
*
* @return string
*/
final public static function random(int type = 0, long length = 8) -> string
{
Expand Down Expand Up @@ -462,6 +531,10 @@ class Str
* echo Phalcon\Helper\Str::reduceSlashes("foo//bar/baz"); // foo/bar/baz
* echo Phalcon\Helper\Str::reduceSlashes("http://foo.bar///baz/buz"); // http://foo.bar/baz/buz
* </code>
*
* @param string $text
*
* @return string
*/
final public static function reduceSlashes(string! text) -> string
{
Expand All @@ -478,6 +551,12 @@ class Str
* echo Str::startsWith("Hello", "he", false); // false
* echo Str::startsWith("Hello", "he"); // true
* </code>
*
* @param string $text
* @param string $start
* @param bool $ignoreCase
*
* @return bool
*/
final public static function startsWith(string! text, string! start, bool ignoreCase = true) -> bool
{
Expand All @@ -493,6 +572,11 @@ class Str
* echo Str::uncamelize("CocoBongo"); // coco_bongo
* echo Str::uncamelize("CocoBongo", "-"); // coco-bongo
* </code>
*
* @param string $text
* @param mixed $delimiter
*
* @return string
*/
final public static function uncamelize(string! text, var delimiter = null) -> string
{
Expand All @@ -508,6 +592,10 @@ class Str
* echo Str::underscore("look behind"); // "look_behind"
* echo Str::underscore("Awesome Phalcon"); // "Awesome_Phalcon"
* </code>
*
* @param string $text
*
* @return string
*/
final public static function underscore(string! text) -> string
{
Expand All @@ -521,6 +609,11 @@ class Str
* <code>
* echo Phalcon\Helper\Str::upper("hello"); // HELLO
* </code>
*
* @param string $text
* @param string $encoding
*
* @return string
*/
final public static function upper(string! text, string! encoding = "UTF-8") -> string
{
Expand Down

0 comments on commit 361181d

Please sign in to comment.