mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Merge pull request #24 from neves/static-vs-instance
Static vs instance
This commit is contained in:
commit
c5b50c0617
@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Valitron;
|
namespace Valitron;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validation Class
|
* Validation Class
|
||||||
*
|
*
|
||||||
@ -44,8 +46,13 @@ class Validator
|
|||||||
// set langDir in the follow order: constructor param, static::$_langDir, default to package lang dir
|
// set langDir in the follow order: constructor param, static::$_langDir, default to package lang dir
|
||||||
$langDir = $langDir ?: static::langDir();
|
$langDir = $langDir ?: static::langDir();
|
||||||
|
|
||||||
static::langDir($langDir);
|
// Load language file in directory
|
||||||
static::lang($lang);
|
$langFile = rtrim($langDir, '/') . '/' . $lang . '.php';
|
||||||
|
if ( stream_resolve_include_path($langFile) ) {
|
||||||
|
static::$_ruleMessages = include $langFile;
|
||||||
|
} else {
|
||||||
|
throw new InvalidArgumentException("fail to load language file '$langFile'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -55,10 +62,6 @@ class Validator
|
|||||||
{
|
{
|
||||||
if($lang !== null) {
|
if($lang !== null) {
|
||||||
static::$_lang = $lang;
|
static::$_lang = $lang;
|
||||||
|
|
||||||
// Load language file in directory
|
|
||||||
$langDir = static::langDir();
|
|
||||||
static::$_ruleMessages = require rtrim($langDir, '/') . '/' . $lang . '.php';
|
|
||||||
}
|
}
|
||||||
return static::$_lang ?: 'en';
|
return static::$_lang ?: 'en';
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
use Valitron\Validator;
|
use Valitron\Validator;
|
||||||
|
|
||||||
class StaticLangTest extends BaseTestCase
|
class LangTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
protected function getLangDir()
|
protected function getLangDir()
|
||||||
{
|
{
|
||||||
@ -41,4 +41,13 @@ class StaticLangTest extends BaseTestCase
|
|||||||
$validator = new Validator(array());
|
$validator = new Validator(array());
|
||||||
$this->assertEquals(realpath($this->getLangDir()), realpath(Validator::langDir()));
|
$this->assertEquals(realpath($this->getLangDir()), realpath(Validator::langDir()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage fail to load language file '/this/dir/does/not/exists/en.php'
|
||||||
|
*/
|
||||||
|
public function testLangException()
|
||||||
|
{
|
||||||
|
new Validator(array(), array(), 'en', '/this/dir/does/not/exists');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
13
tests/Valitron/StaticVsInstanceTest.php
Normal file
13
tests/Valitron/StaticVsInstanceTest.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
use Valitron\Validator;
|
||||||
|
|
||||||
|
class StaticVsInstanceTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
public function testInstanceOverrideStaticLang()
|
||||||
|
{
|
||||||
|
Validator::lang('ar');
|
||||||
|
new Validator(array(), array(), 'en');
|
||||||
|
$this->assertEquals('ar', Validator::lang(),
|
||||||
|
'instance defined lang should not replace static global lang');
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user