[limb-svn] r6755 - in 3.x/trunk/limb/i18n: src src/macro tests/cases tests/cases/macro

svn at limb-project.com svn at limb-project.com
Mon Jan 28 18:50:59 MSK 2008


Author: vasiatka
Date: 2008-01-28 18:50:58 +0300 (Mon, 28 Jan 2008)
New Revision: 6755
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6755

Added:
   3.x/trunk/limb/i18n/src/macro/
   3.x/trunk/limb/i18n/src/macro/i18n_date.filter.php
   3.x/trunk/limb/i18n/tests/cases/macro/
   3.x/trunk/limb/i18n/tests/cases/macro/.setup.php
   3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NDateMacroFilterTest.class.php
Log:
--Added i18n_date filter for macro templates
--Added test for i18n_date filter for macro

Added: 3.x/trunk/limb/i18n/src/macro/i18n_date.filter.php
===================================================================
--- 3.x/trunk/limb/i18n/src/macro/i18n_date.filter.php	                        (rev 0)
+++ 3.x/trunk/limb/i18n/src/macro/i18n_date.filter.php	2008-01-28 15:50:58 UTC (rev 6755)
@@ -0,0 +1,77 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/i18n/src/datetime/lmbLocaleDateTime.class.php');
+
+/**
+ * Filter i18n_date for macro templates
+ * @filter i18n_date
+ * @package i18n
+ * @version $Id$
+ */
+class lmbI18NMacroDateFilter extends lmbMacroFilter
+{
+  var $date;
+
+  function preGenerate($code)
+  {
+    $code->registerInclude('limb/i18n/src/datetime/lmbLocaleDateTime.class.php');
+    $code->registerInclude('limb/datetime/src/lmbDateTime.class.php');
+    parent :: preGenerate($code);
+  }
+  function getValue()
+  {
+    $params="array(";
+    foreach ($this->params as $key=>$value)
+    {
+      $params.=$value.",";
+    }
+    $params.=")";
+    return 'lmbI18NMacroDateFilter::_i18nDateFilter(' . $params.', ' . $this->base->getValue() . ')';
+  }
+
+  static function _i18nDateFilter($params,$value)
+  {
+    $toolkit = lmbToolkit :: instance();
+    if(isset($params[0]) && $params[0])
+    {
+      $locale=$toolkit->getLocaleObject($params[0]);
+    }
+    else
+      $locale=$toolkit->getLocaleObject();
+
+    if(isset($params[3]) && $params[3])
+      $format=$params[3];
+    else
+    {
+      if(isset($params[2]) && $params[2])
+        $format_type = $params[2];
+      else
+        $format_type = 'short_date';
+
+      $property = $format_type . '_format';
+      $format=$locale->$property;
+    }
+
+    if(isset($params[1]) && $params[1])
+      $date_type = $params[1];
+    else
+      $date_type = 'stamp';
+
+    switch($date_type)
+    {
+      case 'string': $date = new lmbLocaleDateTime($value);      break;
+      case 'stamp':  $date = new lmbLocaleDateTime((int)$value); break;
+      default:       $date = new lmbLocaleDateTime($value);      break;
+    }
+
+    return $date->localeStrftime($format, $locale);
+  }
+}
+

Added: 3.x/trunk/limb/i18n/tests/cases/macro/.setup.php
===================================================================
--- 3.x/trunk/limb/i18n/tests/cases/macro/.setup.php	                        (rev 0)
+++ 3.x/trunk/limb/i18n/tests/cases/macro/.setup.php	2008-01-28 15:50:58 UTC (rev 6755)
@@ -0,0 +1,10 @@
+<?php
+lmb_require('limb/macro/common.inc.php');
+lmb_require('limb/fs/src/lmbFs.class.php');
+lmb_require('limb/macro/src/lmbMacroTemplate.class.php');
+lmb_require('limb/macro/src/*.class.php');
+lmb_require('limb/macro/src/compiler/*.interface.php');
+lmb_require('limb/macro/src/compiler/*.class.php');
+lmb_require('limb/macro/tests/cases/lmbBaseMacroTest.class.php');
+lmb_require('limb/config/common.inc.php');
+

