public function testCheckWithFailedConstraints() { $spec = TypedSpec::define()->withFieldType('first_name', Boa::string())->withFieldConstraints('first_name', new StringLengthConstraint(4))->withFieldType('age', Boa::integer()); $result = $spec->check(['first_name' => 'Ed', 'age' => 23]); $this->assertTrue($result->failed()); $this->assertEquals(['first_name'], array_keys($result->getFailed())); }
/** * Construct an instance of a Flick. * * @param array|ArrayAccess|Traversable $functions * @param string $default * * @throws InvalidArgumentException * @throws LackOfCoffeeException */ public function __construct($functions, $default = 'default') { parent::__construct(); Arguments::define(Boa::lst(), Boa::either(Boa::string(), Boa::integer()))->check($functions, $default); $this->functions = $functions; $this->default = $default; }
/** * Construct an instance of a ResourceMethod. * * @param string $method * @param string $verb * @param string $path */ public function __construct($method, $verb, $path) { parent::__construct(); Arguments::define(Boa::string(), Boa::string(), Boa::string())->check($method, $verb, $path); $this->method = $method; $this->verb = $verb; $this->path = $path; }
public function testRenderFieldWithPrimitives() { $spec = (new FormSpec())->withFieldType('first_name', Boa::string())->withFieldType('last_name', Boa::string())->withFieldDefault('first_name', 'Bob')->withFieldType('age', Boa::integer())->withFieldType('focus', Boa::boolean())->withFieldType('awesome', Boa::boolean())->withFieldDefault('awesome', true)->withFieldType('price', Boa::float()); $presenter = new FormSpecPresenter($spec); $this->assertEquals((new Input(['id' => 'first_name', 'class' => 'form-control', 'type' => 'text', 'value' => 'Bob']))->render(), $presenter->renderField('first_name')->render()); $this->assertEquals((new Input(['id' => 'last_name', 'class' => 'form-control', 'type' => 'text']))->render(), $presenter->renderField('last_name')->render()); $this->assertEquals((new Input(['id' => 'age', 'class' => 'form-control', 'type' => 'number']))->render(), $presenter->renderField('age')->render()); $this->assertEquals((new Input(['id' => 'focus', 'class' => 'form-control', 'type' => 'checkbox']))->render(), $presenter->renderField('focus')->render()); $this->assertEquals((new Input(['id' => 'awesome', 'class' => 'form-control', 'type' => 'checkbox', 'checked' => null]))->render(), $presenter->renderField('awesome')->render()); $this->assertEquals((new Input(['id' => 'price', 'class' => 'form-control', 'type' => 'number']))->render(), $presenter->renderField('price')->render()); }
/** * Register a module with this dashboard. * * @param string $moduleClassName * * @throws CoreException * @throws InvalidArgumentException */ public function register($moduleClassName) { Arguments::define(Boa::string())->check($moduleClassName); $instance = $this->application->make($moduleClassName); if (!$instance instanceof Module) { throw new InvalidArgumentException('The provided class should extend the Module class.'); } try { $instance->boot(); $this->modules[$instance->getName()] = $instance; } catch (Exception $e) { $this->failedModules[$instance->getName()] = $e; } }
/** * 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'); }
public function testComplexSpec() { $graph = new SpecGraph(); $graph->add('input', [], Spec::define(['sleepy' => Boa::boolean(), 'tennis_balls' => Boa::integer(), 'message' => Boa::either(Boa::string(), Boa::integer())], [], ['message'])); $graph->add('allowedMessage', ['input'], Spec::define(['message' => [Boa::in(['hi', 'how are you?', 'you dumb']), Boa::in(['hi', 'how are you?', 'you are smart'])]], [], ['message'])); $graph->add('validBallCount', ['input'], Spec::define(['tennis_balls' => Boa::between(1, 10)])); $graph->add('additionalBallProps', ['validBallCount'], Spec::define(['ball_color' => [Boa::string(), Boa::in(['blue', 'red', 'yellow'])]], [], ['ball_color'])); $result = $graph->check(['sleepy' => true, 'tennis_balls' => 3, 'message' => 'hi', 'ball_color' => 'blue']); $this->assertTrue($result->passed()); $result2 = $graph->check(['sleepy' => 1, 'tennis_balls' => 3]); $this->assertEqualsMatrix([[true, $result2->failed()], [1, count($result2->getFailed())], [['message'], $result2->getMissing()]]); $result3 = $graph->check(['sleepy' => true, 'tennis_balls' => -30, 'message' => 'hello']); $this->assertEqualsMatrix([[true, $result3->failed()], [2, count($result3->getFailed())], [[], $result3->getMissing()]]); $result4 = $graph->check(['sleepy' => true, 'tennis_balls' => 3, 'message' => 'how are you?']); $this->assertEqualsMatrix([[true, $result4->failed()], [0, count($result4->getFailed())], [['ball_color'], $result4->getMissing()]]); $result5 = $graph->check(['sleepy' => true, 'tennis_balls' => 3, 'message' => 'how are you?', 'ball_color' => 'liquid_gold']); $this->assertEqualsMatrix([[true, $result5->failed()], [1, count($result5->getFailed())], [[], $result5->getMissing()]]); }
/** * 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); }
/** * 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; }
/** * @param string $prefix * * @return $this */ public function withPrefix($prefix) { Arguments::define(Boa::string())->check($prefix); $this->prefix = $prefix; return $this; }
/** * Set a relationship of the model explicitly. * * @param string $relationName * @param Model|Model[] $related * * @throws LackOfCoffeeException * @throws InvalidArgumentException * @return $this */ public function withFixed($relationName, $related) { $inspector = $this->getInspector($this->getModelInstance()); $relation = $inspector->getRelation($relationName); $relationModelClass = get_class($relation->getRelated()); Arguments::define(Boa::string(), Boa::either(Boa::instance($relationModelClass), Boa::arrOf(Boa::instance($relationModelClass))))->check($relationName, $related); $this->relations[$relationName] = $relation; $this->relationsGenerators[$relationName] = $related; return $this; }
/** * Set a header in the request. * * @param $key * @param $value * * @throws InvalidArgumentException * @return $this */ public function withHeader($key, $value) { Arguments::define(Boa::string(), Boa::string())->check($key, $value); $key = strtolower($key); $this->headers[$key] = $value; return $this; }
/** * Set a key in the response. * * @param string $key * @param string|array $value * * @throws InvalidArgumentException */ public function set($key, $value) { Arguments::define(Boa::string(), Boa::either(Boa::string(), Boa::arr()))->check($key, $value); if (in_array($key, static::getReservedKeys())) { throw new InvalidArgumentException('This response key is reserved.'); } $this->content[$key] = $value; }
/** * 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); }
/** * Get a model by its id. * * @param int $id * @param array $columns * @param array $with * * @throws InvalidArgumentException * @throws LackOfCoffeeException * @return Model */ public function getById($id, array $columns = ['*'], array $with = []) { Arguments::define(Boa::integer(), Boa::arrOf(Boa::string()), Boa::arrOf(Boa::string()))->check($id, $columns, $with); $query = $this->makeModelInstance()->query()->where('id', $id); $this->applyWith($query, $with); return $query->firstOrFail($columns); }
/** * 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; }
/** * Get a category by ID * * @param string $categoryId * * @return GetCategoryByIdResponse */ public function getCategoryById($categoryId) { Arguments::contain(Boa::string())->check($categoryId); return (new CategoryResponseFactory())->makeFromResponse($this->client->get(vsprintf('/v1/getCategoryById/%s', [$categoryId]))); }
/** * Escape the provided input for HTML. * * @param string $string * * @return string */ public static function esc($string) { Arguments::define(Boa::string())->check($string); return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); }
/** * 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; }
public function testAccessors() { $strings = Boa::string(); $spec = new Spec(['name' => $strings], ['name' => 'Bobby'], ['name']); $this->assertEqualsMatrix([[['name' => $strings], $spec->getConstraints()], [['name' => 'Bobby'], $spec->getDefaults()], [['name'], $spec->getRequired()]]); }
/** * 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; }
/** * Check if the constraint is met. * * @param mixed $value * @param array $context * * @return mixed */ public function check($value, array $context = []) { return Std::falsy(Boa::string()->check($value, $context), (new RegexConstraint('/^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$/'))->check($value, $context)); }
/** * 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; }