ircmaxell / filterus Goto Github PK
View Code? Open in Web Editor NEWA simple filtering library for PHP
A simple filtering library for PHP
I have a validation requirement where an array can look like either of these:
[ 'name' => 'string',
'from_address' => 'email',
'from_name' => 'string',
'subject' => 'string',
'body_plain' => 'string',
'to_address' => 'email', ] // Differs just in this key
[ 'name' => 'string',
'from_address' => 'email',
'from_name' => 'string',
'subject' => 'string',
'body_plain' => 'string',
'to_address_field' => 'string', ] // Differs just in this key
My strategy is that, since there is no way to specify keys as optional, I could run a map for each one, and accept if either returns true
. On that basis, I thought I could use a pool as well.
use Filterus\Filter as F;
$baseValidator = [
'name' => 'string',
'from_address' => 'email',
'from_name' => 'string',
'subject' => 'string',
'body_plain' => 'string',
];
// Use a pool of maps, should work?
$eitherValidators = F::pool([
F::map($baseValidator + ['to_address_field' => 'string', ]),
F::map($baseValidator + ['to_address' => 'email', ]),
]);
However, this blows up in the Filterus internals:
explode() expects parameter 2 to be string, array given
.../vendor/ircmaxell/filterus/lib/Filterus/Filter.php:98
.../vendor/ircmaxell/filterus/lib/Filterus/Filter.php:52
.../vendor/ircmaxell/filterus/lib/Filterus/Filters/Map.php:21
.../vendor/ircmaxell/filterus/lib/Filterus/Filters/Map.php:45
I'll write a custom validator for now, but would be interested to see how people would solve this.
Hi, thanks for providing this library. I have the following solution to allow "string or null", and will provide it below. However, if there is a better way to do this, please let me know.
First a null validator:
/**
* Class to allow nulls
*/
class AllowNull extends \Filterus\Filter
{
public function filter($var)
{
return $var;
}
public function validate($var)
{
return is_null($var);
}
}
Then register like so:
use Filterus\Filter as F;
F::registerFilter('null', AllowNull::class);
Finally use in a pool:
$validator = F::pool('string', 'null');
Having an array like
array(19) {
'sku' =>
string(2) "SW"
'_type' =>
string(6) "simple"
'_attribute_set' =>
string(18) "xxxxx"
'_product_websites' =>
string(10) "xxxxx"
'name' =>
NULL
'description' =>
string(8) "TESTTEST"
'short_description' =>
string(8) "TESTTEST"
'price' =>
int(0)
'url_key' =>
NULL
'cost' =>
NULL
'status' =>
int(1)
'visibility' =>
int(4)
'manufacturer' =>
string(9) "xxxx"
'tax_class_id' =>
int(2)
'base_price_unit' =>
string(3) "PCS"
'base_price_base_unit' =>
string(3) "PCS"
'country_of_manufacture' =>
string(11) "xxxxx"
'qty' =>
int(0)
'is_in_stock' =>
int(1)
}
And with this filter:
$filter = Filter::map(array(
'name' => 'string,min:1', //anything above 1 should do
'sku' => 'string,min:3', //prefix is hardcoded, so anything above strlen 2 will do
'_attribute_set' => 'string,min:1', //TODO Should be hardcoded to whatever is in $conf
'_product_websites' => 'string,min:1',
'_type' => 'string,min:1', //TODO Either configurable or simple
));
I would suspect that $filter->validate($product)
would throw back false
since "name" is clearly a) not a string and b) is not having a stringlengh of atleast 1.
But I do get true
as a result, which is false.
hi there, can't see this project on packagist.org, can you put it on. thnx~
Hi,
I tried running the unit tests under php7.2.12 and it fails in
Filterus\Filters\Object
PHP Fatal error: Cannot use 'Object' as class name as it is reserved
I am planning to refactor it and submit a pull request, but before I make any changes do you have any ideas on what class name should it be changed to?
Hi @ircmaxell
There's some PRs that have languished for a while in this repo. If you are willing to make me a committer, I'd be happy to go through the trivial docs ones, check their validity, and merge them down if they are OK.
$arr = [
'a' => 1,
'b' => 2,
'c' => 3,
4 => 4,
];
\Filterus\Filter::factory('array, keys:string')->filter($arr);
Doesn't remove 4
.
I noticed you've got is_scalar()
testing, then a cast to string, which takes place on 4
so that it's '4'
. Was just curious why you wouldn't filter int
out for alpha
when array, keys:int
will exclude 'a'
, 'b'
and 'c'
.
Shouldn't the string
test just be is_string()
? Maybe calls for the addition of array, keys:scalar
?
There isn't an implementation for validate
for emails, so although an empty string will be correctly filtered to null
, it will validate as true
, which is not desirable.
Repro code:
$filter = \Filterus\Filter::factory('email');
$result = $filter->filter('');
var_dump($result); // Returns NULL, correct
$ok = $filter->validate('');
echo $ok ? 'Passed' : 'Failed'; // Incorrectly returns Passed
A solution for now is to use a chain, like so:
$filter = \Filterus\Filter::chain('email', 'string,min:1');
$result = $filter->filter('');
var_dump($result); // Returns NULL, correct
$ok = $filter->validate('');
echo $ok ? 'Passed' : 'Failed'; // Correctly returns Failed
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.