Пример #1
0
 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"]);
 }
Пример #2
0
 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;
 }
Пример #3
0
 public static function collect()
 {
     $collector = V::fn_w(function ($array, Value $v) use(&$collector) {
         if (!($v->isError() || $v->isApplicable()) && $v->get() instanceof Stop) {
             // Postprocessing of the collected values.
             // We need to check weather there are errors in the collection
             // to be able to get errors appropriately.
             $errors = array();
             $vals = array_map(function ($v) use(&$errors) {
                 $v = $v->force();
                 if ($v->isError()) {
                     $errors[] = $v;
                     return $v;
                 }
                 if ($v->isApplicable()) {
                     return $v;
                 }
                 return $v->get();
             }, $array->get());
             if (count($errors) > 0) {
                 return V::error("Collection contains errors.", "_collect", $errors);
             }
             return V::val($vals, "collect");
         }
         $array = $array->get();
         $array[] = $v->force();
         return $collector->apply(V::val($array));
     });
     return $collector->apply(V::val(array()));
 }
Пример #4
0
 public function plain_values()
 {
     $val = rand();
     $rnd = md5(rand());
     $value = V::val($val, $rnd);
     return array(array($value, $val, $rnd));
 }
Пример #5
0
 public function compose_functions()
 {
     $times2 = V::fn(function ($v) {
         return $v * 2;
     });
     return array(array($times2, V::fn("intval", 1), V::val("42")), array(V::fn("count", 1), V::fn("explode", 2, array(" ")), V::val("x x x x")));
 }
Пример #6
0
 public function collect($inp)
 {
     $name = $this->name();
     if (!array_key_exists($name, $inp)) {
         throw new MissingInputError($this->name());
     }
     return V::val($inp[$name], $name);
 }
Пример #7
0
 public function formlets()
 {
     $alwaysTrue = V::fn(function ($_) {
         return true;
     });
     $pure = F::pure(V::val(42));
     return array(array($pure->satisfies($alwaysTrue, "ERROR")));
 }
Пример #8
0
 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);
 }
Пример #9
0
 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));
 }
 /**
  * @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);
 }
Пример #11
0
 /**
  * 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);
 }
Пример #12
0
 public final function map(FunctionValue $transformation)
 {
     return $this->wrap(V::fn(function ($collector, $inp) use($transformation) {
         $res = $collector->collect($inp)->force();
         if ($res->isError()) {
             return $res;
         }
         $res2 = $transformation->apply($res)->force();
         // If mapping was successfull, the underlying value should
         // be considered the origin of the produced value.
         if (!$res2->isError() && !$res2->isApplicable()) {
             return V::val($res2->get(), $res->origin());
         }
         return $res2;
     }));
 }
Пример #13
0
 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");
 }
Пример #14
0
 private function toValue($val, $origin)
 {
     if ($val instanceof Value) {
         return $val;
     } else {
         return V::val($val, $origin);
     }
 }
Пример #15
0
 public function collect($inp)
 {
     $wrapped = $this->_wrapper->apply(V::val($this->_collector))->apply(V::val($inp));
     return $wrapped->force();
 }
Пример #16
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());
 }
Пример #17
0
 public function formlets()
 {
     $p = F::pure(V::val(42));
     return array(array($p->cmb($p)));
 }
Пример #18
0
 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");
 }
Пример #19
0
 public function formlets()
 {
     return array(array(F::pure(V::val(42))));
 }
Пример #20
0
 public static function stop()
 {
     return V::val(new Internal\Stop());
 }
Пример #21
0
 public function formlets()
 {
     return array(array(F::fieldset("Static: ", F::pure(V::val(42)))));
 }
 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;
 }