Last active
February 11, 2021 15:09
-
-
Save BradEstey/58a08d58600b48725e12 to your computer and use it in GitHub Desktop.
Use the Illuminate Validation Class outside of Laravel (With optional Translation and Database connection instances)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"illuminate/validation": "4.1.*", | |
"illuminate/translation": "4.1.*", | |
"illuminate/database": "4.1.*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include "vendor/autoload.php"; | |
use Acme\Validation\Capsule as Validation; | |
// Define the locale and the path to the language directory. | |
$validation = new Validation('en', __DIR__.'/lang'); | |
// Adding a database connection is optional. Only used for | |
// the Exists and Unique rules. | |
$validation->setConnection([ | |
'driver' => 'mysql', | |
'host' => 'localhost', | |
'database' => 'app', | |
'username' => 'user', | |
'password' => 'secret', | |
'charset' => 'utf8', | |
'collation' => 'utf8_unicode_ci', | |
'prefix' => '' | |
]); | |
dd($validation->getValidator()); | |
// Returns an instance of Illuminate\Validation\Factory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Validation\Capsule; | |
use Illuminate\Container\Container; | |
use Illuminate\Database\ConnectionResolver; | |
use Illuminate\Database\ConnectionResolverInterface; | |
use Illuminate\Database\Connectors\ConnectionFactory; | |
use Illuminate\Filesystem\Filesystem; | |
use Illuminate\Translation\FileLoader; | |
use Illuminate\Translation\Translator; | |
use Illuminate\Support\Fluent; | |
use Illuminate\Validation\Factory; | |
use Illuminate\Validation\DatabasePresenceVerifier; | |
use Symfony\Component\Translation\TranslatorInterface; | |
class Manager | |
{ | |
/** | |
* The current globally used instance. | |
* | |
* @var $this | |
*/ | |
protected static $instance; | |
/** | |
* The validation factory instance. | |
* | |
* @var \Illuminate\Validation\Factory | |
*/ | |
protected $validator; | |
/** | |
* The Translator implementation. | |
* | |
* @var \Symfony\Component\Translation\TranslatorInterface | |
*/ | |
protected $translator; | |
/** | |
* The IoC container instance. | |
* | |
* @var \Illuminate\Container\Container | |
*/ | |
protected $container; | |
/** | |
* Create a new validation capsule manager. | |
* | |
* @param string $fallbackLocale | |
* @param string $path | |
* @param \Illuminate\Container\Container $container | |
*/ | |
public function __construct($fallbackLocale = null, $path = null, Container $container = null) | |
{ | |
$this->setupContainer($container); | |
$this->setupTranslator($fallbackLocale, $path); | |
$this->setupValidator(); | |
} | |
/** | |
* Setup the IoC container instance. | |
* | |
* @param \Illuminate\Container\Container $container | |
* @return void | |
*/ | |
protected function setupContainer($container) | |
{ | |
$this->container = $container ?: new Container; | |
$this->container->instance('config', new Fluent); | |
} | |
/** | |
* Setup the translator instance. | |
* | |
* @param string $fallbackLocale | |
* @param string $path | |
* @return void | |
*/ | |
protected function setupTranslator($fallbackLocale, $path) | |
{ | |
$file = new Filesystem; | |
$loader = new FileLoader($file, $path); | |
$trans = new Translator($loader, $this->container['config']['app.locale']); | |
$trans->setFallback($fallbackLocale); | |
$this->translator = $trans; | |
} | |
/** | |
* Set the Translator implementation. | |
* | |
* @param \Symfony\Component\Translation\TranslatorInterface $translator | |
* @return void | |
*/ | |
public function setTranslator(TranslatorInterface $translator) | |
{ | |
$this->translator = $translator; | |
$this->validator->setTranslator($this->translator); | |
} | |
/** | |
* Build the validation factory instance. | |
* | |
* @return void | |
*/ | |
protected function setupValidator() | |
{ | |
$this->validator = new Factory($this->translator, $this->container); | |
} | |
/** | |
* Set the database connection. | |
* | |
* @param array $config | |
* @return void | |
*/ | |
public function setConnection(array $config) | |
{ | |
$connection = new ConnectionFactory($this->container); | |
$db = new ConnectionResolver(array( | |
null => $connection->make($config) | |
)); | |
$this->setPresenceVerifier($db); | |
} | |
/** | |
* Register the database presence verifier. | |
* | |
* @param \Illuminate\Database\ConnectionResolverInterface $db | |
* @return void | |
*/ | |
public function setPresenceVerifier(ConnectionResolverInterface $db) | |
{ | |
$presence = new DatabasePresenceVerifier($db); | |
$this->validator->setPresenceVerifier($presence); | |
} | |
/** | |
* Make this capsule instance available globally. | |
* | |
* @return void | |
*/ | |
public function setAsGlobal() | |
{ | |
static::$instance = $this; | |
} | |
/** | |
* Get the validation factory instance. | |
* | |
* @return \Illuminate\Validation\Factory | |
*/ | |
public function getValidator() | |
{ | |
return $this->validator; | |
} | |
/** | |
* Get the IoC container instance. | |
* | |
* @return \Illuminate\Container\Container | |
*/ | |
public function getContainer() | |
{ | |
return $this->container; | |
} | |
/** | |
* Set the IoC container instance. | |
* | |
* @param \Illuminate\Container\Container $container | |
* @return void | |
*/ | |
public function setContainer(Container $container) | |
{ | |
$this->container = $container; | |
} | |
} |
This no longer works as at v5. $this->validator->setTranslator($this->translator);
is not a method
/**
* Set the Translator implementation.
*
* @param \Symfony\Component\Translation\TranslatorInterface $translator
* @return void
*/
public function setTranslator()
{
$this->translator = $translator;
$this->validator->setTranslator($this->translator);
}
Also, the setAsGlobal seems to do nothing later on
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@alfiebesin that's his manager.php file, not sure why he didn't just called it:
then in the main file: