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

svn at limb-project.com svn at limb-project.com
Thu May 22 13:52:06 MSD 2008


Author: step
Date: 2008-05-22 13:52:05 +0400 (Thu, 22 May 2008)
New Revision: 7037
URL: http://fisheye.limb-project.com/changelog/limb/?cs=7037

Added:
   3.x/trunk/limb/i18n/src/macro/clip.filter.php
   3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NClipMacroFilterTest.class.php
Log:
 -- macro i18n_clip filter added

Added: 3.x/trunk/limb/i18n/src/macro/clip.filter.php
===================================================================
--- 3.x/trunk/limb/i18n/src/macro/clip.filter.php	                        (rev 0)
+++ 3.x/trunk/limb/i18n/src/macro/clip.filter.php	2008-05-22 09:52:05 UTC (rev 7037)
@@ -0,0 +1,71 @@
+<?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
+ */
+
+/**
+ * Filter i18n_clip for macro templates
+ * @filter i18n_clip
+ * @package i18n
+ * @version $Id$
+ */
+class lmbI18NMacroClipFilter extends lmbMacroFilter
+{
+  function preGenerate($code)
+  {
+    $code->registerInclude('limb/i18n/src/charset/driver.inc.php');
+    parent :: preGenerate($code);
+  }
+
+  function getValue()
+  {
+    $suffix = '';
+
+    $value = $this->base->getValue();
+    switch (count($this->params)) {
+    case 1:
+      return 'lmb_substr('. $value .','. 0 .','. $this->params[0] .')';
+      break;
+    case 2:
+      return 'lmb_substr('. $value .','. $this->params[1] .','. $this->params[0]. ')';
+      break;
+    case 3:
+      $suffix = $this->_getSuffix($value,
+                                 $this->params[0],
+                                 $this->params[1],
+                                 $this->params[2]);
+        return 'lmb_substr(' . $value .','. $this->params[1] .','. $this->params[0] .')' . $suffix;
+      break;
+    case 4:
+      $limit = $this->params[0];
+      $offset = $this->params[1];
+      $word_wrap = $this->params[3];
+      $suffix = $this->_getSuffix($value, $limit, $offset, $this->params[2]);
+
+      if (strtoupper(substr($word_wrap,0,1)) != 'N')
+      {
+          if(lmb_preg_match('~^(.{0,'.$limit.'}(?U)\w*)\b~ism', lmb_substr($value, $offset), $match))
+            return $match[1].$suffix;
+          else
+            return '';
+      }
+      else
+        return 'lmb_substr('. $value .','. $offset. ','. $limit .')' . $suffix;
+      break;
+    default:
+        throw new lmbMacroException('Wrong number of filter params(1..4)');
+    }
+  }
+
+  function _getSuffix($value, $limit, $offset, $suffix)
+  {
+    $result = '';
+    if(lmb_strlen($value) > ($limit + $offset))
+      $result = '.' . $suffix;
+    return $result;
+  }
+}
\ No newline at end of file

Added: 3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NClipMacroFilterTest.class.php
===================================================================
--- 3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NClipMacroFilterTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NClipMacroFilterTest.class.php	2008-05-22 09:52:05 UTC (rev 7037)
@@ -0,0 +1,89 @@
+<?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
+ */
+require_once('limb/i18n/utf8.inc.php');
+lmb_require('limb/core/src/lmbSet.class.php');
+
+class lmbI18NClipMacroFilterTest extends lmbBaseMacroTest
+{
+  var $prev_driver;
+
+  function setUp()
+  {
+    parent :: setUp();
+    $this->prev_driver = lmb_use_charset_driver(new lmbUTF8BaseDriver());
+  }
+
+  function tearDown()
+  {
+    lmb_use_charset_driver($this->prev_driver);
+    parent :: tearDown();
+  }
+
+  function testLengthLimit()
+  {
+    $code = '{$#var|i18n_clip:3}';
+    $tpl = $this->_createMacroTemplate($code, 'length_limit.html');
+    $var = "что-то";
+    $tpl->set('var', $var);
+    $out = $tpl->render();
+    $this->assertEqual($out, 'что');
+  }
+
+  function testLengthLimitAndOffset()
+  {
+    $code = '{$#var|i18n_clip:3,5}';
+    $tpl = $this->_createMacroTemplate($code, 'length_limit_and_offset.html');
+    $var = "фреймворк для веб-приложений";
+    $tpl->set('var', $var);
+    $out = $tpl->render();
+    $this->assertEqual($out, 'вор');
+  }
+
+  function testWithSuffix()
+  {
+    $code = '{$#var|i18n_clip:3,5,"..."}';
+    $tpl = $this->_createMacroTemplate($code, 'clip_with_suffix.html');
+    $var = "фреймворк для веб-приложений";
+    $tpl->set('var', $var);
+    $out = $tpl->render();
+    $this->assertEqual($out, 'вор...');
+  }
+
+  function testSuffixNotUsedTooShortString()
+  {
+    $code = '{$#var|i18n_clip:10,"0","..."}';
+    $tpl = $this->_createMacroTemplate($code, 'clip_suffix_not_used.html');
+    $var = "фреймворк";
+    $tpl->set('var', $var);
+    $out = $tpl->render();
+    $this->assertEqual($out, 'фреймворк');
+  }
+
+  // can't implement this since PHP has some bugs with /b modifier in multibyte mode
+  function _testLongStringWordBoundary()
+  {
+    $code = '{$#var|i18n_clip:11,1,"...", "y"}';
+    $tpl = $this->_createMacroTemplate($code, 'clip_with_word_bound.html');
+    $var = "фреймворк для веб-приложений";
+    $tpl->set('var', $var);
+    $out = $tpl->render();
+    $this->assertEqual($out, 'реймворк для...');
+  }
+
+  function testPathBasedDBELengthLimit()
+  {
+    $code = '{$#my.var|i18n_clip:3}';
+    $tpl = $this->_createMacroTemplate($code, 'clip_path_based_dbe_with_limit.html');
+    $data = new lmbSet(array('var' => 'что-то'));
+    $tpl->set('my', $data);
+    $out = $tpl->render();
+    $this->assertEqual($out, 'что');
+  }
+}
+



More information about the limb-svn mailing list