/class/verifier/VerifierManager.php

https://github.com/JeCat/framework · PHP · 139 lines · 90 code · 22 blank · 27 comment · 7 complexity · f68df19ecafa4da0ff341c47f28ea8b7 MD5 · raw file

  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3. // 这个文件是 JeCat PHP框架的一部分,该项目和此文件 均遵循 GNU 自由软件协议
  4. //
  5. // Copyleft 2008-2012 JeCat.cn(http://team.JeCat.cn)
  6. //
  7. //
  8. // JeCat PHP框架 的正式全名是:Jellicle Cat PHP Framework。
  9. // “Jellicle Cat”出自 Andrew Lloyd Webber的音乐剧《猫》(《Prologue:Jellicle Songs for Jellicle Cats》)。
  10. // JeCat 是一个开源项目,它像音乐剧中的猫一样自由,你可以毫无顾忌地使用JCAT PHP框架。JCAT 由中国团队开发维护。
  11. // 正在使用的这个版本是:0.7.1
  12. //
  13. //
  14. //
  15. // 相关的链接:
  16. // [主页] http://www.JeCat.cn
  17. // [源代码] https://github.com/JeCat/framework
  18. // [下载(http)] https://nodeload.github.com/JeCat/framework/zipball/master
  19. // [下载(git)] git clone git://github.com/JeCat/framework.git jecat
  20. // 不很相关:
  21. // [MP3] http://www.google.com/search?q=jellicle+songs+for+jellicle+cats+Andrew+Lloyd+Webber
  22. // [VCD/DVD] http://www.google.com/search?q=CAT+Andrew+Lloyd+Webber+video
  23. //
  24. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  25. /*-- Project Introduce --*/
  26. namespace org\jecat\framework\verifier ;
  27. use org\jecat\framework\lang\Type;
  28. use org\jecat\framework\lang\Object;
  29. class VerifierManager extends Object
  30. {
  31. public function __construct($bLogic = true){
  32. $this->setLogic($bLogic);
  33. }
  34. public function add(IVerifier $aVerifier, $sExceptionWords=null, $callback=null, $arrCallbackArgvs=array())
  35. {
  36. if( in_array($aVerifier,$this->arrVerifiers) )
  37. {
  38. return $this ;
  39. }
  40. $this->arrVerifiers[] = $aVerifier ;
  41. $nIdx = array_search($aVerifier, $this->arrVerifiers) ;
  42. $this->arrVerifierOthers[$nIdx] = array(
  43. $sExceptionWords, $callback, $arrCallbackArgvs
  44. ) ;
  45. // 连续操作
  46. return $this ;
  47. }
  48. public function setLogic($bLogic){
  49. $this->bLogic = (bool)$bLogic;
  50. return $this ;
  51. }
  52. public function logic(){
  53. return $this->bLogic;
  54. }
  55. public function remove(IVerifier $aVerifier)
  56. {
  57. $nIdx = array_search($aVerifier, $this->arrVerifiers) ;
  58. if( $nIdx===false )
  59. {
  60. return ;
  61. }
  62. unset($this->arrVerifiers[$nIdx]) ;
  63. unset($this->arrVerifierOthers[$nIdx]) ;
  64. }
  65. public function clear()
  66. {
  67. $this->arrVerifiers = array() ;
  68. $this->arrVerifierOthers = array() ;
  69. }
  70. public function count()
  71. {
  72. return count($this->arrVerifiers) ;
  73. }
  74. public function iterator()
  75. {
  76. return new \org\jecat\framework\pattern\iterate\ArrayIterator($this->arrVerifiers) ;
  77. }
  78. public function verify($value,$bThrowExcetion=false)
  79. {
  80. $aVerifyFailed = new VerifyFailed('');
  81. foreach($this->arrVerifiers as $nIdx=>$aVerifier)
  82. {
  83. try{
  84. $aVerifier->verify( $value, true ) ;
  85. } catch (VerifyFailed $e) {
  86. // 通过回调函数报告错误
  87. if( $this->arrVerifierOthers[$nIdx][1] )
  88. {
  89. call_user_func_array(
  90. $this->arrVerifierOthers[$nIdx][1]
  91. , array_merge(
  92. array( $value, $aVerifier, $e, $this->arrVerifierOthers[$nIdx][0] )
  93. , Type::toArray($this->arrVerifierOthers[$nIdx][2],Type::toArray_normal)
  94. )
  95. ) ;
  96. }
  97. // 抛出异常
  98. else if($bThrowExcetion)
  99. {
  100. if( $this->arrVerifierOthers[$nIdx][0] )
  101. {
  102. throw new VerifyFailed($this->arrVerifierOthers[$nIdx][0],null,$e) ;
  103. }
  104. else
  105. {
  106. throw $e ;
  107. }
  108. }
  109. return false ;
  110. }
  111. }
  112. return true;
  113. }
  114. private $arrVerifiers = array() ;
  115. private $arrVerifierOthers = array() ;
  116. private $bLogic = true; //true校验器之间为and关系, false校验器之间为or关系
  117. }