Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 708922e

Browse files
committed
initial commit
1 parent e981c53 commit 708922e

File tree

8 files changed

+280
-2
lines changed

8 files changed

+280
-2
lines changed

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
ardent
2-
======
1+
#Ardent
32

43
Self-validating smart models for Laravel 4's Eloquent O/RM

composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "laravelbook/ardent",
3+
"description": "Self-validating smart models for Laravel 4's Eloquent O/RM",
4+
"keywords": ["validation", "laravel", "framework", "eloquent", "database", "sql", "orm"],
5+
"type": "library",
6+
"homepage": "http://laravelbook.com/",
7+
"license": "BSD-3-Clause",
8+
"authors": [
9+
{
10+
"name": "Max Ehsan",
11+
"homepage": "http://laravelbook.com/",
12+
"email": "[email protected]",
13+
"role": "Developer"
14+
}
15+
],
16+
"support": {
17+
"email": "[email protected]"
18+
},
19+
"require": {
20+
"php": ">=5.3.0",
21+
"illuminate/support": "4.0.x",
22+
"illuminate/database": "4.0.x"
23+
},
24+
"autoload": {
25+
"files": [
26+
"src/LaravelBook/Ardent/Ardent.php"
27+
],
28+
"psr-0": {
29+
"LaravelBook\\Ardent": "src/"
30+
}
31+
},
32+
"target-dir": "LaravelBook/Ardent",
33+
"minimum-stability": "dev"
34+
}

phpunit.xml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Package Test Suite">
15+
<directory>./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

src/LaravelBook/Ardent/Ardent.php

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php namespace LaravelBook\Ardent;
2+
3+
use Closure;
4+
use Illuminate\Support\MessageBag;
5+
use Illuminate\Support\Facades\Input;
6+
use Illuminate\Support\Facades\Validator;
7+
8+
/**
9+
* Ardent - Self-validating Eloquent model base class
10+
*
11+
*/
12+
abstract class Ardent extends \Illuminate\Database\Eloquent\Model
13+
{
14+
15+
/**
16+
* The rules to be applied to the data.
17+
*
18+
* @var array
19+
*/
20+
public static $rules = array();
21+
22+
/**
23+
* The array of custom error messages.
24+
*
25+
* @var array
26+
*/
27+
public static $customMessages = array();
28+
29+
/**
30+
* The message bag instance containing validation error messages
31+
*
32+
* @var Illuminate\Support\MessageBag
33+
*/
34+
public $validationErrors;
35+
36+
/**
37+
* Create a new Ardent model instance.
38+
*
39+
* @param array $attributes
40+
* @return void
41+
*/
42+
public function __construct(array $attributes = array())
43+
{
44+
parent::__construct($attributes);
45+
$this->validationErrors = new MessageBag;
46+
}
47+
48+
/**
49+
* Validate the model instance
50+
*
51+
* @param array $rules Validation rules
52+
* @param array $customMessages Custom error messages
53+
* @return bool
54+
*/
55+
public function validate($rules = array(), $customMessages = array())
56+
{
57+
58+
$success = true;
59+
60+
$data = $this->attributes; // the data under validation
61+
if (empty($data)) {
62+
// just extract the fields that are defined in the validation rule-set
63+
$data = array_intersect_key(Input::all(), $rules);
64+
}
65+
66+
if (!empty($data) && (!empty($rules) || !empty(static::$rules))) {
67+
68+
// check for overrides
69+
$rules = (empty($rules)) ? static::$rules : $rules;
70+
$customMessages = (empty($customMessages)) ? static::$customMessages : $customMessages;
71+
72+
// construct the validator
73+
$validator = Validator::make($data, $rules, $customMessages);
74+
$success = $validator->passes();
75+
76+
if ($success) {
77+
// if the model is valid, unset old errors
78+
if ($this->validationErrors->count() > 0) {
79+
$this->validationErrors = new MessageBag;
80+
}
81+
} else {
82+
// otherwise set the new ones
83+
$this->validationErrors = $validator->messages();
84+
85+
// stash the input to the current session
86+
Input::flash();
87+
}
88+
89+
}
90+
91+
return $success;
92+
}
93+
94+
/**
95+
* onSave - Invoked before a model is saved. Return false to abort the operation.
96+
*
97+
* @return bool
98+
*/
99+
protected function onSave()
100+
{
101+
return true;
102+
}
103+
104+
/**
105+
* onForceSave - Invoked before a model is saved forcefully. Return false to abort the operation.
106+
*
107+
* @return bool
108+
*/
109+
protected function onForceSave()
110+
{
111+
return true;
112+
}
113+
114+
/**
115+
* Save the model to the database.
116+
*
117+
* @param array $rules:array
118+
* @param array $customMessages
119+
* @param closure $onSave
120+
* @return bool
121+
*/
122+
public function save($rules = array(), $customMessages = array(), Closure $onSave = null)
123+
{
124+
125+
// validate
126+
$validated = $this->validate($rules, $customMessages);
127+
128+
// execute onSave callback
129+
$proceed = is_null($onSave) ? $this->onSave() : $onSave($this);
130+
131+
// save if all conditions are satisfied
132+
return ($proceed && $validated) ? parent::save() : false;
133+
134+
}
135+
136+
/**
137+
* Force save the model even if validation fails.
138+
*
139+
* @param array $rules:array
140+
* @param array $customMessages:array
141+
* @return bool
142+
*/
143+
public function force_save($rules = array(), $customMessages = array(), Closure $onForceSave = null)
144+
{
145+
146+
// validate the model
147+
$this->validate($rules, $customMessages);
148+
149+
// execute onForceSave callback
150+
$proceed = is_null($onForceSave) ? $this->onForceSave() : $onForceSave($this);
151+
152+
// save regardless of the outcome of validation
153+
return $proceed ? parent::save() : false;
154+
155+
}
156+
157+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php namespace LaravelBook\Ardent\Facades;
2+
3+
use Illuminate\Support\Facades\Facade;
4+
5+
class Ardent extends Facade {
6+
7+
/**
8+
* Get the registered name of the component.
9+
*
10+
* @return string
11+
*/
12+
protected static function getFacadeAccessor() { return 'ardent'; }
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php namespace LaravelBook\Ardent\Providers;
2+
3+
use Illuminate\Support\ServiceProvider;
4+
use LaravelBook\Ardent\Ardent;
5+
6+
class ArdentServiceProvider extends ServiceProvider {
7+
8+
/**
9+
* Indicates if loading of the provider is deferred.
10+
*
11+
* @var bool
12+
*/
13+
protected $defer = false;
14+
15+
/**
16+
* Bootstrap the application events.
17+
*
18+
* @return void
19+
*/
20+
public function boot()
21+
{
22+
$this->package('laravelbook/ardent');
23+
}
24+
25+
/**
26+
* Register the service provider.
27+
*
28+
* @return void
29+
*/
30+
public function register()
31+
{
32+
//
33+
}
34+
35+
/**
36+
* Get the services provided by the provider.
37+
*
38+
* @return array
39+
*/
40+
public function provides()
41+
{
42+
return array('ardent');
43+
}
44+
45+
}

tests/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)