MaxLineLength.php

Go to the documentation of this file.
00001 <?php
00002 
00031 class Robo47_Validate_MaxLineLength extends Zend_Validate_Abstract
00032 {
00036     const LINE_TOO_LONG = 'lineToLong';
00037     
00038     protected $_messageTemplates = array(
00039         self::LINE_TOO_LONG => "Line %value% is too long"
00040     );
00046     protected $_lineLength = 80;
00052     protected $_encoding = 'utf-8';
00053 
00058     public function __construct($lineLength = 80, $encoding = 'utf-8')
00059     {
00060         $this->setLineLength($lineLength);
00061         $this->setEncoding($encoding);
00062     }
00063 
00070     public function setEncoding($encoding)
00071     {
00072         $this->_encoding = $encoding;
00073         return $this;
00074     }
00075 
00081     public function getEncoding()
00082     {
00083         return $this->_encoding;
00084     }
00085 
00092     public function setLineLength($lineLength)
00093     {
00094         $lineLength = (int) $lineLength;
00095         if ($lineLength < 1) {
00096             $message = 'lineLength is less than 1';
00097             throw new Robo47_Validate_Exception($message);
00098         } else {
00099             $this->_lineLength = $lineLength;
00100         }
00101         return $this;
00102     }
00103 
00109     public function getLineLength()
00110     {
00111         return $this->_lineLength;
00112     }
00113 
00121     public function isValid($value)
00122     {
00123         // replace other line-endings
00124         $value = str_replace("\n\r", "\n", $value);
00125         $value = str_replace("\r\n", "\n", $value);
00126         // create array
00127         $asArray = explode("\n", $value);
00128 
00129         foreach ($asArray as $linenumber => $line) {
00130             if (mb_strwidth($line, $this->_encoding) > $this->_lineLength) {
00131                 $this->_error(self::LINE_TOO_LONG, $linenumber + 1);
00132                 return false;
00133             }
00134         }
00135         return true;
00136     }
00137 }