Skip to content

Commit

Permalink
adding support for empty strings in arrays for isEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
viveleroi committed Mar 30, 2011
1 parent 4e9f939 commit a4e2837
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions Peregrine.php 100755 → 100644
Expand Up @@ -193,7 +193,8 @@ public function getRawSource(){
/**
* Determines whether or not a value is empty. You may provide
* an array of additional characters that should be counted as
* empty values.
* empty values. Arrays are imploded and checked for empty values
* so array('') will be caught.
*
* Empty returns true if the key does not exist at all.
*
Expand All @@ -208,7 +209,11 @@ public function isEmpty($key, $count_as_empty = false){
return true;
} else {
$val = $this->getRaw($key);
if(is_array($val)){
$val = implode('',$val);
}
$val = $count_as_empty ? str_replace($count_as_empty, '', $val) : $val;
$val = trim($val);
return empty($val);
}
}
Expand Down Expand Up @@ -664,7 +669,7 @@ public function getInt($key = false, $default = NULL){
*/
public function getDigits($key = false, $default = NULL){
$default = $default === NULL ? false : $default;
if($this->keyExists($key) && !$this->equals($key, '', true)){
if($this->keyExists($key) && !$this->equals($key, '', true) && !$this->equals($key, NULL, true)){
// We need to mimic the type back to the user that they gave us
$type = gettype($this->getKey($key));
$clean = preg_replace('/[^\d]/', '', $this->getKey($key));
Expand All @@ -686,7 +691,7 @@ public function getDigits($key = false, $default = NULL){
*/
public function getFloat($key = false, $default = NULL){
$default = $default === NULL ? false : $default;
if($this->keyExists($key) && !$this->equals($key, '', true)){
if($this->keyExists($key) && !$this->equals($key, '', true) && !$this->equals($key, NULL, true)){
// We need to mimic the type back to the user that they gave us
$type = gettype($this->getKey($key));
$clean = preg_replace('/[^\d\.]/', '', $this->getKey($key));
Expand All @@ -707,7 +712,7 @@ public function getFloat($key = false, $default = NULL){
*/
public function getCurrency($key = false, $default = NULL){
$default = $default === NULL ? false : $default;
if($this->keyExists($key) && !$this->equals($key, '', true)){
if($this->keyExists($key) && !$this->equals($key, '', true) && !$this->equals($key, NULL, true)){
// We need to mimic the type back to the user that they gave us
$type = gettype($this->getKey($key));
$clean = preg_replace('/[^\d\.,\$]/', '', $this->getKey($key));
Expand Down
3 changes: 2 additions & 1 deletion PeregrineTest.php
Expand Up @@ -51,12 +51,13 @@ public function test_getRaw() {
*
*/
public function test_isEmpty() {
$my_arr = array('test'=>0,'test2'=>'full');
$my_arr = array('test'=>0,'test2'=>'full','arr'=>array(''));
$arr = Peregrine::sanitize( $my_arr );
$this->assertEquals(true, $arr->isEmpty('test'));
$this->assertEquals(false, $arr->isEmpty('test2'));
$this->assertEquals(true, $arr->isEmpty('fake'));
$this->assertEquals(true, $arr->isEmpty('test2','full')); // count "full" as empty
$this->assertEquals(true, $arr->isEmpty('arr'));
}


Expand Down

0 comments on commit a4e2837

Please sign in to comment.