Skip to content

Commit

Permalink
Merge pull request #2 from GromNaN/static
Browse files Browse the repository at this point in the history
Allows to call the `validate` method statically
  • Loading branch information
willdurand committed Jan 20, 2014
2 parents 29e9306 + 656f237 commit 1a7d388
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ $validator->validate('(function xss(x){evil()})');
// returns `false`
```

Or as a static method:

```php
\JsonpCallbackValidator::validate('JSONP.callback');
// returns `true`

\JsonpCallbackValidator::validate('(function xss(x){evil()})');
// returns `false`
```

Installation
------------
Expand Down
10 changes: 5 additions & 5 deletions src/JsonpCallbackValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*/
class JsonpCallbackValidator
{
private $regexp = '/^[a-zA-Z_$][0-9a-zA-Z_$]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/';
private static $regexp = '/^[a-zA-Z_$][0-9a-zA-Z_$]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/';

private $reservedKeywords = array(
private static $reservedKeywords = array(
'break',
'do',
'instanceof',
Expand Down Expand Up @@ -62,14 +62,14 @@ class JsonpCallbackValidator
* @param string $callback
* @return boolean
*/
public function validate($callback)
public static function validate($callback)
{
foreach (explode('.', $callback) as $identifier) {
if (!preg_match($this->regexp, $identifier)) {
if (!preg_match(self::$regexp, $identifier)) {
return false;
}

if (in_array($identifier, $this->reservedKeywords)) {
if (in_array($identifier, self::$reservedKeywords)) {
return false;
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/JsonpCallbackValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ public static function dataProviderForTestValidate()
array("array_of_functions['\'']", self::IS_VALID),
);
}

public function testCallStatically()
{
$this->assertTrue(\JsonpCallbackValidator::validate('foo'));
}
}

0 comments on commit 1a7d388

Please sign in to comment.