public function testUser() { $filter = Filter::map(["[user]" => ["login,password,confirm" => "Trim,\r\n\t\v", "password" => "Compare,===,,confirm"], "[profile]" => ["first_name,last_name,middle_name" => "Trim;Lower;FirstUpper"]]); $context = $filter->run(["user" => ["login" => "\tlogin\r\n", "password" => "pass\r\n", "confirm" => "pass"], "profile" => ["first_name" => "иван", "last_name" => "иванов ", "middle_name" => " иванович "]]); $this->assertCount(0, $context->errors); $this->assertEquals("Иван", $context->data["profile"]["first_name"]); $this->assertEquals("Иванов", $context->data["profile"]["last_name"]); $this->assertEquals("Иванович", $context->data["profile"]["middle_name"]); $this->assertEquals("login", $context->data["user"]["login"]); $this->assertEquals("pass", $context->data["user"]["password"]); }
public function testBoolean() { $filter = Filter::map([Boolean::create()]); $this->assertCount(0, $filter->run("yes")->errors); $this->assertCount(0, $filter->run("true")->errors); $this->assertCount(0, $filter->run("1")->errors); $this->assertCount(0, $filter->run("on")->errors); $this->assertCount(0, $filter->run("no")->errors); $this->assertCount(0, $filter->run("false")->errors); $this->assertCount(0, $filter->run("0")->errors); $this->assertCount(0, $filter->run("off")->errors); $this->assertCount(0, $filter->run("")->errors); $this->assertCount(1, $filter->run("oops")->errors); }
public function testToBooleanFilter() { $filter = Filter::map(["to bool"]); $this->assertSame(true, $filter->run("yes")->data[0]); $this->assertSame(true, $filter->run("true")->data[0]); $this->assertSame(true, $filter->run("1")->data[0]); $this->assertSame(true, $filter->run("on")->data[0]); $this->assertSame(false, $filter->run("no")->data[0]); $this->assertSame(false, $filter->run("false")->data[0]); $this->assertSame(false, $filter->run("0")->data[0]); $this->assertSame(false, $filter->run("off")->data[0]); $this->assertSame(false, $filter->run("")->data[0]); $this->assertSame(false, $filter->run("oops")->data[0]); }
/** * * @param Closure $op * @param mixed $expected * @param string $with * @param mixed $filter */ public function __construct($op = "?", $expected = null, $with = null, $filter = null) { if ($op instanceof Closure) { $this->op = $op->bindTo($this); } else { $this->op = $op; } $this->expected = $expected; $this->with = $with; if ($filter instanceof Filter) { $this->filter = $filter; } elseif (is_string($filter)) { $this->filter = Filter::map($filter); } elseif (is_array($filter)) { $this->filter = Filter::map($filter); } else { $this->filter = null; } }