Permalink
Browse files

Alpha Filter

Added support for a simplistic alpha filter, with unicode support but not locale. closes #6.
  • Loading branch information...
1 parent df9f5d9 commit 58bfb636d0d1d066c20727b943932471f9f405b4 @rdohms committed Apr 25, 2012
Showing with 104 additions and 0 deletions.
  1. +45 −0 src/DMS/Filter/Rules/Alpha.php
  2. +59 −0 tests/DMS/Filter/Rules/AlphaTest.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace DMS\Filter\Rules;
+
+/**
+ * Alpha Rule
+ *
+ * @package DMS
+ * @subpackage Filter
+ *
+ * @Annotation
+ */
+class Alpha 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{L}' . $whitespaceChar . ']/u';
+ $this->pattern = '/[^a-zA-Z' . $whitespaceChar . ']/';
+
+ return parent::applyFilter($value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getDefaultOption()
+ {
+ return 'allowWhitespace';
+ }
+
+}
@@ -0,0 +1,59 @@
+<?php
+
+namespace DMS\Filter\Rules;
+
+use Tests;
+
+class AlphaTest extends Tests\Testcase
+{
+
+ public function setUp()
+ {
+ parent::setUp();
+ }
+
+ public function tearDown()
+ {
+ parent::tearDown();
+ }
+
+ /**
+ * @dataProvider provideForRule
+ */
+ public function testRule($options, $value, $expectedResult, $unicodeSetting = null)
+ {
+ $rule = new Alpha($options);
+
+ if ($unicodeSetting !== null) {
+
+ $property = new \ReflectionProperty($rule, 'unicodeEnabled');
+ $property->setAccessible(true);
+ $property->setValue($rule, $unicodeSetting);
+
+ }
+
+ $result = $rule->applyFilter($value);
+
+ $this->assertEquals($expectedResult, $result);
+ }
+
+ public function provideForRule()
+ {
+ return array(
+ array(false, "My Text", "MyText", true),
+ array(false, "My Text", "MyText", false),
+ array(true, "My Text", "My Text", true),
+ array(true, "My Text", "My Text", false),
+ array(true, "My Text!", "My Text", true),
+ array(true, "My Text!", "My Text", false),
+ array(true, "My Text21!", "My Text", true),
+ array(true, "My Text21!", "My Text", false),
+ array(true, "João 2Sorrisão", "João Sorrisão", true),
+ array(true, "João 2Sorrisão", "Joo Sorriso", false),
+ array(true, "Helgi Þormar Þorbjörnsson", "Helgi Þormar Þorbjörnsson", true),
+ array(true, "Helgi Þormar Þorbjörnsson", "Helgi ormar orbjrnsson", false),
+ array(true, "Helgi Þormar!@#$&*( )(*&%$#@Þorbjörnsson", "Helgi Þormar Þorbjörnsson", true),
+ array(true, "Helgi Þormar!@#$&*( )(*&%$#@Þorbjörnsson", "Helgi ormar orbjrnsson", false),
+ );
+ }
+}

0 comments on commit 58bfb63

Please sign in to comment.