├── tests ├── .gitkeep └── Database │ └── ModelTest.php ├── .gitignore ├── .travis.yml ├── composer.json ├── phpunit.xml ├── src └── Way │ └── Database │ ├── DatabaseServiceProvider.php │ └── Model.php └── readme.md /tests/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | 7 | before_script: 8 | - curl -s http://getcomposer.org/installer | php 9 | - php composer.phar install --dev 10 | 11 | script: phpunit -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "way/database", 3 | "description": "", 4 | "authors": [ 5 | { 6 | "name": "Jeffrey Way", 7 | "email": "jeffrey@jeffrey-way.com" 8 | } 9 | ], 10 | "require": { 11 | "php": ">=5.4.0", 12 | "illuminate/support": "~4.1", 13 | "illuminate/database": "~4.1", 14 | "illuminate/validation": "~4.1" 15 | }, 16 | "require-dev": { 17 | "mockery/mockery": "dev-master" 18 | }, 19 | "autoload": { 20 | "psr-0": { 21 | "Way\\Database": "src/" 22 | } 23 | }, 24 | "minimum-stability": "stable" 25 | } 26 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/Way/Database/DatabaseServiceProvider.php: -------------------------------------------------------------------------------- 1 | 'Way\Database\Model' 30 | ``` 31 | Now, your models can simply extend `Model`. 32 | 33 | ```php 34 | 'required' 53 | ]; 54 | 55 | //Use this for custom messages 56 | protected static $messages = [ 57 | 'name.required' => 'My custom message for :attribute required' 58 | ]; 59 | } 60 | ``` 61 | 62 | Now, simply save the model as you normally would, and let the package worry about the validation. If it fails, then the model's `save` method will return false. 63 | 64 | Here's an example of storing a new dog. 65 | 66 | ```php 67 | public function store() 68 | { 69 | $dog = new Dog(Input::all()); 70 | 71 | if ($dog->save()) 72 | { 73 | return Redirect::route('dogs.index'); 74 | } 75 | 76 | return Redirect::back()->withInput()->withErrors($dog->getErrors()); 77 | } 78 | ``` 79 | 80 | If using Eloquent's static `create` method, you can use the `hasErrors()` methods to determine if validation errors exist. 81 | 82 | ```php 83 | $dog = Dog::create(Input::all()); 84 | 85 | if ($dog->hasErrors()) ... 86 | ``` 87 | That's it! Have fun. 88 | -------------------------------------------------------------------------------- /src/Way/Database/Model.php: -------------------------------------------------------------------------------- 1 | validator = $validator ?: \App::make('validator'); 41 | } 42 | 43 | /** 44 | * Listen for save event 45 | */ 46 | protected static function boot() 47 | { 48 | parent::boot(); 49 | 50 | static::saving(function($model) 51 | { 52 | return $model->validate(); 53 | }); 54 | } 55 | 56 | /** 57 | * Validates current attributes against rules 58 | */ 59 | public function validate() 60 | { 61 | $v = $this->validator->make($this->attributes, static::$rules, static::$messages); 62 | 63 | if ($v->passes()) 64 | { 65 | return true; 66 | } 67 | 68 | $this->setErrors($v->messages()); 69 | 70 | return false; 71 | } 72 | 73 | /** 74 | * Set error message bag 75 | * 76 | * @var Illuminate\Support\MessageBag 77 | */ 78 | protected function setErrors($errors) 79 | { 80 | $this->errors = $errors; 81 | } 82 | 83 | /** 84 | * Retrieve error message bag 85 | */ 86 | public function getErrors() 87 | { 88 | return $this->errors; 89 | } 90 | 91 | /** 92 | * Inverse of wasSaved 93 | */ 94 | public function hasErrors() 95 | { 96 | return ! empty($this->errors); 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /tests/Database/ModelTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('passes')->once()->andReturn(true); 18 | 19 | $validation = m::mock('Illuminate\Validation\Validator'); 20 | $validation->shouldReceive('make') 21 | ->once() 22 | ->andReturn($response); 23 | 24 | $model = new Model([], $validation); 25 | $result = $model->validate(); 26 | 27 | // If validation passes, we should return true 28 | // and not set any errors. 29 | $this->assertTrue($result); 30 | $this->assertNull($model->getErrors()); 31 | } 32 | 33 | public function testValidateFail() 34 | { 35 | // Mock Validator response and have it fail 36 | $response = m::mock('StdClass'); 37 | $response->shouldReceive('passes')->once()->andReturn(false); 38 | $response->shouldReceive('messages')->once()->andReturn('foo'); 39 | 40 | $validation = m::mock('Illuminate\Validation\Validator'); 41 | $validation->shouldReceive('make') 42 | ->once() 43 | ->andReturn($response); 44 | 45 | $model = new Model([], $validation); 46 | $result = $model->validate(); 47 | 48 | // This time we should return false 49 | // and store the validation messages 50 | // on the errors property 51 | $this->assertFalse($result); 52 | $this->assertEquals('foo', $model->getErrors()); 53 | } 54 | 55 | public function testGetErrors() 56 | { 57 | $model = m::mock('Way\Database\Model')->makePartial(); 58 | $model->setErrors('foo'); 59 | 60 | $this->assertEquals('foo', $model->getErrors()); 61 | } 62 | 63 | public function testHasErrors() 64 | { 65 | $model = m::mock('Way\Database\Model')->makePartial(); 66 | 67 | $this->assertFalse($model->hasErrors()); 68 | 69 | $model->setErrors('foo'); 70 | 71 | $this->assertTrue($model->hasErrors()); 72 | } 73 | 74 | } --------------------------------------------------------------------------------