LoginSignup
2
1

More than 1 year has passed since last update.

PHP ぼくのかんがえたさいきょうの、NULLやブランクや配列数が0の時にTRUEを返し、数字の0の時はFalseを返す関数、その名はisNULL

Last updated at Posted at 2021-10-06

概要

NULLやブランクや配列数が0の時にTRUEを返し、数字の0の時はFalseを返す関数です。テストもありまぁす。

isNULLの仕様

(第二引数がNULLの時、)第一引数の値と戻り値の関係

第一引数の値 戻り値
null TRUE
文字 - 空 TRUE
文字 - 1文字以上 false
数値(0も含めて) false
配列 - 個数0 TRUE
配列 - 個数1以上 false
空オブジェクト TRUE

第一引数がオブジェクトで第二引数がプロパティ名の時(もしくは配列とキー名)

プロパティが 動作
存在しない TRUE(*__getがあれば呼び出す)
存在する 値をisNULL再帰呼び出しで判定

isNULL

/**
 * @param mixed $val
 * @param string $key
 * @return bool
 */
function isNULL($val, $key = null) {

    if (! isset($val)) {
        return true;
    }

    $type = gettype($val);

    if (is_null($key)) {
        if ('array' === $type && 0 === count($val)) {
            return true;
        }
        if ('string' === $type && '' === $val) {
            return true;
        }
        return false;
    }

    if ('object' === $type) {
        if (property_exists($val, $key)) {
            return isNULL($val->{$key});
        }
        if (method_exists($val, '__get')) {
            return isNULL($val->{$key});
        }
        return true;
    }

    if ('array' === $type) {
        if (! array_key_exists($key, $val)) {
            return true;
        }
        return isNULL($val[$key]);
    }

    return true;
}

isNULLのテスト

/**
 *
 */
public function testIsNULL() {

    $null = null;
    $this->assertTrue(isNULL($null));

    $blank = '';
    $this->assertTrue(isNULL($blank));

    $not_blank = '1';
    $this->assertFalse(isNULL($not_blank));

    $int_zero = 0;
    $this->assertFalse(isNULL($int_zero));

    $empty_arr = [];
    $this->assertTrue(isNULL($empty_arr));

    $not_empty_arr = ['1'];
    $this->assertFalse(isNULL($not_empty_arr));

    $obj = new \stdClass();
    $this->assertTrue(isNULL($obj, 'not_exist_property'));

    $obj->exist_property = 'value';
    $this->assertFalse(isNULL($obj, 'exist_property'));

    $obj->null_property = NULL;
    $this->assertTrue(isNULL($obj, 'null_property'));

    $obj->blank_property = '';
    $this->assertTrue(isNULL($obj, 'blank_property'));

    $obj->zero_property = 0;
    $this->assertFalse(isNULL($obj, 'zero_property'));

    $obj->empty_arr = [];
    $this->assertTrue(isNULL($obj, 'empty_arr'));

    $obj = new hasGetTestClass();
    $this->assertSame('my name is やみ姐さん', $obj->name);
    $this->assertFalse(isNULL($obj, 'name'));

    $obj = 4; 
    $this->assertTrue(isNULL($obj, 'ratio'));
}

/**
 * Class hasGetTestClass
 */
class hasGetTestClass {

    private $_data = [
        'name' => 'my name is やみ姐さん',
    ];

    public function __get($key) {
        if (array_key_exists($key, $this->_data)) {
            return $this->_data[$key];
        }
        return null;
    }
}

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1