PHP code example of dazet / type-utils
1. Go to this page and download the library: Download dazet/type-utils 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/ */
dazet / type-utils example snippets
$array = ['string', 123, [], null];
array_filter($array, StringUtil::canBeString); // ['string', 123, null]
$a = ['string', 123, [], new stdClass()];
array_map(StringUtil::toStringOrNull, $a); // ['string', '123', null, null]
$a = ['string', 123, null];
array_map(StringUtil::toString, $a); // ['string', '123', '']
$b = ['string', new stdClass()];
array_map(StringUtil::toString, $b); // throws InvalidTypeException
$array = [123, 1.23, '123', 'string', null];
array_filter($array, NumberUtil::canBeNumber); // [123, 1.23, '123', null]
$a = [123, 1.23, 'string'];
array_map(NumberUtil::toIntOrNull, $a); // [123, 1, null]
$a = [123, 1.23, 'string'];
array_map(NumberUtil::toFloatOrNull, $a); // [123.0, 1.23, null]
$array = [['array'], new ArrayObject(['object']), 'string'];
array_filter($array, ArrayUtil::canBeArray); // [['array'], new ArrayObject(['object'])]
$a = ['a' => ['array'], 'b' => 'string', 'c' => new ArrayObject(['object'])];
array_map(ArrayUtil::toArrayOrNull, $a); // ['a' => ['array'], 'b' => null, 'c' => ['object']]
$iterate = function() {
yield 9 => 'nine';
yield 9 => 'nine';
yield 9 => 'nine';
};
array_map(ArrayUtil::toArrayOrNull, $iterate()); // [9 => 'nine']
$a = ['a' => ['array'], 'b' => 'string', 'c' => new ArrayObject(['object'])];
array_map(ArrayUtil::toArrayListOrNull, $a); // [['array'], null, ['object']]
$iterate = function() {
yield 9 => 'nine';
yield 9 => 'nine';
yield 9 => 'nine';
};
array_map(ArrayUtil::toArrayOrNull, $iterate()); // ['nine', 'nine', 'nine']
$now = new DateTime('now');
$a = ['today', $now, 'never'];
array_filter($a, DateUtil::canBeDate); // ['today', $now]