2
0

WordCount.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_File_Count
  23. */
  24. require_once 'Zend/Validate/File/Count.php';
  25. /**
  26. * Validator for counting all words in a file
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Validate_File_WordCount extends Zend_Validate_File_Count
  34. {
  35. /**#@+
  36. * @const string Error constants
  37. */
  38. const TOO_MUCH = 'fileWordCountTooMuch';
  39. const TOO_LESS = 'fileWordCountTooLess';
  40. const NOT_FOUND = 'fileWordCountNotFound';
  41. /**#@-*/
  42. /**
  43. * @var array Error message templates
  44. */
  45. protected $_messageTemplates = array(
  46. self::TOO_MUCH => "Too much words, maximum '%max%' are allowed but '%count%' were counted",
  47. self::TOO_LESS => "Too less words, minimum '%min%' are expected but '%count%' were counted",
  48. self::NOT_FOUND => "File '%value%' is not readable or does not exist",
  49. );
  50. /**
  51. * Defined by Zend_Validate_Interface
  52. *
  53. * Returns true if and only if the counted words are at least min and
  54. * not bigger than max (when max is not null).
  55. *
  56. * @param string $value Filename to check for word count
  57. * @param array $file File data from Zend_File_Transfer
  58. * @return boolean
  59. */
  60. public function isValid($value, $file = null)
  61. {
  62. // Is file readable ?
  63. require_once 'Zend/Loader.php';
  64. if (!Zend_Loader::isReadable($value)) {
  65. return $this->_throw($file, self::NOT_FOUND);
  66. }
  67. $content = file_get_contents($value);
  68. $this->_count = str_word_count($content);
  69. if (($this->_max !== null) && ($this->_count > $this->_max)) {
  70. return $this->_throw($file, self::TOO_MUCH);
  71. }
  72. if (($this->_min !== null) && ($this->_count < $this->_min)) {
  73. return $this->_throw($file, self::TOO_LESS);
  74. }
  75. return true;
  76. }
  77. /**
  78. * Throws an error of the given type
  79. *
  80. * @param string $file
  81. * @param string $errorType
  82. * @return false
  83. */
  84. protected function _throw($file, $errorType)
  85. {
  86. if ($file !== null) {
  87. $this->_value = $file['name'];
  88. }
  89. $this->_error($errorType);
  90. return false;
  91. }
  92. }