Skip to content

Commit

Permalink
[4.0.x] - Added Arr helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed Feb 17, 2019
1 parent 655eafe commit f8153d3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
55 changes: 55 additions & 0 deletions phalcon/helper/arr.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Helper;

use Phalcon\Helper\Exception;

/**
* Phalcon\Helper\Arr
*
* Array helper methods
*/
class Arr
{
/**
* Helper method to check an array for an element. If it exists it returns it,
* if not, it returns the supplied default value
*/
public static function get(var key, array collection, var defaultValue = null) -> var
{
var value;

if typeof !== "string" || true !== is_numeric(key) {
throw new Exception("Key must be either string or numeric");
}

if likely fetch value, collection[name] {
return value;
}

return defaultValue;
}

/**
* Helper method to add an element to an array with an optional key. Returns
* the final array
*/
public static function set(var value, array collection = [], var key = null) -> array
{
if null === key {
let collection[] = value;
} else {
let collection[key] = value;
}

return collection;
}
}
22 changes: 22 additions & 0 deletions phalcon/helper/exception.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Helper;

/**
* Phalcon\Helper\Tag\Exception
*
* Exceptions thrown in Phalcon\Helper will use this class
*
*/
class Exception extends \Phalcon\Exception
{

}

0 comments on commit f8153d3

Please sign in to comment.