/** * 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; }
public function testCheckNested() { $instance = Spec::define(['name' => Boa::string(), 'count' => Boa::integer(), 'address' => Spec::define(['street' => Spec::define(['first_line' => Boa::string(), 'second_line' => Boa::either(Boa::string(), Boa::integer())], [], ['first_line']), 'state' => [Boa::string(), new StringLengthConstraint(2, 2)], 'zip' => Boa::integer()], [], ['street', 'zip'])]); $resultOne = $instance->check(['name' => 'Doge', 'count' => 7, 'address' => []]); $this->assertTrue($resultOne->failed()); $resultTwo = $instance->check(['name' => 'Doge', 'count' => 7, 'address' => ['street' => [], 'state' => 90]]); $failed = $resultTwo->getFailed(); $missing = $resultTwo->getMissing(); $this->assertTrue($resultTwo->failed()); $this->assertArrayHasKey('address.state', $failed); $this->assertTrue(in_array('address.street.first_line', $missing)); $resultThree = $instance->check(['name' => 'Doge', 'count' => 7, 'address' => ['street' => ['first_line' => '1337 Hacker Way'], 'state' => 'GA', 'zip' => 13370]]); $this->assertTrue($resultThree->passed()); }
/** * 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()]]); }
/** * 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; }
/** * 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; }
/** * 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); }
/** * 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; }
/** * Generate an authorization code for the Research API server. * * @param null|integer $timestamp * * @return string */ public function generateCode($timestamp = null) { Arguments::contain(Boa::either(Boa::null(), Boa::integer()))->check($timestamp); $timestamp = Std::coalesce($timestamp, time() + 3600 * 3); $signature = md5(implode('', [$timestamp, $this->clientId, $this->secret])); return vsprintf('%s|%s|%s', [$timestamp, $this->clientId, $signature]); }
/** * 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 a value is between two other values. * * @param int|float $min * @param int|float $max * @param int|float $value * * @throws LackOfCoffeeException * @return bool */ public static function within($min, $max, $value) { Arguments::define(Boa::either(Boa::integer(), Boa::float()), Boa::either(Boa::integer(), Boa::float()), Boa::either(Boa::integer(), Boa::float()))->check($min, $max, $value); if ($min > $max) { throw new LackOfCoffeeException('Max value is less than the min value.'); } return $min <= $value && $max >= $value; }
/** * Get all models paginated. * * @param array $columns * @param array $with * @param int $take * @param string $pageName * @param null|int $page * * @throws LackOfCoffeeException * @return LengthAwarePaginator */ public function getAllPaginated($columns = ['*'], $with = [], $take = 25, $pageName = 'page', $page = null) { Arguments::define(Boa::arrOf(Boa::string()), Boa::arrOf(Boa::string()), Boa::integer(), Boa::string(), Boa::either(Boa::null(), Boa::integer()))->check($columns, $with, $take, $pageName, $page); $query = $this->makeModelInstance()->query(); $this->applyWith($query, $with); return $query->paginate($take, $columns, $pageName, $page); }