diff --git a/docs/changes.md b/docs/changes.md index 1b86cff..94e6027 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -11,11 +11,17 @@ title: Support Change Log * Allow `Orchestra\Support\Nesty` to prepend an item without knowing the current first item. * Add `Orchestra\Support\Messages::extend()` and tweak how Messages notification can be manipulated on current request. * Add `Orchestra\Support\Nesty::is()` to return instance of `Illuminate\Support\Fluent` to allow further chaining of the instance. +* Add `Orchestra\Support\Str::searchable()` for better pattern matching. * Add `Orchestra\Support\Relic`. * Add `Orchestra\Support\Str::replace()`. +* `Illuminate\Support\Str::title()` is implemented, remove duplicate method. +* Add support to use `Orchestra\Support\Validator::extendScope()`, useful to have when need to deal with conditional rules . +* Refactor `Orchestra\Support\Str::streamGetContents()`. +* Implement [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) coding standard. * Refactor `Orchestra\Support\Validator::getBindedRules()` to use `Orchestra\Support\Str::replace()`. * Refactor `Orchestra\Support\Nesty::decendants()`. * Return `Orchestra\Support\Nesty::getItems()` as instance of `Illuminate\Support\Collection`. +* Add support for custom messages on `Orchestra\Support\Validator` using `$phrases` protected property. * Add following facades: - `Orchestra\Support\Facades\Debug` - `Orchestra\Support\Facades\Notifier` diff --git a/src/Orchestra/Support/Validator.php b/src/Orchestra/Support/Validator.php index ddab88b..4c7b6d4 100644 --- a/src/Orchestra/Support/Validator.php +++ b/src/Orchestra/Support/Validator.php @@ -13,6 +13,13 @@ abstract class Validator */ protected $resolver; + /** + * List of phrases. + * + * @var array + */ + protected $phrases = array(); + /** * List of rules. * @@ -84,16 +91,16 @@ public function bind(array $bindings) * * @param array $input * @param string|array $event - * @param array $messages + * @param array $phrases * @return \Illuminate\Validation\Validator */ - public function with(array $input, $events = array(), $messages = array()) + public function with(array $input, $events = array(), array $phrases = array()) { $this->runQueuedOn(); - $rules = $this->runValidationEvents($events); + list($rules, $phrases) = $this->runValidationEvents($events, $phrases); - $this->resolver = V::make($input, $rules, $messages); + $this->resolver = V::make($input, $rules, $phrases); $this->runQueuedExtend($this->resolver); @@ -144,12 +151,13 @@ protected function runQueuedExtend($resolver) } /** - * Run validation events and return the finalize rules. + * Run validation events and return the finalize rules and phrases. * * @param array|string $events + * @param array $phrases * @return array */ - protected function runValidationEvents($events) + protected function runValidationEvents($events, array $phrases) { is_array($events) or $events = (array) $events; @@ -158,12 +166,16 @@ protected function runValidationEvents($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()); + $phrases = new Fluent(array_merge($this->phrases, $phrases)); foreach ((array) $events as $event) { - Event::fire($event, array(& $rules)); + Event::fire($event, array(& $rules, & $phrases)); } - return $rules->getAttributes(); + return array( + $rules->getAttributes(), + $phrases->getAttributes(), + ); } } diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 2bf5b39..b475890 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -38,9 +38,10 @@ public function testValidation() $event = m::mock('\Illuminate\Events\Dispatcher[fire]'); $validator = m::mock('\Illuminate\Validation\Factory'); $rules = array('email' => array('email', 'foo:2'), 'name' => 'any'); + $phrases = array('email.required' => 'Email required'); $event->shouldReceive('fire')->once()->with('foo.event', m::any())->andReturn(null); - $validator->shouldReceive('make')->once()->with(array(), $rules, array()) + $validator->shouldReceive('make')->once()->with(array(), $rules, $phrases) ->andReturn(m::mock('\Illuminate\Validation\Validator')); \Illuminate\Support\Facades\Event::swap($event); @@ -66,8 +67,9 @@ public function testValidationWithoutAScope() $event = m::mock('\Illuminate\Events\Dispatcher[fire]'); $validator = m::mock('\Illuminate\Validation\Factory'); $rules = array('email' => array('email', 'foo:2'), 'name' => 'any'); + $phrases = array('email.required' => 'Email required', 'name' => 'Any name'); - $validator->shouldReceive('make')->once()->with(array(), $rules, array()) + $validator->shouldReceive('make')->once()->with(array(), $rules, $phrases) ->andReturn(m::mock('\Illuminate\Validation\Validator')); \Illuminate\Support\Facades\Event::swap($event); @@ -76,7 +78,7 @@ public function testValidationWithoutAScope() $stub = new FooValidator; $stub->bind(array('id' => '2')); - $validation = $stub->with(array()); + $validation = $stub->with(array(), null, array('name' => 'Any name')); $this->assertInstanceOf('\Illuminate\Validation\Validator', $validation); } @@ -89,6 +91,10 @@ class FooValidator extends \Orchestra\Support\Validator 'name' => 'any', ); + protected $phrases = array( + 'email.required' => 'Email required', + ); + protected function onFoo($value) { $_SERVER['validator.onFoo'] = $value;