Skip to content

Commit

Permalink
Merge branch '2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
crynobone committed Dec 20, 2013
2 parents 19e03fc + 14732f5 commit aace370
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- 5.3
- 5.4
- 5.5
- hhvm

before_script:
- composer self-update
Expand All @@ -13,3 +14,7 @@ script: phpunit -c phpunit.xml --coverage-text

after_script:
- php vendor/bin/coveralls -v

matrix:
allow_failures:
- php: hhvm
10 changes: 8 additions & 2 deletions src/Orchestra/Support/Relic.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ class Relic
*/
public function get($key, $default = null)
{
return array_get($this->items, $key, $default);
$value = array_get($this->items, $key, $default);

if (is_null($value)) {
return value($default);
}

return $value;
}

/**
Expand All @@ -30,7 +36,7 @@ public function get($key, $default = null)
*/
public function set($key, $value = null)
{
return array_set($this->items, $key, $value);
return array_set($this->items, $key, value($value));
}

/**
Expand Down
111 changes: 63 additions & 48 deletions src/Orchestra/Support/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

abstract class Validator
{
/**
* Laravel validator instance.
*
* @var \Illuminate\Validation\Validator
*/
protected $resolver;

/**
* List of rules.
*
Expand All @@ -28,19 +35,15 @@ abstract class Validator
protected $bindings = array();

/**
* Current scope.
* Scenario queues.
*
* @var string
*/
protected $scope = null;

/**
* Create a new instance.
* @var array
*/
public function __construct()
{
$this->setRules($this->rules);
}
protected $queued = array(
'on' => null,
'extend' => null,
'parameters' => array(),
);

/**
* Create a scope scenario.
Expand All @@ -49,15 +52,16 @@ public function __construct()
* @param array $parameters
* @return Validator
*/
public function on($scenario, $parameters = array())
public function on($scenario, array $parameters = array())
{
$this->scope = $scenario;

$method = 'on'.ucfirst($scenario);
$on = 'on'.ucfirst($scenario);
$extend = 'extend'.ucfirst($scenario);

if (method_exists($this, $method)) {
call_user_func_array(array($this, $method), $parameters);
}
$this->queued = array(
'on' => method_exists($this, $on) ? $on : null,
'extend' => method_exists($this, $extend) ? $extend : null,
'parameters' => $parameters,
);

return $this;
}
Expand All @@ -68,7 +72,7 @@ public function on($scenario, $parameters = array())
* @param array $bindings
* @return Validator
*/
public function bind($bindings)
public function bind(array $bindings)
{
$this->bindings = array_merge($this->bindings, $bindings);

Expand All @@ -78,26 +82,21 @@ public function bind($bindings)
/**
* Execute validation service.
*
* @param array $input
* @param string $event
* @return \Illuminate\Validation\Factory
* @param array $input
* @param string|array $event
* @return \Illuminate\Validation\Validator
*/
public function with($input, $events = array())
public function with(array $input, $events = array())
{
$rules = $this->runValidationEvents($events);
$validation = V::make($input, $rules);
$this->runQueuedOn();

if (is_null($this->scope)) {
return $validation;
}
$rules = $this->runValidationEvents($events);

$method = 'extend'.ucfirst($this->scope);
$this->resolver = V::make($input, $rules);

if (method_exists($this, $method)) {
call_user_func(array($this, $method), $validation);
}
$this->runQueuedExtend($this->resolver);

return $validation;
return $this->resolver;
}

/**
Expand All @@ -107,7 +106,7 @@ public function with($input, $events = array())
*/
protected function getBindedRules()
{
$rules = $this->rules;
$rules = $this->rules;

if (! empty($this->bindings)) {
foreach ($rules as $key => $value) {
Expand All @@ -118,36 +117,52 @@ protected function getBindedRules()
return $rules;
}

/**
* Run queued on scenario.
*
* @return void
*/
protected function runQueuedOn()
{
if (! is_null($method = $this->queued['on'])) {
call_user_func_array(array($this, $method), $this->queued['parameters']);
}
}

/**
* Run queued extend scenario.
*
* @param \Illuminate\Validation\Validator $resolver
* @return void
*/
protected function runQueuedExtend($resolver)
{
if (! is_null($method = $this->queued['extend'])) {
call_user_func(array($this, $method), $resolver);
}
}

/**
* Run validation events and return the finalize rules.
*
* @param array $events
* @param array|string $events
* @return array
*/
protected function runValidationEvents($events)
{
is_array($events) or $events = (array) $events;

// Merge all the events.
$events = array_merge($this->events, (array) $events);
$events = array_merge($this->events, $events);

// Convert rules array to Fluent, in order to pass it by references
// in all event listening to this validation.
$rules = new Fluent($this->getBindedRules());
$rules = new Fluent($this->getBindedRules());

foreach ((array) $events as $event) {
Event::fire($event, array(& $rules));
}

return $rules->getAttributes();
}

/**
* Set validation rules, this would override all previously defined
* rules.
*
* @return array
*/
public function setRules($rules = array())
{
return $this->rules = $rules;
}
}
2 changes: 1 addition & 1 deletion tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testValidation()
$validation = $stub->with(array(), 'foo.event');

$this->assertEquals('orchestra', $_SERVER['validator.onFoo']);
$this->assertInstanceOf('Validator', $_SERVER['validator.extendFoo']);
$this->assertEquals($validation, $_SERVER['validator.extendFoo']);
$this->assertInstanceOf('Validator', $validation);
}

Expand Down

0 comments on commit aace370

Please sign in to comment.