/**
  * 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;
 }
 /**
  * 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)));
 }
Beispiel #3
0
 /**
  * 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 = Std::jsonDecode($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']);
 }
Beispiel #4
0
 /**
  * @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;
 }
Beispiel #5
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 #6
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 #7
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);
 }
 /**
  * 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;
 }
 /**
  * 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 #10
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 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);
 }