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<ContactInformation> */
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.

use function Jungi\Common\array_equals;

$a = [new Phone('(321) 456-1234'), new Phone('(465) 799-4566')];
$b = [new Phone('(321) 456-1234'), new Phone('(465) 799-4566')];
assert(true === array_equals($a, $b));

$a = [new Phone('(321) 456-1234'), new Phone('(465) 799-4566')];
$b = [new Phone('(321) 456-1234')];
assert(false === array_equals($a, $b));

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

Returns true if a value is present in an iterable.

use function Jungi\Common\in_iterable;

$iterable = [new Phone('(656) 456-7765'), new Phone('(321) 456-1234')];

assert(true === in_iterable(new Phone('(321) 456-1234'), $iterable));
assert(false === in_iterable(new Phone('(232) 456-1234'), $iterable));

iterable_unique(iterable $iterable): iterable

Returns an iterable without duplicates.

use function Jungi\Common\iterable_unique;
use function Jungi\Common\array_equals;

$unique = iterable_unique([
    new Phone('(321) 456-1234'),
    new Phone('(465) 799-4566'),
    new Phone('(321) 456-1234'),
]);
$expected = [
    new Phone('(321) 456-1234'), 
    new Phone('(465) 799-4566'),
];
assert(true === array_equals($expected, $unique));

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.

use function Jungi\Common\iterable_search;

assert(2 === iterable_search(new Phone('(321) 456-1234'), [
    new Phone('(321) 456-1234'),
    new Phone('(465) 799-4566'),
    new Phone('(321) 456-1234'),
]);
assert(false === iterable_search(new Phone('(444) 555-1234'), [
    new Phone('(321) 456-1234'),
    new Phone('(465) 799-4566'),
    new Phone('(321) 456-1234'),
]);

Last updated