PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/class/Plugin/Validator/Max.php

http://github.com/ethna/ethna
PHP | 128 lines | 78 code | 14 blank | 36 comment | 23 complexity | a346619b3e179a4859088816173f4ae5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4. * Max.php
  5. *
  6. * @author ICHII Takashi <ichii386@schweetheart.jp>
  7. * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  8. * @package Ethna
  9. * @version $Id: 14c12a1fa15fe1054ff8a4fc4f50017e2f8dfa43 $
  10. */
  11. // {{{ Ethna_Plugin_Validator_Max
  12. /**
  13. * 最大値チェックプラグイン
  14. *
  15. * @author ICHII Takashi <ichii386@schweetheart.jp>
  16. * @access public
  17. * @package Ethna
  18. */
  19. class Ethna_Plugin_Validator_Max extends Ethna_Plugin_Validator
  20. {
  21. /** @var bool 配列を受け取るかフラグ */
  22. public $accept_array = false;
  23. /**
  24. * 最大値のチェックを行う
  25. *
  26. * @access public
  27. * @param string $name フォームの名前
  28. * @param mixed $var フォームの値
  29. * @param array $params プラグインのパラメータ
  30. */
  31. public function validate($name, $var, $params)
  32. {
  33. $true = true;
  34. $type = $this->getFormType($name);
  35. if (isset($params['max']) == false || $this->isEmpty($var, $type)) {
  36. return $true;
  37. }
  38. switch ($type) {
  39. case VAR_TYPE_INT:
  40. if ($var > $params['max']) {
  41. if (isset($params['error'])) {
  42. $msg = $params['error'];
  43. } else {
  44. $msg = _et('Please input less than %d(int) to {form}.');
  45. }
  46. return Ethna::raiseNotice($msg, E_FORM_MAX_INT, array($params['max']));
  47. }
  48. break;
  49. case VAR_TYPE_FLOAT:
  50. if ($var > $params['max']) {
  51. if (isset($params['error'])) {
  52. $msg = $params['error'];
  53. } else {
  54. $msg = _et('Please input less than %f(float) to {form}.');
  55. }
  56. return Ethna::raiseNotice($msg, E_FORM_MAX_FLOAT, array($params['max']));
  57. }
  58. break;
  59. case VAR_TYPE_DATETIME:
  60. $t_max = strtotime($params['max']);
  61. $t_var = strtotime($var);
  62. if ($t_var > $t_max) {
  63. if (isset($params['error'])) {
  64. $msg = $params['error'];
  65. } else {
  66. $msg = _et('Please input datetime value before %s to {form}.');
  67. }
  68. return Ethna::raiseNotice($msg, E_FORM_MAX_DATETIME, array($params['max']));
  69. }
  70. break;
  71. case VAR_TYPE_FILE:
  72. $st = stat($var['tmp_name']);
  73. if ($st[7] > $params['max'] * 1024) {
  74. if (isset($params['error'])) {
  75. $msg = $params['error'];
  76. } else {
  77. $msg = _et('Please specify file whose size is less than %d KB to {form}.');
  78. }
  79. return Ethna::raiseNotice($msg, E_FORM_MAX_FILE, array($params['max']));
  80. }
  81. break;
  82. case VAR_TYPE_STRING:
  83. //
  84. // マルチバイトエンコーディングと、そうでない場合で
  85. // 異なるプラグインを呼ぶ。
  86. //
  87. // これは Ethna_Controller#client_encoding の値によ
  88. // って動きが決まる
  89. //
  90. $ctl = Ethna_Controller::getInstance();
  91. $client_enc = $ctl->getClientEncoding();
  92. $plugin = $this->backend->getPlugin();
  93. // select Plugin.
  94. if (mb_enabled() && strcasecmp('UTF-8', $client_enc) == 0) {
  95. $plugin_name = 'Mbstrmax';
  96. $params['mbstrmax'] = $params['max'];
  97. } elseif (strcasecmp('EUC-JP', $client_enc == 0)
  98. || strcasecmp('eucJP-win', $client_enc == 0)) {
  99. // 2.3.x compatibility
  100. $plugin_name = 'Strmaxcompat';
  101. $params['strmaxcompat'] = $params['max'];
  102. } else {
  103. $plugin_name = 'Strmax';
  104. $params['strmax'] = $params['max'];
  105. }
  106. unset($params['max']);
  107. $vld = $plugin->getPlugin('Validator', $plugin_name);
  108. return $vld->validate($name, $var, $params);
  109. break;
  110. }
  111. return $true;
  112. }
  113. }
  114. // }}}