public function testGetByKey() { $this->assertEquals(2, getByKey(array('a' => 1, 'b' => 2, 'c' => 3), 'b')); $this->assertEquals(-1, getByKey(array('a' => 1, 'b' => 2, 'c' => 3), 'd', -1)); $this->assertEquals(2, call_user_func(getByKey, array('a' => 1, 'b' => 2, 'c' => 3), 'b')); $this->assertEquals('\\nspl\\a\\getByKey', getByKey); }
/** * @param string|object|array $type * @param bool $onlyOr * @return string */ public static function getFor($type, $onlyOr = false) { if (is_array($type)) { $messagesFor = f\partial(f\map, ['\\nspl\\args\\_p\\ErrorMessage', 'getFor']); if ($onlyOr) { return implode(' or ', $messagesFor($type)); } else { $isOr = function ($t) { return isset(Checker::$isOr[$t]) || class_exists($t); }; list($orTypes, $andTypes) = f\partition($isOr, $type); return implode(' and ', array_filter([implode(' or ', $messagesFor($orTypes)), implode(' and ', $messagesFor($andTypes))])); } } if ($type instanceof Constraint) { return $type->__toString(); } return a\getByKey(self::$messages, $type, 'be ' . (class_exists($type) ? $type : end(explode('\\', $type)))); }