Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Digits Filter

Added Digits filter with unicode support. closes #8.
Refactored Alnum filter to extend common regexp base
  • Loading branch information...
commit 1824ceb095f78fbca43308f4fedf64bc3decb609 1 parent 6b77d45
@rdohms authored
Showing with 125 additions and 27 deletions.
  1. +16 −27 Rules/Alnum.php
  2. +45 −0 Rules/Digits.php
  3. +64 −0 Rules/RegExp.php
View
43 Rules/Alnum.php
@@ -3,19 +3,23 @@
namespace DMS\Filter\Rules;
/**
- * Alnum Rule (Alpanumeric)
- *
+ * Alnum Rule (Alphanumeric)
+ *
* @package DMS
* @subpackage Filter
- *
+ *
* @Annotation
*/
-class Alnum extends Rule
+class Alnum extends RegExp
{
- protected static $unicodeEnabled;
-
+
+ /**
+ * Allow Whitespace or not
+ *
+ * @var bool
+ */
public $allowWhitespace = true;
-
+
/**
* {@inheritDoc}
*/
@@ -23,28 +27,13 @@ public function applyFilter($value)
{
//Check for Whitespace support
$whitespaceChar = ($this->allowWhitespace)? " ":"";
-
- //Build pattern
- $pattern = ($this->checkUnicodeSupport())?
- '/[^\p{L}\p{N}' . $whitespaceChar . ']/u' :
- '/[^a-zA-Z0-9' . $whitespaceChar . ']/' ;
-
- return preg_replace($pattern, '', $value);
- }
- /**
- * Verifies that Regular Expression functions support unicode
- * @return boolean
- */
- public function checkUnicodeSupport()
- {
- if (null === self::$unicodeEnabled) {
- self::$unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;
- }
-
- return self::$unicodeEnabled;
+ $this->unicodePattern = '/[^\p{L}\p{N}' . $whitespaceChar . ']/u';
+ $this->pattern = '/[^a-zA-Z0-9' . $whitespaceChar . ']/';
+
+ return parent::applyFilter($value);
}
-
+
/**
* {@inheritDoc}
*/
View
45 Rules/Digits.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace DMS\Filter\Rules;
+
+/**
+ * Digits Rule
+ *
+ * @package DMS
+ * @subpackage Filter
+ *
+ * @Annotation
+ */
+class Digits extends RegExp
+{
+
+ /**
+ * Allow Whitespace or not
+ *
+ * @var bool
+ */
+ public $allowWhitespace = true;
+
+ /**
+ * {@inheritDoc}
+ */
+ public function applyFilter($value)
+ {
+ //Check for Whitespace support
+ $whitespaceChar = ($this->allowWhitespace)? " ":"";
+
+ $this->unicodePattern = '/[^\p{N}' . $whitespaceChar . ']/';
+ $this->pattern = '/[^0-9' . $whitespaceChar . ']/';
+
+ return parent::applyFilter($value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getDefaultOption()
+ {
+ return 'allowWhitespace';
+ }
+
+}
View
64 Rules/RegExp.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace DMS\Filter\Rules;
+
+/**
+ * RegExp Rule
+ *
+ * Filter using preg_replace and unicode or non-unicode patterns
+ *
+ * @package DMS
+ * @subpackage Filter
+ *
+ * @Annotation
+ */
+class RegExp extends Rule
+{
+ /**
+ * Defines if Unicode is supported
+ *
+ * @var boolean
+ */
+ protected static $unicodeEnabled;
+
+ /**
+ * Unicode version of Pattern
+ *
+ * @var string
+ */
+ public $unicodePattern;
+
+ /**
+ * Reg Exp Pattern
+ *
+ * @var string
+ */
+ public $pattern;
+
+ /**
+ * {@inheritDoc}
+ */
+ public function applyFilter($value)
+ {
+ //Build pattern
+ $pattern = ($this->checkUnicodeSupport() && $this->unicodePattern !== null)
+ ? $this->unicodePattern
+ : $this->pattern;
+
+ return preg_replace($pattern, '', $value);
+ }
+
+ /**
+ * Verifies that Regular Expression functions support unicode
+ * @return boolean
+ */
+ public function checkUnicodeSupport()
+ {
+ if (null === self::$unicodeEnabled) {
+ self::$unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;
+ }
+
+ return self::$unicodeEnabled;
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.