PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Filter/Alnum.php

http://github.com/zendframework/zf2
PHP | 172 lines | 70 code | 19 blank | 83 comment | 13 complexity | 82acc083dd99c66c3811db2328dfe08d MD5 | raw file
Possible License(s): BSD-3-Clause
  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_Filter
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Filter;
  24. use Zend\Config\Config,
  25. Zend\Locale\Locale as ZendLocale,
  26. Zend\Registry;
  27. /**
  28. * @uses Zend\Filter\AbstractFilter
  29. * @uses Zend\Locale\Locale
  30. * @category Zend
  31. * @package Zend_Filter
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Alnum extends AbstractFilter
  36. {
  37. /**
  38. * Whether to allow white space characters; off by default
  39. *
  40. * @var boolean
  41. */
  42. protected $allowWhiteSpace;
  43. /**
  44. * Is PCRE is compiled with UTF-8 and Unicode support
  45. *
  46. * @var mixed
  47. **/
  48. protected static $unicodeEnabled;
  49. /**
  50. * Locale to use
  51. *
  52. * @var \Zend\Locale\Locale object
  53. */
  54. protected $locale;
  55. /**
  56. * Sets default option values for this instance
  57. *
  58. * @param boolean $allowWhiteSpace
  59. * @return void
  60. */
  61. public function __construct($options = false)
  62. {
  63. if ($options instanceof Config) {
  64. $options = $options->toArray();
  65. } elseif (!is_array($options)) {
  66. $options = func_get_args();
  67. $temp = array();
  68. if (!empty($options)) {
  69. $temp['allowWhiteSpace'] = array_shift($options);
  70. }
  71. if (!empty($options)) {
  72. $temp['locale'] = array_shift($options);
  73. }
  74. $options = $temp;
  75. }
  76. if (null === self::$unicodeEnabled) {
  77. self::$unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;
  78. }
  79. if (array_key_exists('allowWhiteSpace', $options)) {
  80. $this->setAllowWhiteSpace($options['allowWhiteSpace']);
  81. }
  82. if (!array_key_exists('locale', $options)) {
  83. $options['locale'] = null;
  84. }
  85. $this->setLocale($options['locale']);
  86. }
  87. /**
  88. * Returns the allowWhiteSpace option
  89. *
  90. * @return boolean
  91. */
  92. public function getAllowWhiteSpace()
  93. {
  94. return $this->allowWhiteSpace;
  95. }
  96. /**
  97. * Sets the allowWhiteSpace option
  98. *
  99. * @param boolean $allowWhiteSpace
  100. * @return \Zend\Filter\Alnum Provides a fluent interface
  101. */
  102. public function setAllowWhiteSpace($allowWhiteSpace)
  103. {
  104. $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
  105. return $this;
  106. }
  107. /**
  108. * Returns the locale option
  109. *
  110. * @return string
  111. */
  112. public function getLocale()
  113. {
  114. return $this->locale;
  115. }
  116. /**
  117. * Sets the locale option
  118. *
  119. * @param boolean $locale
  120. * @return \Zend\Filter\Alnum Provides a fluent interface
  121. */
  122. public function setLocale($locale = null)
  123. {
  124. $this->locale = ZendLocale::findLocale($locale);
  125. return $this;
  126. }
  127. /**
  128. * Defined by Zend_Filter_Interface
  129. *
  130. * Returns the string $value, removing all but alphabetic and digit characters
  131. *
  132. * @param string $value
  133. * @return string
  134. */
  135. public function filter($value)
  136. {
  137. $whiteSpace = $this->allowWhiteSpace ? '\s' : '';
  138. if (!self::$unicodeEnabled) {
  139. // POSIX named classes are not supported, use alternative a-zA-Z0-9 match
  140. $pattern = '/[^a-zA-Z0-9' . $whiteSpace . ']/';
  141. } elseif (((string) $this->locale == 'ja')
  142. || ((string) $this->locale == 'ko')
  143. || ((string) $this->locale == 'zh')
  144. ) {
  145. // Use english alphabeth
  146. $pattern = '/[^a-zA-Z0-9' . $whiteSpace . ']/u';
  147. } else {
  148. // Use native language alphabeth
  149. $pattern = '/[^\p{L}\p{N}' . $whiteSpace . ']/u';
  150. }
  151. return preg_replace($pattern, '', (string) $value);
  152. }
  153. }