File indexing completed on 2024-12-22 05:37:12

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category  Zend
0016  * @package   Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version   $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_File_Count
0024  */
0025 // require_once 'Zend/Validate/File/Count.php';
0026 
0027 /**
0028  * Validator for counting all words in a file
0029  *
0030  * @category  Zend
0031  * @package   Zend_Validate
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Validate_File_WordCount extends Zend_Validate_File_Count
0036 {
0037     /**#@+
0038      * @const string Error constants
0039      */
0040     const TOO_MUCH  = 'fileWordCountTooMuch';
0041     const TOO_LESS  = 'fileWordCountTooLess';
0042     const NOT_FOUND = 'fileWordCountNotFound';
0043     /**#@-*/
0044 
0045     /**
0046      * @var array Error message templates
0047      */
0048     protected $_messageTemplates = array(
0049         self::TOO_MUCH => "Too much words, maximum '%max%' are allowed but '%count%' were counted",
0050         self::TOO_LESS => "Too less words, minimum '%min%' are expected but '%count%' were counted",
0051         self::NOT_FOUND => "File '%value%' is not readable or does not exist",
0052     );
0053 
0054     /**
0055      * Defined by Zend_Validate_Interface
0056      *
0057      * Returns true if and only if the counted words are at least min and
0058      * not bigger than max (when max is not null).
0059      *
0060      * @param  string $value Filename to check for word count
0061      * @param  array  $file  File data from Zend_File_Transfer
0062      * @return boolean
0063      */
0064     public function isValid($value, $file = null)
0065     {
0066         // Is file readable ?
0067         // require_once 'Zend/Loader.php';
0068         if (!Zend_Loader::isReadable($value)) {
0069             return $this->_throw($file, self::NOT_FOUND);
0070         }
0071 
0072         $content = file_get_contents($value);
0073         $this->_count = str_word_count($content);
0074         if (($this->_max !== null) && ($this->_count > $this->_max)) {
0075             return $this->_throw($file, self::TOO_MUCH);
0076         }
0077 
0078         if (($this->_min !== null) && ($this->_count < $this->_min)) {
0079             return $this->_throw($file, self::TOO_LESS);
0080         }
0081 
0082         return true;
0083     }
0084 
0085     /**
0086      * Throws an error of the given type
0087      *
0088      * @param  string $file
0089      * @param  string $errorType
0090      * @return false
0091      */
0092     protected function _throw($file, $errorType)
0093     {
0094         if ($file !== null) {
0095             $this->_value = $file['name'];
0096         }
0097 
0098         $this->_error($errorType);
0099         return false;
0100     }
0101 }