add global static lang support and sample to README.md

This commit is contained in:
Marcos Neves 2013-08-16 12:20:25 -03:00
parent 2fac74557b
commit 2dd9fa79a3
3 changed files with 81 additions and 11 deletions

View File

@ -67,6 +67,21 @@ if($v->validate()) {
} }
``` ```
Setting language and language dir globally:
```php
// boot or config file
use Valitron\Validator as V;
V::langDir(__DIR__.'/validator_lang'); // always set langDir before lang.
V::lang('ar');
```
## Built-in Validation Rules ## Built-in Validation Rules
* `required` - Required field * `required` - Required field

View File

@ -29,7 +29,7 @@ class Validator
/** /**
* Setup validation * Setup validation
*/ */
public function __construct($data, $fields = array(), $lang = 'en', $langDir = null) public function __construct($data, $fields = array(), $lang = null, $langDir = null)
{ {
// Allows filtering of used input fields against optional second array of field names allowed // Allows filtering of used input fields against optional second array of field names allowed
// This is useful for limiting raw $_POST or $_GET data to only known fields // This is useful for limiting raw $_POST or $_GET data to only known fields
@ -39,18 +39,14 @@ class Validator
} }
} }
// Only load language files if language or directory has changed // set lang in the follow order: constructor param, static::$_lang, defaul to en
if($lang !== static::$_lang || $langDir !== static::$_langDir) { $lang = $lang ?: static::$_lang ?: 'en';
// Set language directory for loading language files // set langDir in the follow order: constructor param, static::$_langDir, default to package lang dir
if($langDir === null) { $langDir = $langDir ?: static::$_langDir ?: dirname(dirname(__DIR__)) . '/lang';
$langDir = dirname(dirname(__DIR__)) . '/lang';
}
static::langDir($langDir);
// Set language for error messages static::langDir($langDir);
static::lang($lang); static::lang($lang);
} }
}
/** /**
* Get/set language to use for validation messages * Get/set language to use for validation messages

View File

@ -0,0 +1,59 @@
<?php
use Valitron\Validator;
class StaticLangTest extends \PHPUnit_Framework_TestCase
{
public function tearDown()
{
$this->resetProperty('_lang');
$this->resetProperty('_langDir');
$this->resetProperty('_ruleMessages', array());
}
protected function resetProperty($name, $value = null)
{
$prop = new ReflectionProperty('Valitron\Validator', $name);
$prop->setAccessible(true);
$prop->setValue($value);
$prop->setAccessible(false);
}
protected function getLangDir()
{
return __DIR__.'/../../lang';
}
/**
* Lang defined statically should not be overrided by constructor default
*/
public function testLangDefinedStatically()
{
$lang = 'ar';
Validator::lang($lang);
$validator = new Validator(array());
$this->assertEquals($lang, Validator::lang());
}
/**
* LangDir defined statically should not be overrided by constructor default
*/
public function testLangDirDefinedStatically()
{
$langDir = $this->getLangDir();
Validator::langDir($langDir);
$validator = new Validator(array());
$this->assertEquals($langDir, Validator::langDir());
}
public function testDefaultLangShouldBeEn()
{
$validator = new Validator(array());
$this->assertEquals('en', Validator::lang());
}
public function testDefaultLangDirShouldBePackageLangDir()
{
$validator = new Validator(array());
$this->assertEquals(realpath($this->getLangDir()), realpath(Validator::langDir()));
}
}