Permalink
Browse files

Initial Commit

  • Loading branch information...
ircmaxell committed Jan 15, 2013
0 parents commit e2e6fafc94dc018711a207103571973aa3a896b1
@@ -0,0 +1 @@
vendor
@@ -0,0 +1,11 @@
{
"require": {
"php": ">=5.3.0"
},
"autoload": {
"files": ["lib/Filterus/functions.php"],
"psr-0": {
"Filterus": "lib"
}
}
}
@@ -0,0 +1,105 @@
<?php
namespace Filterus;
abstract class Filter {
protected static $filters = array(
'alnum' => 'Filterus\Filters\Alnum',
'array' => 'Filterus\Filters\Arrays',
'bool' => 'Filterus\Filters\Boolean',
'email' => 'Filterus\Filters\Email',
'float' => 'Filterus\Filters\Float',
'int' => 'Filterus\Filters\Int',
'ip' => 'Filterus\Filters\IP',
'object' => 'Filterus\Filters\Object',
'raw' => 'Filterus\Filters\Raw',
'regex' => 'Filterus\Filters\Regex',
'string' => 'Filterus\Filters\String',
'url' => 'Filterus\Filters\URL',
);
public static function arrays($filter = '', $keys = '', $values = '') {
list(, $options) = static::parseFilter('array,' . $filter);
if ($keys) {
$options['keys'] = $keys;
}
if ($values) {
$options['values'] = $values;
}
return new Filters\Arrays($options);
}
public static function map($filters) {
return new Filters\Map(array('filters' => $filters));
}
public static function chain() {
$filters = func_get_args();
return new Filters\Chain(array('filters' => $filters));
}
public static function pool() {
$filters = func_get_args();
return new Filters\Pool(array('filters' => $filters));
}
public static function factory($filter) {
if ($filter instanceof self) {
return $filter;
}
list ($filterName, $options) = static::parseFilter($filter);
if (!isset(self::$filters[$filterName])) {
throw new \RuntimeException('Invalid Filter Specified: ' . $filter);
}
$class = self::$filters[$filterName];
return new $class($options);
}
public static function registerFilter($name, $class) {
if (!$class instanceof self) {
throw new \InvalidArgumentException("Class name must be an instance of Filter");
}
$this->filters[strtolower($name)] = $class;
}
protected $defaultOptions = array();
protected $options = array();
abstract public function filter($var);
final public function __construct(array $options = array()) {
$this->setOptions($options);
}
public function setOption($key, $value) {
$this->options[$key] = $value;
return $this;
}
public function setOptions(array $options) {
$this->options = $options + $this->defaultOptions;
return $this;
}
public function validate($var) {
return $var == $this->filter($var);
}
protected static function parseFilter($filter) {
$parts = explode(',', $filter);
$filterName = strtolower(array_shift($parts));
$options = array();
foreach ($parts as $part) {
$part = trim($part);
if (empty($part)) {
continue;
}
list ($name, $value) = explode(':', $part, 2);
$options[$name] = $value;
}
return array($filterName, $options);
}
}
@@ -0,0 +1,14 @@
<?php
namespace Filterus\Filters;
class Alnum extends Regex {
protected $defaultOptions = array(
'min' => 0,
'max' => PHP_INT_MAX,
'default' => '',
'regex' => '/^[a-zA-Z0-9]*$/',
);
}
@@ -0,0 +1,51 @@
<?php
namespace Filterus\Filters;
class Arrays extends \Filterus\Filter {
protected $defaultOptions = array(
'min' => 0,
'max' => PHP_INT_MAX,
'keys' => null,
'values' => null,
'default' => array(),
);
public function filter($var) {
if (!is_array($var)) {
return $this->options['default'];
}
$count = count($var);
if ($this->options['min'] > $count) {
return $this->options['default'];
} elseif ($this->options['max'] < $count) {
return $this->options['default'];
}
if ($this->options['keys']) {
$filter = self::factory($this->options['keys']);
foreach ($var as $key => $value) {
if (!$filter->validate($key)) {
unset($var[$key]);
}
}
}
if ($this->options['values']) {
$filter = self::factory($this->options['values']);
foreach ($var as $key => $value) {
if (!$filter->validate($value)) {
unset($var[$key]);
}
}
}
return $var;
}
public function validate($var) {
if (!is_array($var)) {
return false;
}
return parent::validate($var);
}
}
@@ -0,0 +1,18 @@
<?php
namespace Filterus\Filters;
class Boolean extends \Filterus\Filter {
protected $defaultOptions = array(
'default' => null,
);
public function filter($var) {
return filter_var($var, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
public function validate($var) {
return $this->filter($var) !== null;
}
}
@@ -0,0 +1,30 @@
<?php
namespace Filterus\Filters;
class Chain extends \Filterus\Filter {
protected $defaultOptions = array(
'filters' => array(),
);
public function filter($var) {
foreach ($this->options['filters'] as $filter) {
$filter = self::factory($filter);
$var = $filter->filter($var);
}
return $var;
}
public function validate($var) {
foreach ($this->options['filters'] as $filter) {
$filter = self::factory($filter);
if (!$filter->validate($var)) {
return false;
}
$var = $filter->filter($var);
}
return true;
}
}
@@ -0,0 +1,11 @@
<?php
namespace Filterus\Filters;
class Email extends \Filterus\Filter {
public function filter($var) {
return filter_var($var, FILTER_VALIDATE_EMAIL);
}
}
@@ -0,0 +1,32 @@
<?php
namespace Filterus\Filters;
class Float extends \Filterus\Filter {
protected $defaultOptions = array(
'min' => null,
'max' => null,
'default' => 0.0,
);
public function filter($var) {
if (!is_numeric($var)) {
return $this->options['default'];
}
$var = (float) $var;
if (null !== $this->options['min'] && $this->options['min'] > $var) {
return $this->options['min'];
} elseif (null !== $this->options['max'] && $this->options['max'] < $var) {
return $this->options['max'];
}
return $var;
}
public function validate($var) {
if (!is_numeric($var)) {
return false;
}
return parent::validate($var);
}
}
@@ -0,0 +1,31 @@
<?php
namespace Filterus\Filters;
class IP extends \Filterus\Filter {
protected $defaultOptions = array(
'ipv4' => true,
'ipv6' => true,
'private' => true,
'reserved' => true,
);
public function filter($var) {
$flags = 0;
if ($this->options['ipv4']) {
$flags |= FILTER_FLAG_IPV4;
}
if ($this->options['ipv6']) {
$flags |= FILTER_FLAG_IPV6;
}
if (!$this->option['private']) {
$flags |= FILTER_FLAG_NO_PRIV_RANGE;
}
if (!$this->options['reserved']) {
$flags |= FILTER_FLAG_NO_RES_RANGE;
}
return filter_var($var, FILTER_VALIDATE_IP, $flags);
}
}
@@ -0,0 +1,34 @@
<?php
namespace Filterus\Filters;
defined('PHP_INT_MIN') or define('PHP_INT_MIN', ~PHP_INT_MAX);
class Int extends \Filterus\Filter {
protected $defaultOptions = array(
'min' => PHP_INT_MIN,
'max' => PHP_INT_MAX,
'default' => 0,
);
public function filter($var) {
if (!is_numeric($var)) {
return $this->options['default'];
}
$var = (int) $var;
if ($this->options['min'] > $var) {
return $this->options['min'];
} elseif ($this->options['max'] < $var) {
return $this->options['max'];
}
return $var;
}
public function validate($var) {
if (!is_numeric($var)) {
return false;
}
return parent::validate($var);
}
}
Oops, something went wrong.

0 comments on commit e2e6faf

Please sign in to comment.