ZF-6053: Add new string pad filter
Description
Class could be something like:
<?php require_once 'Zend/Filter/Interface.php'; class Zend_Filter_StringPad implements Zend_Filter_Interface { const TYPE_BOTH = 'both'; const TYPE_LEFT = 'left'; const TYPE_RIGHT = 'right'; /** * Padding length * @var string */ protected $_length; /** * Padding characters * @var string */ protected $_string; /** * Padding type * @var string */ protected $_type; public function __construct($length, $string = ' ', $type = self::PADDING_RIGHT) { $this->setLength($length); $this->setString($string); $this->setType($type); } /** * Retrieve padding length * * @return int */ public function getLength() { return $this->_length; } /** * Retrieve padding characters * * @return string */ public function getString() { return $this->_string; } /** * Retrieve padding type * * @return string */ public function getType() { return $this->_type; } /** * Set padding length * * @param int $length * @return Zend_Filter_StringPad */ public function setLength($length) { $this->_length = (int) $length; return $this; } /** * Set padding characters * * @param string $string * @return Zend_Filter_StringPad */ public function setString($string) { $this->_string = (string) $string; return $this; } /** * Set padding type * * @param string $type * @return Zend_Filter_StringPad */ public function setType($type) { switch ($type) { case self::TYPE_BOTH: case self::TYPE_LEFT: case self::TYPE_RIGHT: $this->_type = $type; break; default: throw new Zend_Filter_Exception('Padding type is invalid'); } return $this; } /** * Converts our pad type consts to STR_PAD_* consts * * @return string */ protected function _getType() { switch ($this->_type) { case self::TYPE_LEFT: return STR_PAD_LEFT; case self::TYPE_RIGHT: return STR_PAD_RIGHT; default: return STR_PAD_BOTH; } } /** * Pad a string to a certain length with another string * * @param string $value * @return string */ public function filter($value) { $pad = ''; if (strlen($value) > 0) { $pad = str_pad($value, $this->_length, $this->_string, $this->_getType()); } return $pad; } } ?>
Comments
Posted by Ben Scholzen (dasprid) on 2009-03-18T02:21:35.000+0000
If so, it should use Zend_Text_MultiByte::strPad() to allow correct padding of non-ascii strings.
Posted by Václav Vaník (dimmu) on 2009-03-18T02:55:52.000+0000
Multibyte-safe version of the Zend_Filter_StringPad
Posted by Václav Vaník (dimmu) on 2009-03-18T02:57:47.000+0000
Oh Ben, you are right. I added the multibyte-safe version. I think it is not necessary to use Zend_Text_MultiByte class because of dependency.
Posted by Thomas Weidner (thomas) on 2009-04-17T00:00:51.000+0000
A proposal where this feature is part of has been added to Wiki. Waiting for Recommendation and Response from Zend since 15.April.2009
Posted by Thomas Weidner (thomas) on 2009-12-07T15:09:52.000+0000
Closing as "needs proposal". The proposal is already written and waiting for acceptance. See here for details and vote for it: http://framework.zend.com/wiki/display/…