1111
1212
1313use Closure ;
14+ use Illuminate \Container \Container ;
15+ use Illuminate \Database \Capsule \Manager as DatabaseCapsule ;
16+ use Illuminate \Events \Dispatcher ;
1417use Illuminate \Support \MessageBag ;
1518use Illuminate \Support \Facades \Input ;
1619use Illuminate \Support \Facades \Hash ;
1720use Illuminate \Support \Facades \Validator ;
1821use Illuminate \Database \Eloquent \Model ;
1922use Illuminate \Support \Str ;
23+ use Illuminate \Validation \Factory as ValidationFactory ;
24+ use Symfony \Component \Translation \Translator ;
2025
2126/**
2227 * Ardent - Self-validating Eloquent model base class
@@ -96,6 +101,20 @@ abstract class Ardent extends Model
96101 */
97102 public $ autoHashPasswordAttributes = false ;
98103
104+ /**
105+ * If set to true will try to instantiate the validator as if it was outside Laravel.
106+ *
107+ * @var bool
108+ */
109+ protected static $ externalValidator = false ;
110+
111+ /**
112+ * A Translator instance, to be used by standalone Ardent instances.
113+ *
114+ * @var \Illuminate\Validation\Factory
115+ */
116+ protected static $ validationFactory ;
117+
99118 /**
100119 * Create a new Ardent model instance.
101120 *
@@ -108,6 +127,31 @@ public function __construct( array $attributes = array() ) {
108127 $ this ->validationErrors = new MessageBag ;
109128 }
110129
130+ /**
131+ * Configures Ardent to be used outside of Laravel - correctly setting Eloquent and Validation modules.
132+ *
133+ * @param array $connection Connection info used by {@link \Illuminate\Database\Capsule\Manager::addConnection}.
134+ * Should contain driver, host, port, database, username, password, charset and collation.
135+ * @param string $language A language string as used by {@link \Symfony\Component\Translation\Translator}
136+ */
137+ public static function configureAsExternal ( array $ connection , $ language ) {
138+ $ db = new DatabaseCapsule ;
139+ $ db ->addConnection ( $ connection );
140+ $ db ->setEventDispatcher ( new Dispatcher ( new Container ) );
141+ //TODO: configure a cache manager (as an option)
142+ $ db ->bootEloquent ();
143+
144+ self ::$ externalValidator = true ;
145+ self ::$ validationFactory = new ValidationFactory ( new Translator ( $ language ) );
146+ }
147+
148+ protected static function makeValidator ( $ data , $ rules , $ customMessages ) {
149+ if (self ::$ externalValidator )
150+ return self ::$ validationFactory ->make ( $ data , $ rules , $ customMessages );
151+ else
152+ return Validator::make ( $ data , $ rules , $ customMessages );
153+ }
154+
111155 /**
112156 * Validate the model instance
113157 *
@@ -142,7 +186,7 @@ public function validate( array $rules = array(), array $customMessages = array(
142186 $ data = $ this ->getAttributes (); // the data under validation
143187
144188 // perform validation
145- $ validator = Validator:: make ( $ data , $ rules , $ customMessages );
189+ $ validator = self :: makeValidator ( $ data , $ rules , $ customMessages );
146190 $ success = $ validator ->passes ();
147191
148192 if ( $ success ) {
0 commit comments