Skip to content

Commit

Permalink
Tweak.
Browse files Browse the repository at this point in the history
  • Loading branch information
jumilla committed Jul 29, 2015
1 parent 71be694 commit b1d4412
Showing 1 changed file with 46 additions and 32 deletions.
78 changes: 46 additions & 32 deletions sources/Specs/InputModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

namespace LaravelPlus\Extension\Specs;

use InvalidArgumentException;

class InputModel
{
/**
* @var \LaravelPlus\Extension\Specs\InputSpec
*/
private $spec;
protected $spec;

/**
* @var array
*/
private $in;
protected $in;

/**
* @var \Illuminate\Validation\Validator
*/
private $validator;
protected $validator;

/**
* @param string|\LaravelPlus\Extension\Specs\InputSpec $spec
Expand All @@ -42,53 +44,34 @@ public function __construct($spec, array $in = null)
}

$this->spec = $spec;
$this->in = $in ?: $this->getInput();
$this->in = $in ?: $this->gatherInput();

$rules = $this->spec->rules();
if (!is_array($rules)) {
throw new \InvalidArgumentException('rule specs for "'.$path.'" must array.');
throw new InvalidArgumentException("rule specs for '$path' must array.");
}

$ruleMessages = $this->spec->ruleMessages();
if (!is_array($ruleMessages)) {
throw new \InvalidArgumentException('rule translation for "'.$path.'" must array.');
throw new InvalidArgumentException("rule translation for '$path' must array.");
}

$labels = $this->spec->labels();
if (!is_array($labels)) {
throw new \InvalidArgumentException('rule labels for "'.$path.'" must array.');
throw new InvalidArgumentException("rule labels for '$path' must array.");
}
$this->validator = \Validator::make($this->in, $rules, $ruleMessages, $labels);

$this->validator = app('validator')->make($this->in, $rules, $ruleMessages, $labels);
}

/**
* @return array
*/
public function getInput()
protected function gatherInput()
{
return app('request')->only($this->spec->attributes());
}

/**
* Dynamically retrieve attributes on the model.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->in[$key];
}

/**
* Dynamically set attributes on the model.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
$this->in[$key] = $value;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -120,4 +103,35 @@ public function validator()
{
return $this->validator;
}

/**
* Dynamically retrieve attributes on the model.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->in[$key];
}

/**
* Dynamically set attributes on the model.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
$this->in[$key] = $value;
}

/**
* @return array
*/
public function input()
{
return $this->in;
}
}

0 comments on commit b1d4412

Please sign in to comment.