/** * This method creates associative array using key and value functions on array elements. * * Example: * <code> * $array = range(1, 2); * $map = Arrays::toMap($array, function ($elem) { * return $elem * 10; * }, function ($elem) { * return $elem + 1; * }); * </code> * Result: * <code> * Array * ( * [10] => 2 * [20] => 3 * ) * </code> * * @param array $elements * @param callable $keyFunction * @param callable|null $valueFunction * @return array */ public static function toMap(array $elements, $keyFunction, $valueFunction = null) { if ($valueFunction == null) { $valueFunction = Functions::identity(); } $keys = array_map($keyFunction, $elements); $values = array_map($valueFunction, $elements); return empty($keys) ? array() : array_combine($keys, $values); }
/** * @test */ public function shouldNegatePredicate() { $this->assertFalse(Functions::call(Functions::not(Functions::identity()), true)); $this->assertTrue(Functions::call(Functions::not(Functions::identity()), false)); }