Merge pull request #24 from neves/static-vs-instance

Static vs instance
This commit is contained in:
Vance Lucas 2013-08-19 08:41:31 -07:00
commit c5b50c0617
3 changed files with 32 additions and 7 deletions

View File

@ -1,6 +1,8 @@
<?php
namespace Valitron;
use InvalidArgumentException;
/**
* Validation Class
*
@ -44,8 +46,13 @@ class Validator
// set langDir in the follow order: constructor param, static::$_langDir, default to package lang dir
$langDir = $langDir ?: static::langDir();
static::langDir($langDir);
static::lang($lang);
// Load language file in directory
$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) {
static::$_lang = $lang;
// Load language file in directory
$langDir = static::langDir();
static::$_ruleMessages = require rtrim($langDir, '/') . '/' . $lang . '.php';
}
return static::$_lang ?: 'en';
}

View File

@ -1,7 +1,7 @@
<?php
use Valitron\Validator;
class StaticLangTest extends BaseTestCase
class LangTest extends BaseTestCase
{
protected function getLangDir()
{
@ -41,4 +41,13 @@ class StaticLangTest extends BaseTestCase
$validator = new Validator(array());
$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');
}
}

View 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');
}
}