public function testDefineWithInvalid() { $definition = Arguments::define(PrimitiveTypeConstraint::forType(ScalarTypes::SCALAR_STRING), EitherConstraint::create(MaybeConstraint::forType(PrimitiveTypeConstraint::forType(CompoundTypes::COMPOUND_ARRAY)), PrimitiveTypeConstraint::forType(ScalarTypes::SCALAR_BOOLEAN))); $definition->check('wow', true); $definition->check('wow', []); $definition->check('wow', null); $this->setExpectedException(InvalidArgumentException::class); $definition->check('wow', 25); }
/** * Construct an instance of a BetweenConstraint. * * @param int|float $min * @param int|float $max * * @throws LackOfCoffeeException * @throws InvalidArgumentException */ public function __construct($min, $max) { parent::__construct(); Arguments::define(new NumericConstraint(), new NumericConstraint())->check($min, $max); if ($min > $max) { throw new LackOfCoffeeException('Min should be smaller than or equal to max.'); } $this->min = $min; $this->max = $max; }
/** * Construct an instance of a StringLengthConstraint. * * @param int $min * @param int $max * * @throws InvalidArgumentException */ public function __construct($min = 0, $max = -1) { parent::__construct(); Arguments::define(Boa::integer(), Boa::integer())->check($min, $max); if ($min < 0) { throw new InvalidArgumentException('The minimum length is expected to be >= 0.'); } if ($max != -1 && $max < $min || $max < -1) { throw new InvalidArgumentException(' The maximum length is expected to be: (== -1 || >= minimum) &&' . ' >= -1.'); } $this->min = $min; $this->max = $max; }
/** * Run the flick on input. * * @param string|int $input * * @throws UnknownKeyException * @return mixed */ public function go($input) { Arguments::define(Boa::readMap())->define($input); $map = ComplexFactory::toReadMap($this->functions); if ($map->member($input)) { /** @var callable $function */ $function = Maybe::fromJust($map->lookup($input)); return $function(); } elseif ($map->member($this->default)) { /** @var callable $function */ $function = Maybe::fromJust($map->lookup($this->default)); return $function(); } throw new UnknownKeyException(); }
/** * Escape the provided string. * * @param SafeHtmlWrapper|SafeHtmlProducerInterface|string $string * * @throws CoreException * @throws InvalidArgumentException * @return SafeHtmlWrapper|string */ public static function escape($string) { Arguments::define(Boa::either(Boa::either(Boa::instance(SafeHtmlWrapper::class), Boa::instance(SafeHtmlProducerInterface::class)), Boa::string()))->check($string); if ($string instanceof SafeHtmlWrapper) { return $string; } elseif ($string instanceof SafeHtmlProducerInterface) { $result = $string->getSafeHtml(); if ($result instanceof SafeHtmlWrapper) { return $result; } elseif ($result instanceof SafeHtmlProducerInterface) { return static::escape($result); } throw new CoreException(vsprintf('Object of class %s implements SafeHtmlProducerInterface' . ' but it returned an unsafe type: %s', [get_class($string), TypeHound::fetch($result)])); } return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); }
/** * Add a custom message for a field. * * @param $field * @param $message * * @throws InvalidArgumentException * @return $this */ public function message($field, $message) { Arguments::define(Boa::string(), Boa::string())->check($field, $message); $this->messages[$field] = $message; return $this; }
/** * @param RamlParameter[] $formParameters * * @return RamlBody */ public function setFormParameters($formParameters) { Arguments::define(Boa::arrOf(Boa::instance(RamlParameter::class)))->check($formParameters); $new = clone $this; $new->formParameters = $formParameters; return $new; }
/** * Add one or more fields to the list of fields that are required. * * @param string|string[] $fields * * @throws InvalidArgumentException * @return $this */ public function required($fields) { Arguments::define(Boa::either(Boa::string(), Boa::arrOf(Boa::string())))->check($fields); if (!is_array($fields)) { $fields = [$fields]; } $this->required = $this->required->append(ArrayList::of($fields)); return $this; }
/** * Construct an instance of a OnlyTransform. * * @param array $allowed */ public function __construct(array $allowed) { parent::__construct(); Arguments::define(Boa::arrOf(Boa::string()))->check($allowed); $this->allowed = $allowed; }
/** * Get a copy of the provided array excluding the specified values. * * @param array $input * @param array $excluded * * @return array * @throws InvalidArgumentException */ public static function exceptValues(array $input, $excluded = []) { Arguments::define(Boa::arrOf(Boa::either(Boa::string(), Boa::integer())))->check($excluded); return Std::filter(function ($value, $_) use($excluded) { return !in_array($value, $excluded); }, $input); }
/** * Generate a hash of the content using the provided private key. * * @param string $content * @param string $privateKey * * @return string */ public function hash($content, $privateKey) { Arguments::define(Boa::string(), Boa::string())->check($content, $privateKey); return hash_hmac($this->algorithm, $content, $privateKey); }
/** * Construct an instance of a AtLeastOneOfConstraint. * * @param array $fields */ public function __construct(array $fields) { parent::__construct(); Arguments::define(Boa::arrOf(Boa::string()))->check($fields); $this->fields = $fields; }
/** * Wrap the provided value inside a Map. * * @param array|ArrayObject|ArrayAccess|MapInterface $input * * @return ArrayAccessMap|ArrayList * @throws CoreException * @throws InvalidArgumentException */ public static function toMap($input) { Arguments::define(Boa::map())->check($input); if ($input instanceof MapInterface) { return $input; } if (is_array($input) || $input instanceof ArrayObject) { return new ArrayMap($input); } if ($input instanceof ArrayAccess) { return new ArrayAccessMap($input); } throw new CoreException('Unable to build Map'); }
/** * Attempt call the provided function a number of times until it no longer * throws an exception. * * @param callable $function * @param int $attempts * * @return mixed|null * @throws InvalidArgumentException */ public static function retry(callable $function, $attempts) { Arguments::define(Boa::func(), Boa::integer())->check($function, $attempts); for ($ii = 0; $ii < $attempts; $ii++) { try { $result = static::call($function, $ii); return $result; } catch (Exception $e) { continue; } } return null; }
/** * @param string $type * * @return RamlParameter */ public function setType($type) { Arguments::define(Boa::in(RamlTypes::getValues()))->check($type); $new = clone $this; $new->type = $type; return $new; }
/** * A shortcut for building mocks. * * @param string $type * @param Closure|CallExpectation[] $definition * * @return $this */ public function mock($type, $definition) { Arguments::define(Boa::string(), Boa::either(Boa::func(), Boa::arrOf(Boa::instance(CallExpectation::class))))->check($type, $definition); if (is_array($definition)) { $this->provide(static::expectationsToMock($type, $definition)); return $this; } $this->provide(Mockery::mock($type, $definition)); return $this; }
/** * Return a new Map of the same type containing the added key. * * @param string $key * @param mixed $value * * @return static */ public function insert($key, $value) { Arguments::define($this->getKeyType(), $this->getValueType())->check($key, $value); $cloned = array_merge($this->value); $cloned[$key] = $value; return static::of($cloned); }
/** * @param string $prefix * * @return $this */ public function withPrefix($prefix) { Arguments::define(Boa::string())->check($prefix); $this->prefix = $prefix; return $this; }