PHP code example of ko-ko-ko / assert

1. Go to this page and download the library: Download ko-ko-ko/assert library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ko-ko-ko / assert example snippets


use KoKoKo\assert\Assert;

Assert::assert($var, 'var')->notEmpty()->string();

// It's the same as

if (empty($var)) {
    throw new \InvalidArgumentException('Param $var must be not empty');
}

if (!is_string($var)) {
    throw new \InvalidArgumentException('Param $var must be string');
}

// OK
Assert::assert('', 'var')->isEmpty();
Assert::assert(5, 'var', '\LogicException');

// EXCEPTION: var MUST NOT be an object
Assert::assert(new stdClass(), 'var');

// OK
Assert::assert('', 'var')->isEmpty();
Assert::assert('5', 'var')->notEmpty();

// EXCEPTION
Assert::assert('5', 'var')->isEmpty();
Assert::assert([], 'var')->notEmpty();

// OK
Assert::assert([], 'var')->isArray();

// EXCEPTION
Assert::assert('5', 'var')->isArray();

// OK
Assert::assert(false, 'var')->bool();

// EXCEPTION
Assert::assert('5', 'var')->bool();

// OK
Assert::assert(15.2, 'var')->float();

// EXCEPTION
Assert::assert('15.2', 'var')->float();
Assert::assert([], 'var')->float();

// OK
Assert::assert(15, 'var')->int();

// EXCEPTION
Assert::assert(15.2, 'var')->int();
Assert::assert([], 'var')->int();

// OK
Assert::assert(15, 'var')->numeric();

// EXCEPTION
Assert::assert('*', 'var')->numeric();

// OK
Assert::assert(null, 'var')->isNull();
Assert::assert('a', 'var')->notNull();

// EXCEPTION
Assert::assert('a', 'var')->isNull();
Assert::assert(null, 'var')->notNull();

// OK
Assert::assert('5', 'var')->string();

// EXCEPTION
Assert::assert([], 'var')->string();

// OK
Assert::assert(tmpfile(), 'var')->resource();

// EXCEPTION
Assert::assert(5, 'var')->resource();

// OK
Assert::assert('a', 'var')->inArray(['a', 'b']);

// EXCEPTION
Assert::assert('c', 'var')->inArray(['a', 'b']);

// ----------

// EXCEPTION: $range MUST be array
Assert::assert(['a'], 'var')->inArray('a');

// OK
Assert::assert('a', 'var')->isSame('a');

// EXCEPTION
Assert::assert('a', 'var')->isSame('b');

// ----------

// EXCEPTION: $anotherValue MUST be not object
Assert::assert('a', 'var')->isSame(new \stdClass());

// OK
Assert::assert('a', 'var')->notSame('b');

// EXCEPTION
Assert::assert('a', 'var')->notSame('a');

// ----------

// EXCEPTION: $anotherValue MUST be not object
Assert::assert('a', 'var')->notSame(new \stdClass());

// OK
Assert::assert('5', 'var')->digit();

// EXCEPTION
Assert::assert('c', 'var')->digit();

// ----------

// EXCEPTION: var MUST be string
Assert::assert(5, 'var')->digit();

// OK
Assert::assert('a', 'var')->match('/a/');

// EXCEPTION
Assert::assert('b', 'var')->match('/a/');

// ----------

// EXCEPTION: pattern MUST be not empty
Assert::assert('a', 'var')->match('');

// EXCEPTION: var MUST be string
Assert::assert(5, 'var')->match('/a/');

// EXCEPTION: pattern MUST be correct RegExp
Assert::assert('a', 'var')->match('/a');

// OK
Assert::assert('aa', 'var')->glob('a*');

// EXCEPTION
Assert::assert('bb', 'var')->glob('a*');

// ----------

// EXCEPTION: pattern MUST be not empty
Assert::assert('a', 'var')->glob('');

// EXCEPTION: pattern MUST be string
Assert::assert('a', 'var')->glob(false);

// EXCEPTION: var MUST be string
Assert::assert(5, 'var')->glob('/a/');

// OK
Assert::assert('aa', 'var')->length(2);

// EXCEPTION
Assert::assert('bb', 'var')->length(5);

// ----------

// EXCEPTION: length MUST be int
Assert::assert('a', 'var')->length(null);

// EXCEPTION: length MUST be more >= 0
Assert::assert('a', 'var')->length(-2);

// EXCEPTION: var MUST be string
Assert::assert(5, 'var')->length(1);

// OK
Assert::assert('aa', 'var')->lengthLess(5);
Assert::assert('bb', 'var')->lengthMore(1);

// EXCEPTION
Assert::assert('bb', 'var')->lengthLess(1);
Assert::assert('aa', 'var')->lengthMore(5);

// ----------

// EXCEPTION: length MUST be int
Assert::assert('a', 'var')->lengthLess(null);
Assert::assert('a', 'var')->lengthMore(null);

// EXCEPTION: length MUST be more >= 0
Assert::assert('a', 'var')->lengthLess(-2);
Assert::assert('a', 'var')->lengthMore(-2);

// EXCEPTION: var MUST be string
Assert::assert(5, 'var')->lengthLess(1);
Assert::assert([], 'var')->lengthMore(1);

