public function asFormlet($values) { $this->formlet_collected = F::pure(L::collect()); foreach ($values as $value) { $this->formlet_collected = $this->formlet_collected->cmb(F::pure(V::val($value))); } $this->formlet_collected = $this->formlet_collected->cmb(F::pure(V::val(new Stop()))); $ns = NameSource::instantiate("test"); $repr = $this->formlet_collected->instantiate($ns); $this->formlet_result = $repr["collector"]->collect(array()); }
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"); }
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 instantiate(NameSource $name_source) { $res = $name_source->getNameAndNext(); return array("builder" => new TagBuilder("textarea", $this, $res["name"]), "collector" => new AnyCollector($res["name"]), "name_source" => $res["name_source"]); }
protected function assertFormletsEqual(Formlet $a, Formlet $b, $value, $contains_applicable) { // Two formlets are considered equal, when their observable output // is equal (that is like extensional equality?) $ns_a = NameSource::instantiate("test"); $ns_b = NameSource::instantiate("test"); $ns = NameSource::instantiate("test"); $repr_a = $a->instantiate($ns_a); $repr_b = $b->instantiate($ns_b); $name_and_ns = $ns->getNameAndNext(); $inp = array($name_and_ns["name"] => "100"); $val_a = $repr_a["collector"]->collect($inp); $val_b = $repr_b["collector"]->collect($inp); // This will only work if equal works as expected on the result, that // is the thing checked really is equality and not identity. if (!$contains_applicable) { if ($val_a->isError()) { print_r($val_a->toDict()); } $this->assertEquals($val_a->get(), $val_b->get()); } else { $this->assertEquals($val_a->apply($value)->get(), $val_b->apply($value)->get()); } $dict_a = new RenderDict($inp, $val_a); $dict_b = new RenderDict($inp, $val_b); $rendered_a = $repr_a["builder"]->build()->render(); $rendered_b = $repr_b["builder"]->build()->render(); $this->assertEquals($rendered_a, $rendered_b); $rendered_a2 = $repr_a["builder"]->buildWithDict($dict_a)->render(); $rendered_b2 = $repr_b["builder"]->buildWithDict($dict_b)->render(); $this->assertEquals($rendered_a2, $rendered_b2); }