PHP code example of peekandpoke / psi
1. Go to this page and download the library: Download peekandpoke/psi 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/ */
peekandpoke / psi example snippets
/** @var Person[] $input */
$input = $service->getStuff();
/** @var Person[] $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->filter(function (Person $p) { return $p->getAge() >= 18; })
->toArray();
/** @var Person[] $input */
$ipnut = $service->getStuff();
$result = [];
foreach ($input as $item) {
if ($item instanceof Person && $item->getAge() >= 18) {
$result[] = $item
}
}
/** @var Person[] $input */
$input = $service->getStuff();
/** @var Person[] $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->filter(function (Person $p) { return $p->getAge() >= 18; })
->sortBy(function (Person $p) { return $p->getAge(); })
->toArray();
/** @var Person[] $input */
$ipnut = $service->getStuff();
$result = [];
foreach ($input as $item) {
if ($item instanceof Person && $item->getAge() >= 18) {
$result[] = $item
}
}
usort($result, function (Person $p1, Person $p2) {
return $p1->getAge() <=> $p2->getAge();
});
/** @var Person[] $input */
$input = $service->getStuff();
/** @var Person[] $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->filter(function (Person $p) { return $p->getAge() >= 18; })
->sortBy(function (Person $p) { return $p->getName(); })
->reverse()
->toArray();
/** @var Person[] $input */
$input = $service->getStuff();
$result = Psi::it($input)
->map(function (Person $p) { return $p->getAge(); })
->toArray();
/** @var Person[] $input */
$input = $service->getStuff();
$result = Psi::it($input)
->sortBy(function (Person $p) { return $p->getName(); })
->map(function (Person $p) { return [ $p->getName(), $p->getAge() ]; })
->toArray();
$input = $service->getStuff();
/** @var Person[] $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->filterBy(
function (Person $p) { return $p->getAge(); }, // map the input
new Psi\IsGreaterThanOrEqual(18) // apply filter on the mapped value
)
->toArray();
$result = Psi::it([1, 1, 1])
->all(new Psi\IsEqualTo(1)); // true, all elements are equal to 1
$result = Psi::it([1, 1, 1])
->all(function ($it) { return $it === 1 }); // true, all elements are equal to 1
$result = Psi::it([1, 2, 3])
->all(new Psi\IsEqualTo(1)); // false, not all elements are equal to 1
$result = Psi::it([])
->all(new Psi\IsEqualTo(1)); // true, since no element does not match the condition
$result = Psi::it([2, 1, 4])
->any(new Psi\IsEqualTo(1)); // true, there is one element that is equal to 1
$result = Psi::it([2, 1, 4])
->any(function ($it) { return $it === 1 }); // true, there is one element that is equal to 1
$result = Psi::it([2, 3, 4])
->any(new Psi\IsEqualTo(1)); // false, there is no element that is equal to 1
$result = Psi::it([])
->any(new Psi\IsEqualTo(1)); // false, when there is no element in the list, then none can match
$input = $service->getStuff();
/** @var Person[] $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->groupBy(
function (Person $p) { return $p->getAge(); }, // define the group
)
->toKeyValueArray(); // toKeyValueArray() will preserve keys, in our case the age-groups
var_dump($result);
/** @var Person[] $result */
$result = Psi::it(
$service->getStuff(),
$service->getMoreStuff(),
$service->getEvenMoreStuff()
)
->filter(new Psi\IsInstanceOf(Person::class))
->toArray();
$input = $service->getStuff();
/** @var Person[] $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->filter(function (Person $p) { return $p->getAge() >= 18; })
->skip(10)
->limit(5)
->toArray();
$input = $service->getStuff();
/** @var Person[] $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->skip(10)
->filter(function (Person $p) { return $p->getAge() >= 18; })
->limit(5)
->toArray();
$input = $service->getStuff();
/** @var Person[] $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->skip(10)
->limit(5)
->filter(function (Person $p) { return $p->getAge() >= 18; })
->toArray();
$input = $service->getStuff();
/** @var float $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->filter(function (Person $p) { return $p->getAge() >= 18; })
->count();
$input = $service->getStuff();
/** @var float $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->map(function (Person $p) { return $p->getAge(); })
->sum();
$input = $service->getStuff();
/** @var float $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->map(function (Person $p) { return $p->getAge(); })
->min();
$input = $service->getStuff();
/** @var float $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->map(function (Person $p) { return $p->getAge(); })
->max();
$input = $service->getStuff();
/** @var float $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->map(function (Person $p) { return $p->getAge(); })
->avg();
$input = $service->getStuff();
/** @var float $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->map(function (Person $p) { return $p->getAge(); })
->median();
$input = $service->getStuff();
/** @var float $result */
$result = Psi::it($input)
->filter(new Psi\IsInstanceOf(Person::class))
->filter(function (Person $p) { return $p->getAge() >= 18; })
->map(function (Person $p) { return $p->getAge(); })
->median();
/** @var Person|null $result */
$result = Psi::it(input)
->filter(new Psi\IsInstanceOf(Person::class))
->filterBy(
function(function (Person $p) { return $p->getName(); }),
new Psi\Str\IsStartingWith('A')
)
->getFirst()
/** @var Person|null $result */
$result = Psi::it(input)
->filter(new Psi\IsInstanceOf(Person::class))
->filterBy(
function(function (Person $p) { return $p->getName(); }),
new Psi\Str\IsStartingWith('A')
)
->getLast()
/** @var Person|null $result */
$result = Psi::it(input)
->filter(new Psi\IsInstanceOf(Person::class))
->filterBy(
function(function (Person $p) { return $p->getName(); }),
new Psi\Str\IsStartingWith('A')
)
->getRandom()
$result = Psi::it($input)->filter(new Psi\IsArray())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotArray())->toArray();
$result = Psi::it($input)->filter(new Psi\IsBool())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotBool())->toArray();
$result = Psi::it($input)->filter(new Psi\IsCallable())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotCallable())->toArray();
$result = Psi::it($input)->filter(new Psi\IsDateString())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotDateString())->toArray();
$result = Psi::it($input)->filter(new Psi\IsEmpty())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotEmpty())->toArray();
$result = Psi::it($input)->filter(new Psi\IsInstanceOf())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotInstanceOf())->toArray();
$result = Psi::it($input)->filter(new Psi\IsInteger())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotInteger())->toArray();
$result = Psi::it($input)->filter(new Psi\IsIntegerString())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotIntegerString())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNull())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotNull())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNull())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotNull())->toArray();
$result = Psi::it($input)->filter(new Psi\IsObject())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotObject())->toArray();
$result = Psi::it($input)->filter(new Psi\IsResource())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotResource())->toArray();
$result = Psi::it($input)->filter(new Psi\IsResource())->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotResource())->toArray();
$result = Psi::it($input)->filter(new Psi\IsEqualTo("Summer"))->toArray();
$result = Psi::it($input)->filter(new Psi\IsNotEqualTo("Winter"))->toArray();
Psi::it(range(0, 10))
->chunk(3)
->toArray();
Psi::it(range(0, 20))
->filter(new IsMultipleOf(2))
->skip(5)
->toArray();
Psi::it(range(0, 20))
->filter(new IsMultipleOf(2))
->limit(5)
->toArray();
Psi::it(range(0, 20))
->takeWhile(new Psi\IsLessThan(5))
->toArray();
Psi::it(range(0, 20))
->takeUntil(new Psi\IsGreaterThan(5))
->toArray();
Psi::it([1, 2, 3])
->filter(function ($i) { return $i < 3; })
->getLast()
2
Psi::it([1, 2, 3])
->filter(function ($i) { return $i < 3; })
->getRandom()
Psi::it([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
->filter(new Psi\Num\IsMultipleOf(3))
->toArray();
Psi::it(range(0, 30))
->filter(new Psi\Num\IsPrime())
->toArray();
$result = Psi::it($persons)
->filterBy(
function (Person $p) { return $p->getAge(); },
new IsLessThan(18)
)
->toArray()
$result = Psi::it($persons)
->uniqueBy(
function (Person $p) { return $person->getName(); }
)
->toArray();
$result = Psi::it($values)
->map(new Psi\ToFloat())
->toArray();
$result = Psi::it($values)
->map(new Psi\ToInteger())
->toArray();
$result = Psi::it($values)
->map(new Psi\ToString())
->toArray();
$result = Psi::it($values)
->filter(new Psi\Str\IsStartingWith('a'))
->toArray();
$result = Psi::it($values)
->filter(new Psi\Str\IsNotStartingWith('a', false))
->toArray();
$result = Psi::it($values)
->filter(new Psi\Str\IsEndingWith('a'))
->toArray();
$result = Psi::it($values)
->filter(new Psi\Str\IsNotEndingWith('a', false))
->toArray();
$result = Psi::it($values)
->filter(new Psi\Str\IsContaining('a'))
->toArray();
$result = Psi::it($values)
->filter(new Psi\Str\IsNotContaining('a', false))
->toArray();
$result = Psi::it($values)
->filter(new Psi\Str\IsMatchingRegex('/[0-9]{2,}'))
->toArray();
$result = Psi::it($values)
->filter(new Psi\Str\IsNotMatchingRegex('/ABC/i'))
->toArray();
$result = Psi::it($values)
->map(new Psi\Str\WithoutAccents())
->toArray();
Psi::it(
[ ['name' => 'a', 'val' => 1], ['name' => 'a', 'val' => 2], ['name' => 'b', 'val' => 1] ]
)
->groupBy(
function ($o) { return $o['name']; }
)
->toArray()
['a' => [ ['name' => 'a', 'val' => 1], ['name' => 'a', 'val' => 2] ], 'b' => ... ]
array(3) {
[7] =>
array(2) {
[0] =>
object(Person) ...
[1] =>
object(Person) ...
}
[15] =>
array(1) {
[0] =>
object(Person) ...
...
}
[21] =>
array (2) {
...
}
...
}
$result = Psi::it($values)
->filterBy(
function (Person $p) { return $p->getAge(); }
)
->toArray()