Functions

A set of simple functions that extend the standard PHP functions API

equals(mixed $a, mixed $b): bool

It's a very useful one, especially when you have to deal with different types of values.

Returns true when one of the conditions is met:

  • if $a implements Equatable and is equal with $b

  • if $a and $b are equal, and of the same type ===

use function Jungi\Common\equals;

/** @implements Equatable<self> */
class ContactInformation implements Equatable
{
    public function __construct(
        private Phone $phone,
        private ?Phone $mobile = null
    ) {}
    
    public function equals(self $other): bool
    {
        return $this->phone->equals($other->phone)
            && equals($this->mobile, $other->mobile);
    }
}

$a = new ContactInformation(new Phone('(321) 456-1234'), new Phone('(886) 456-6543'));
$b = new ContactInformation(new Phone('(321) 456-1234'), new Phone('(886) 456-6543'));
assert(true === equals($a, $b);

$a = new ContactInformation(new Phone('(321) 456-1234'));
$b = new ContactInformation(new Phone('(321) 456-1234'));
assert(true === equals($a, $b);

$a = new ContactInformation(new Phone('(321) 456-1234'));
$b = new ContactInformation(new Phone('(321) 456-1234'), new Phone('(886) 456-6543'));
assert(false === equals($a, $b);
assert(false === equals($b, $a);

array_equals(array $a, array $b): bool

Returns true if both arrays have the same keys and their values are equal.

in_iterable(mixed $value, iterable $iterable): bool

Returns true if a value is present in an iterable.

iterable_unique(iterable $iterable): iterable

Returns an iterable without duplicates.

iterable_search(mixed $value, iterable $iterable): mixed

Returns the first key where the given value is equal. If the value is not found, false is returned.

Last updated