Ejemplo n.º 1
0
 public function formlets()
 {
     $alwaysTrue = V::fn(function ($_) {
         return true;
     });
     $pure = F::pure(V::val(42));
     return array(array($pure->satisfies($alwaysTrue, "ERROR")));
 }
Ejemplo n.º 2
0
 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"));
 }
 /**
  * @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);
 }
Ejemplo n.º 4
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;
     }));
 }
Ejemplo n.º 5
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");
 }
Ejemplo n.º 6
0
 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"];
 }
Ejemplo n.º 7
0
 public static function isMultipleOf($s)
 {
     return V::fn(function ($value) use($s) {
         return $value % $s === 0;
     });
 }
Ejemplo n.º 8
0
 /** 
  * Construct a function value from a closure or the name of an ordinary
  * function. One could specify the arity to use php-static functions with optional
  * arguments. An array of arguments to be inserted in the first arguments 
  * of the function could be passed optionally as well.
  * 
  * @param   Closure | string    $function
  * @param   null | integer      $arity
  * @param   mixed[]             $args
  * @return  IValue 
  */
 public static function fun($function, $arity = null, $args = null)
 {
     return V::fn($function, $arity, $args);
 }
Ejemplo n.º 9
0
 protected function alwaysThrows2()
 {
     return V::fn(function ($a, $b) {
         throw new TestException("test exception");
     });
 }
Ejemplo n.º 10
0
 public static function id()
 {
     static $fn = null;
     if ($fn === null) {
         $fn = V::fn(function ($v) {
             return $v;
         });
     }
     assert('$fn !== null');
     return $fn;
 }
Ejemplo n.º 11
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");
 }