Beispiel #1
0
 /**
  * Construct an instance of a Flick.
  *
  * @param array|ArrayAccess|Traversable $functions
  * @param string $default
  *
  * @throws InvalidArgumentException
  * @throws \Chromabits\Nucleus\Exceptions\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;
 }
Beispiel #2
0
 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 ResourceMethod.
  *
  * @param string $method
  * @param string $verb
  * @param string $path
  */
 public function __construct($method, $verb, $path)
 {
     parent::__construct();
     Arguments::contain(Boa::string(), Boa::string(), Boa::string())->check($method, $verb, $path);
     $this->method = $method;
     $this->verb = $verb;
     $this->path = $path;
 }
Beispiel #4
0
 /**
  * Register a module with this dashboard.
  *
  * @param string $moduleClassName
  *
  * @throws CoreException
  * @throws InvalidArgumentException
  */
 public function register($moduleClassName)
 {
     Arguments::contain(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;
     }
 }
Beispiel #5
0
 /**
  * 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');
 }
Beispiel #6
0
 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()]]);
 }
Beispiel #7
0
 /**
  * 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;
 }
 /**
  * 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])));
 }
 /**
  * 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::contain(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;
 }
Beispiel #10
0
 /**
  * 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;
 }
Beispiel #11
0
 /**
  * Set a key in the response.
  *
  * @param string $key
  * @param string|array $value
  *
  * @throws InvalidArgumentException
  */
 public function set($key, $value)
 {
     Arguments::contain(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;
 }
Beispiel #12
0
 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()]]);
 }
 /**
  * @param string $prefix
  *
  * @return $this
  */
 public function withPrefix($prefix)
 {
     Arguments::define(Boa::string())->check($prefix);
     $this->prefix = $prefix;
     return $this;
 }
Beispiel #14
0
 /**
  * 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);
 }
Beispiel #15
0
 /**
  * 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);
 }
 /**
  * Set a header in the request.
  *
  * @param $key
  * @param $value
  *
  * @throws InvalidArgumentException
  * @return $this
  */
 public function withHeader($key, $value)
 {
     Arguments::contain(Boa::string(), Boa::string())->check($key, $value);
     $key = strtolower($key);
     $this->headers[$key] = $value;
     return $this;
 }
 /**
  * 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;
 }
Beispiel #18
0
 /**
  * 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));
 }
Beispiel #19
0
 /**
  * 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;
 }
Beispiel #20
0
 /**
  * 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;
 }
Beispiel #21
0
 /**
  * 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');
 }
 /**
  * Get an array with only the specified keys of the provided array.
  *
  * @param array|null $included
  *
  * @return static
  */
 public function only($included = [])
 {
     Arguments::define(Boa::either(Boa::arrOf(Boa::either(Boa::string(), Boa::integer())), Boa::null()))->check($included);
     if (is_null($included)) {
         return $this;
     }
     if (count($included) == 0) {
         return static::zero();
     }
     return static::of(array_intersect_key($this->value, array_flip($included)));
 }
 /**
  * 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::contain(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);
 }