public function plain_values() { $val = rand(); $rnd = md5(rand()); $value = V::val($val, $rnd); return array(array($value, $val, $rnd)); }
public function instantiate(NameSource $name_source) { $fmlt = $this->_formlet->instantiate($name_source); $b = $this->_transform_builder->apply(V::val($fmlt["builder"]))->get(); $c = $this->_transform_collector->apply(V::val($fmlt["collector"]))->get(); return array("builder" => $b, "collector" => $c, "name_source" => $fmlt["name_source"]); }
public function buildWithDict(RenderDict $dict) { $base = $this->_builder->buildWithDict($dict); $res = $this->_transformation->apply(V::val($dict))->apply(V::val($base))->get(); C::guardIsHTML($res); return $res; }
public function collect($inp) { $name = $this->name(); if (!array_key_exists($name, $inp)) { throw new MissingInputError($this->name()); } return V::val($inp[$name], $name); }
public function formlets() { $alwaysTrue = V::fn(function ($_) { return true; }); $pure = F::pure(V::val(42)); return array(array($pure->satisfies($alwaysTrue, "ERROR"))); }
public static function _empty() { // ToDo: Why does this not work? /*if (self::_emptyInst === null) { self::_emptyInst = new RenderDict(_val(0)); } return self::_emptyInst;*/ return new RenderDict(array(), V::val(0), true); }
public function testCollectCanBeReused() { $fn = L::collect(); $fn_a = $fn->apply(V::val(1))->apply(V::val(2))->apply(V::val(new Stop())); $fn_b = $fn->apply(V::val(3))->apply(V::val(4))->apply(V::val(new Stop())); $res_a = $fn_a->get(); $res_b = $fn_b->get(); $this->assertEquals($res_a, array(1, 2)); $this->assertEquals($res_b, array(3, 4)); }
function depth_first_searches() { $is_text = V::fn(function ($html) { return $html instanceof HTMLText; }); $get_text = V::fn(function ($html) { return $html->content(); }); return array(array(H::text("Hello World"), $is_text, $get_text, "Hello World"), array(H::nop(), $is_text, $get_text, null), array(H::tag("foo", array(), H::text("Hello World")), $is_text, $get_text, "Hello World"), array(H::tag("foo", array(), H::concat(H::text("Hello World"), H::text("Hello World"))), $is_text, $get_text, "Hello World"), array(H::tag("foo", array(), H::concat(H::tag("bar", array(), H::concat(H::text("Hello World"), H::text("Blub"))), H::text("Blaw"))), $is_text, $get_text, "Hello World")); }
public final function satisfies(FunctionValue $predicate, $error) { C::guardIsString($error); C::guardHasArity($predicate, 1); return $this->map(V::fn_w(function ($value) use($predicate, $error) { if (!$predicate->apply($value)->get()) { return V::error($error, $value->origin()); } return $value; })); }
/** * @dataProvider functions_and_args */ public function testFunctionResult($fun, $args) { $fn = V::fn($fun); $res1 = call_user_func_array($fun, $args); $tmp = $fn; for ($i = 0; $i < $fn->arity(); ++$i) { $tmp = $tmp->apply(V::val($args[$i])); } $res2 = $tmp->get(); $this->assertEquals($res1, $res2); }
/** * Make a depth first search. * Performs $transformation on every node that matches $predicate. If * $transformation returns null, search will go on, if it returns a * value, search will stop and return the value. */ public function depthFirst(FunctionValue $predicate, FunctionValue $transformation) { C::guardHasArity($predicate, 1); C::guardHasArity($transformation, 1); if ($predicate->apply(V::val($this))->get()) { $res = $transformation->apply(V::val($this))->get(); if ($res !== null) { return $res; } } return $this->goDepth($predicate, $transformation); }
public function testMappedTwice() { $d = new RenderDict(array("foo" => "bar"), V::val(0)); $n = NameSource::instantiate("test"); $f = F::text("foobar"); $rdict1 = null; $rhtml1 = null; $rdict2 = null; $rhtml2 = null; $transformation = V::fn(function ($dict, $html) use(&$rdict1, &$rhtml1) { $rdict1 = $dict; $rhtml1 = $html; return H::nop(); }); $transformation2 = V::fn(function ($dict, $html) use(&$rdict2, &$rhtml2) { $rdict2 = $dict; $rhtml2 = $html; return H::text("baz"); }); $f2 = $f->mapHTML($transformation)->mapHTML($transformation2); $i = $f2->instantiate($n); $r1 = $i["builder"]->build(); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\RenderDict", $rdict1); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\RenderDict", $rdict2); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLText", $rhtml1); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLNop", $rhtml2); $this->assertEquals($rhtml1->render(), "foobar"); $this->assertEquals($rhtml2->render(), ""); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLText", $r1); $this->assertEquals($r1->render(), "baz"); $r2 = $i["builder"]->buildWithDict($d); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\RenderDict", $rdict1); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\RenderDict", $rdict2); $this->assertEquals($d, $rdict1); $this->assertEquals($d, $rdict2); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLText", $rhtml1); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLNop", $rhtml2); $this->assertEquals($rhtml1->render(), "foobar"); $this->assertEquals($rhtml2->render(), ""); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLText", $r2); $this->assertEquals($r2->render(), "baz"); }
/** * One can get a dictionary out of an error that contains the error messages * from the error itself and all the other errors that led to it. */ public function testErrorToDict() { $a = V::error("a", "a"); $b = V::error("b", "b"); $c = V::error("c", "c"); $x1 = V::error("1", "x"); $x2 = V::error("2", "x"); $all = V::error("all", "all", array($a, $b, $c, $x1, $x2)); $dict = $all->toDict(); $this->assertArrayHasKey("a", $dict); $this->assertArrayHasKey("b", $dict); $this->assertArrayHasKey("c", $dict); $this->assertArrayHasKey("x", $dict); $this->assertArrayHasKey("all", $dict); $this->assertCount(5, $dict); $this->assertEquals($dict["a"], array("a")); $this->assertEquals($dict["b"], array("b")); $this->assertEquals($dict["c"], array("c")); $this->assertEquals($dict["x"], array("1", "2")); $this->assertEquals($dict["all"], array("all")); }
public function __construct($id, $action, $attrs, IFormlet $formlet) { if (!preg_match("#[a-zA-Z][a-zA-Z0-9_]+#", $id)) { throw new Exception("Form::__construct: '{$id}' can not be used as " . "id. Only use numbers and digits."); } C::guardIsFormlet($formlet); C::guardIsString($id); C::guardIsString($action); $attrs = Value::defaultTo($attrs, array()); C::guardEachAndKeys($attrs, "C::guardIsString", "C::guardIsString"); $this->_id = $id; $this->_input = null; $this->_result = null; $attrs["method"] = "post"; $attrs["action"] = $action; $formlet = $formlet->mapHTML(V::fn(function ($dict, $html) use($attrs) { return H::tag("form", $attrs, $html); })); $name_source = NameSource::instantiate($this->_id); $repr = $formlet->instantiate($name_source); $this->_builder = $repr["builder"]; $this->_collector = $repr["collector"]; }
public function formlets() { return array(array(F::pure(V::val(42)))); }
public function testMappedTwiceBuilder() { $d = new RenderDict(array("foo" => "bar"), V::val(0)); $b = new TextBuilder("foobar"); $rdict1 = null; $rhtml1 = null; $rdict2 = null; $rhtml2 = null; $transformation = V::fn(function ($dict, $html) use(&$rdict1, &$rhtml1) { $rdict1 = $dict; $rhtml1 = $html; return H::nop(); }); $transformation2 = V::fn(function ($dict, $html) use(&$rdict2, &$rhtml2) { $rdict2 = $dict; $rhtml2 = $html; return H::text("baz"); }); $b2 = $b->map($transformation)->map($transformation2); $r1 = $b2->build(); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\RenderDict", $rdict1); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\RenderDict", $rdict2); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLText", $rhtml1); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLNop", $rhtml2); $this->assertEquals($rhtml1->render(), "foobar"); $this->assertEquals($rhtml2->render(), ""); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLText", $r1); $this->assertEquals($r1->render(), "baz"); $r2 = $b2->buildWithDict($d); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\RenderDict", $rdict1); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\RenderDict", $rdict2); $this->assertEquals($d, $rdict1); $this->assertEquals($d, $rdict2); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLText", $rhtml1); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLNop", $rhtml2); $this->assertEquals($rhtml1->render(), "foobar"); $this->assertEquals($rhtml2->render(), ""); $this->assertInstanceOf("Lechimp\\Formlets\\Internal\\HTMLText", $r2); $this->assertEquals($r2->render(), "baz"); }
private function toValue($val, $origin) { if ($val instanceof Value) { return $val; } else { return V::val($val, $origin); } }
public static function isMultipleOf($s) { return V::fn(function ($value) use($s) { return $value % $s === 0; }); }
/** * One can apply an error value and gets an error back. * @dataProvider error_values */ public function testErrorAppliedIsError(Value $value, $reason, $origin) { $this->assertTrue($value->apply(V::val(1))->isError()); }
public static function id() { static $fn = null; if ($fn === null) { $fn = V::fn(function ($v) { return $v; }); } assert('$fn !== null'); return $fn; }
protected function alwaysThrows2() { return V::fn(function ($a, $b) { throw new TestException("test exception"); }); }
public function formlets() { $p = F::pure(V::val(42)); return array(array($p->cmb($p))); }
public function formlets() { return array(array(F::fieldset("Static: ", F::pure(V::val(42))))); }
public static function stop() { return V::val(new Internal\Stop()); }
public function collect($inp) { $wrapped = $this->_wrapper->apply(V::val($this->_collector))->apply(V::val($inp)); return $wrapped->force(); }
public function formlets_and_values() { $data = array(); $pure_val = F::pure(V::val(42)); $pure_fn = F::pure(F::id()); $formlets = array(array($pure_val, false), array($pure_fn, true), array(F::text("TEXT")->cmb($pure_val), false), array(F::text("TEXT")->cmb($pure_fn), true), array($pure_val->cmb(F::text("TEXT")), false), array($pure_fn->cmb(F::text("TEXT")), true), array($pure_fn->cmb(F::input("text")), false), array($pure_fn->cmb(F::textarea_raw()), false), array($pure_fn->cmb(F::text_input()), false), array($pure_fn->cmb(F::textarea()), false), array($pure_fn->cmb(F::checkbox()), false), array(F::submit("SUBMIT")->cmb($pure_val), false), array(F::submit("SUBMIT")->cmb($pure_fn), true), array($pure_val->cmb(F::submit("SUBMIT")), false), array($pure_fn->cmb(F::submit("SUBMIT")), true), array(F::email("*****@*****.**"), false), array(F::hidden("foo"), false), array(F::number(1, 0, 1000, 1, array(), "no int", "not in range", "step_length"), false), array(F::password("foo"), false), array(F::radio(array("100" => "100"), "100"), false), array(F::search("foo"), false), array(F::url("foo"), false), array(F::select(array("100" => "100")), false)); $functions = array(F::id()); $values = array(0, 1, 2); foreach ($formlets as $formlet) { foreach ($functions as $fn1) { foreach ($functions as $fn2) { foreach ($values as $value) { $data[] = array($formlet[0], $fn1, $fn2, V::val($value), $formlet[1]); } } } } return $data; }