Skip to content

Commit

Permalink
Refactor Orchestra\Support\Validator to minimize usage of Illuminate\…
Browse files Browse the repository at this point in the history
…Support\Fluent, this allow rules to be assigned as array and only pass as instance of Fluent during event (to allow pass by references). Fixed orchestral/foundation#52.

Signed-off-by: crynobone <[email protected]>
  • Loading branch information
crynobone committed Jun 24, 2013
1 parent 59f6562 commit 23a0ea9
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/Orchestra/Support/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ public function bind($bindings)
*/
public function with($input, $events = array())
{
$rules = $this->getBindedRules();
$this->runValidationEvents($events, $rules);
$rules = $this->runValidationEvents($events);

return V::make($input, $rules->getAttributes());
return V::make($input, $rules);
}

/**
Expand All @@ -96,9 +95,6 @@ public function with($input, $events = array())
*/
protected function getBindedRules()
{
// If rules is not instance of \Illuminate\Support\Fluent, set it.
if ( ! $this->rules instanceof Fluent) $this->setRules($this->rules);

$rules = $this->rules;
$bindings = $this->prepareBindings('{', '}');

Expand All @@ -107,7 +103,7 @@ protected function getBindedRules()
$value = strtr($value, $bindings);
};

foreach ($rules->getAttributes() as $key => $value)
foreach ($rules as $key => $value)
{
if (is_array($value)) array_walk($value, $filter, $bindings);
else $value = strtr($value, $bindings);
Expand Down Expand Up @@ -139,31 +135,38 @@ protected function prepareBindings($prefix = '{', $suffix = '}')
}

/**
* Run validation events.
* Run validation events and return the finalize rules.
*
* @access protected
* @param array $events
* @return void
* @return array
*/
protected function runValidationEvents($events, $rules)
protected function runValidationEvents($events)
{
// Merge all the events.
$events = array_merge($this->events, (array) $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());

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

return $rules->getAttributes();
}

/**
* Set validation rules, this would override all previously defined
* rules.
*
* @access public
* @return void
* @return array
*/
public function setRules($rules = array())
{
$this->rules = new Fluent($rules);
return $this->rules = $rules;
}
}

0 comments on commit 23a0ea9

Please sign in to comment.