// OK
Assert::assert('aa', 'var')->lengthBetween(1, 5);

// EXCEPTION
Assert::assert('bb', 'var')->lengthBetween(3, 10);

// ----------

// EXCEPTION: form MUST be int
Assert::assert('a', 'var')->lengthBetween(null, 5);

// EXCEPTION: to MUST be int
Assert::assert('a', 'var')->lengthBetween(1, []);

// EXCEPTION: form MUST be more >= 0
Assert::assert('a', 'var')->lengthBetween(-2, 5);

// EXCEPTION: form MUST be more than to
Assert::assert('a', 'var')->lengthBetween(5, 1);

// EXCEPTION: var MUST be string
Assert::assert(5, 'var')->lengthBetween(1);

// OK
Assert::assert(1, 'var')->positive();
Assert::assert(-5, 'var')->negative();

// EXCEPTION
Assert::assert(-1, 'var')->positive();
Assert::assert(0, 'var')->positive();
Assert::assert(10, 'var')->negative();
Assert::assert(0, 'var')->negative();

// ----------

// EXCEPTION: var MUST be int or float
Assert::assert('A', 'var')->positive();
Assert::assert([], 'var')->negative();

// OK
Assert::assert(1, 'var')->less(2);
Assert::assert(10, 'var')->more(5);

// EXCEPTION
Assert::assert(10, 'var')->less(5);
Assert::assert(1, 'var')->more(2);

// ----------

// EXCEPTION: length MUST be int or float
Assert::assert(1, 'var')->less(null);
Assert::assert(1, 'var')->more(null);

// EXCEPTION: var MUST be int or float
Assert::assert('A', 'var')->less(1);
Assert::assert([], 'var')->more(1);

// OK
Assert::assert(2, 'var')->between(1, 5);

// EXCEPTION
Assert::assert(2.5, 'var')->between(3, 10);

// ----------

// EXCEPTION: form MUST be int
Assert::assert(2, 'var')->between(null, 5);

// EXCEPTION: to MUST be int
Assert::assert(2, 'var')->between(1, []);

// EXCEPTION: form MUST be more than to
Assert::assert(2, 'var')->between(5, 1);

// EXCEPTION: var MUST be int or float
Assert::assert('A', 'var')->between(1);

// OK
Assert::assert(['a' => 'b', 'c' => 'd'], 'data')->hasKey('a');

// EXCEPTION
Assert::assert(['a' => 'b', 'c' => 'd'], 'data')->hasKey('e');

// ----------

// EXCEPTION: data MUST be an array
Assert::assert('notArray', 'data')->hasKey(1);

// EXCEPTION: key MUST be string or int
Assert::assert(['a' => 'b', 'c' => 'd'], 'data')->hasKey(null);


// OK
Assert::assert(['a', 'b', 'c'], 'data')->count(3);

// EXCEPTION
Assert::assert(['a', 'c', 'd'], 'data')->count(1);

// ----------

// EXCEPTION: data MUST be an array
Assert::assert('notArray', 'data')->hasKey(1);

// EXCEPTION: count MUST be int
Assert::assert(['a', 'c', 'd'], 'data')->count(null);

// EXCEPTION: count MUST be greater than 0
Assert::assert(['a', 'c', 'd'], 'data')->count(-5);


// OK
$data = [1, 2, 3, 4, 5];

Assert::assert($data, 'data')->forList(
    function (Assert $assert) {
        $assert->int()->positive();
    }
);

// ----------

// EXCEPTION: data MUST be array
Assert::assert('some data', 'data')->forList(
    function (Assert $assert) {}
);

// EXCEPTION: $data: key 'a' MUST be int
Assert::assert(['a' => 'b'], 'data')->forList(
    function (Assert $assert) {}
);


// OK
$data = ['A' => 'A1', 'B' => 'B1', 'C' => 'C1'];

Assert::assert($data, 'data')->forMap(
    function (Assert $keyAssert, Assert $valueAssert) {
        $keyAssert->lengthMore(1);
        $valueAssert->lengthMore(1);
    }
);

// ----------

// EXCEPTION: data MUST be array
Assert::assert('some data', 'data')->forMap(
    function (Assert $keyAssert, Assert $valueAssert) {}
);

// RETURN 'a'
Assert::assert('a', 'var')->get();

// RETURN true
Assert::assert('a', 'var')->toBool()->get();

// RETURN 0.0
Assert::assert('a', 'var')->toFloat()->get();

// RETURN -15.2
Assert::assert('-15.2', 'var')->toFloat()->get();

// ----------

// EXCEPTION: var MUST NOT be array
Assert::assert([], 'var')->toFloat()->get();

// RETURN 0
Assert::assert('a', 'var')->toInt()->get();

// RETURN -15
Assert::assert('-15.2', 'var')->toInt()->get();

// ----------

// EXCEPTION: var MUST NOT be array
Assert::assert([], 'var')->toInt()->get();

// RETURN ''
Assert::assert(false, 'var')->toString()->get();

// RETURN '-15'
Assert::assert(-15, 'var')->toString()->get();

// ----------

// EXCEPTION: var MUST NOT be array
Assert::assert([], 'var')->toString()->get();