PHP code example of gmazzap / pentothal
1. Go to this page and download the library: Download gmazzap/pentothal 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/ */
gmazzap / pentothal example snippets
$data = [
'foo',
1,
true,
'bar',
[],
''
];
$strings = array_filter($data, 'is_string'); // ['foo', 'bar', '']
$strings = array_filter($data, function($item) {
return is_string($item) && $item !== '';
});
use Pentothal as P;
$strings = array_filter($data, P\combine(P\isType('string'), P\isNotEmpty()));
use Pentothal as P;
$data = ['yes', 'no', 'Yes', 'No', 'YES', 'NO' ];
$yes = array_filter($data, P\applyAfter('strtolower', isSame('yes'))); // ['yes', 'Yes', 'YES']
use Pentothal as P;
// some example data
$countableOne = new \ArrayObject(['foo' => 'bar']);
$countableTwo = new \ArrayObject(['a' => 'a', 'b' => 'b']);
$plainObj = new \stdClass();
$string1 = 'a';
$string3 = 'abc';
$number1 = 1;
$number3 = 3;
$list = [
'a' => $countableOne,
'b' => $countableTwo,
'c' => $plainObj,
'd' => $string1,
'e' => $string3,
'f' => $number1,
'g' => $number3
];
$predicate = P\combine(
P\pool(P\isNotObject(), P\isType('Countable')), // filter out: ['c' => $plainObj]
P\pool(P\isNotString(), P\size(3)), // filter out: ['d' => $string1]
P\pool(P\isString(), P\size(1)) // filter out: ['a' => $countableTwo, 'g' => $number3]
);
$negatePredicate = negate($predicate);
$in = array_filter($list, $predicate);
$out = array_filter($list, $negatePredicate);
var_dump($in); // array('a' => $countableOne, 'e' => $string3, 'f' => $number1];
var_dump($out); // array('b' => $countableTwo, 'c' => $plainObj, 'd' => $string1, 'g' => $number3];