Added: 3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NDateMacroFilterTest.class.php
===================================================================
--- 3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NDateMacroFilterTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NDateMacroFilterTest.class.php	2008-01-28 15:50:58 UTC (rev 6755)
@@ -0,0 +1,151 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+lmb_require('limb/i18n/src/datetime/lmbLocaleDate.class.php');
+
+class lmbI18NDateMacroFilterTest extends lmbBaseMacroTest
+{
+  protected function _createMacro($file)
+  {
+    $base_dir = LIMB_VAR_DIR . '/tpl';
+    $cache_dir = LIMB_VAR_DIR . '/tpl/compiled';
+    $macro = new lmbMacroTemplate($file,
+                                  new lmbMacroConfig(
+                                                     $cache_dir,
+                                                     true,
+                                                     true,
+                                                     array($base_dir),
+                                                     null,
+                                                     array('limb/macro/src/filters','limb/i18n/src/macro')
+                                                     )
+                                 );
+    return $macro;
+  }
+
+  function tearDown()
+  {
+    lmbFs :: rm(LIMB_VAR_DIR . '/tpl');
+  }
+
+  function testSetDateByString()
+  {
+    $code = '{$#var|i18n_date:"en_US", "string"}';
+    $tpl = $this->_createMacroTemplate($code, 'tpl.html');
+    $time = mktime(0, 0, 0, 2, 20, 2002);
+    $tpl->set('var', $time);
+    $out = $tpl->render();
+    $this->assertEqual($out, '02/20/2002');
+  }
+
+  function testSetDateByStampValue()
+  {
+    $date = new lmbDateTime('2004-12-20 10:15:30');
+    $time=$date->getStamp();
+
+    $code = '{$#var|i18n_date:"en_US", "stamp"}';
+    $tpl = $this->_createMacroTemplate($code, 'tpl.html');
+
+    $tpl->set('var', $time);
+    $out = $tpl->render();
+    $this->assertEqual($out, '12/20/2004');
+  }
+
+  function testFormatType()
+  {
+    $date = new lmbDateTime('2005-01-20 10:15:30');
+    $time=$date->getStamp();
+
+    $code = '{$#var|i18n_date:"en_US", "stamp", "date"}';
+    $tpl = $this->_createMacroTemplate($code, 'tpl.html');
+
+    $tpl->set('var', $time);
+    $out = $tpl->render();
+    $this->assertEqual($out, 'Thursday 20 January 2005');
+  }
+
+  function testSetDateTimeByString()
+  {
+    $time='2002-02-20 10:23:24';
+
+    $code = '{$#var|i18n_date:"en_US", "string", "short_date_time"}';
+    $tpl = $this->_createMacroTemplate($code, 'tpl.html');
+
+    $tpl->set('var', $time);
+    $out = $tpl->render();
+    $this->assertEqual($out, '02/20/2002 10:23:24');
+  }
+
+  function testDefinedFormat()
+  {
+    $date = new lmbDateTime('2004-12-20 10:15:30');
+    $time=$date->getStamp();
+
+    $code = '{$#var|i18n_date:"en_US", "stamp", "", "%Y %m %d"}';
+    $tpl = $this->_createMacroTemplate($code, 'tpl.html');
+
+    $tpl->set('var', $time);
+    $out = $tpl->render();
+    $this->assertEqual($out, '2004 12 20');
+  }
+
+  function testUseRussianAsCurrentLocale()
+  {
+    $toolkit = lmbToolkit :: save();
+    $toolkit->addLocaleObject(new lmbLocale('ru_RU', new lmbIni(dirname(__FILE__).'/../../../i18n/locale/ru_RU.ini')));
+
+    $date = new lmbDateTime('2004-12-20 10:15:30');
+    $time=$date->getStamp();
+
+    $code = '{$#var|i18n_date:"ru_RU", "stamp"}';
+    $tpl = $this->_createMacroTemplate($code, 'tpl.html');
+
+    $tpl->set('var', $time);
+    $out = $tpl->render();
+    $this->assertEqual($out, '20.12.2004');
+
+    lmbToolkit :: restore();
+  }
+
+  function testComplexPathBasedDBEWithDefinedFormat()
+  {
+    $date = new lmbDateTime('2005-01-20 10:15:30');
+    $my_dataspace = new lmbSet(array('var' => $date->getStamp()));
+
+    $code = '{$#my.var|i18n_date:"en_US", "stamp", "", "%Y %m %d"}';
+    $tpl = $this->_createMacroTemplate($code, 'tpl.html');
+
+    $tpl->set('my', $my_dataspace);
+    $out = $tpl->render();
+    $this->assertEqual($out, '2005 01 20');
+  }
+
+  function testDateByCurrentLocale()
+  {
+    $date = new lmbDateTime('2004-12-20 10:15:30');
+    $time=$date->getStamp();
+
+  	$code = '{$#var|i18n_date:"","stamp"}';
+    $tpl = $this->_createMacroTemplate($code, 'tpl.html');
+    $tpl->set('var', $time);
+    $out = $tpl->render();
+    $this->assertEqual($out, '12/20/2004');
+  }
+
+  function testWithOutParams()
+  {
+    $date = new lmbDateTime('2004-12-20 10:15:30');
+    $time=$date->getStamp();
+
+  	$code = '{$#var|i18n_date}';
+    $tpl = $this->_createMacroTemplate($code, 'tpl.html');
+    $tpl->set('var', $time);
+    $out = $tpl->render();
+    $this->assertEqual($out, '12/20/2004');
+  }
+}
+



More information about the limb-svn mailing list