/** * Add middleware to use. * * @param array $middleware * * @throws InvalidArgumentException * @return $this */ public function withMiddleware(array $middleware) { Arguments::define(Boa::arrOf(Boa::string()))->check($middleware); $this->middleware += $middleware; return $this; }
/** * 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 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; }
/** * 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 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; }
/** * The inverse operation of calling toResponse. * * This function will attempt to parse a Response object into an * ApiResponse object, which can be useful for introspection and testing. * * @param Response $response * * @throws CoreException * @return static */ public static function fromResponse(Response $response) { $body = Json::decode($response->getContent()); $result = Spec::define(['messages' => Boa::arrOf(Boa::string()), 'status' => Boa::in(static::getValidStatuses()), 'code' => Boa::integer()])->check($body); if ($result->failed()) { throw new CoreException('Unable to parse an ApiResponse out of the content of a' . ' Response object. Make sure that the Response object was' . ' actually generated by an ApiResponse or a compatible' . ' implementation.'); } return new static(Arr::except($body, static::getReservedKeys()), $body['status'], $body['messages']); }
/** * 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 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; }
/** * 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; }