PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Array.php

https://github.com/jeremyFreeAgent/Filter
PHP | 122 lines | 32 code | 17 blank | 73 comment | 4 complexity | 9a4cb82530a954fda32c5e8ab41d3e1c MD5 | raw file
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2013, Ivan Enderlin. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. *
  36. * @category Framework
  37. * @package Hoa_Filter
  38. * @subpackage Hoa_Filter_Array
  39. *
  40. */
  41. /**
  42. * Hoa_Filter_Abstract
  43. */
  44. import('Filter.Abstract');
  45. /**
  46. * Hoa_Filter
  47. */
  48. import('Filter.~');
  49. /**
  50. * Class Hoa_Filter_Array.
  51. *
  52. * Apply filters on an array.
  53. *
  54. * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
  55. * @copyright Copyright © 2007-2013 Ivan Enderlin.
  56. * @license New BSD License
  57. * @since PHP 5
  58. * @version 0.1
  59. * @package Hoa_Filter
  60. * @subpackage Hoa_Filter_Array
  61. */
  62. class Hoa_Filter_Array extends Hoa_Filter_Abstract {
  63. /**
  64. * Needed arguments.
  65. *
  66. * @var Hoa_Filter_Abstract array
  67. */
  68. protected $arguments = array(
  69. 'filters' => 'specify an associative array of key => filter to apply.'
  70. );
  71. /**
  72. * Apply filters.
  73. * In this case, the string must be an array. If it is not an array, the
  74. * string will be convert to an array.
  75. *
  76. * @access public
  77. * @param string $string The string to filter.
  78. * @return array
  79. * @throw Hoa_Filter_Exception
  80. */
  81. public function filter ( $string = null ) {
  82. if(!is_array($string))
  83. $string = array($string);
  84. $filters = $this->getFilterArgument('filters');
  85. $lastFilter = current($filters);
  86. foreach($filters as $key => &$filter)
  87. if($filter === null)
  88. $filter = $lastFilter;
  89. else
  90. $lastFilter = $filters[$key];
  91. foreach($string as $key => &$value) {
  92. $add = new Hoa_Filter();
  93. if(!isset($filters[$key]))
  94. if(isset($filters['*']))
  95. $add->addFilter($filters['*']);
  96. else
  97. continue;
  98. else
  99. $add->addFilter($filters[$key]);
  100. $value = $add->filter($value);
  101. $add = null;
  102. }
  103. return $string;
  104. }
  105. }