public function testWriterMonad() { $ret = runWriter(ret(1)->bind(function ($_) { return ret(2); })->bind(function ($a) { return tell("a")->bind(function ($_) use($a) { return ret($a + 5); }); })); $this->assertEquals([7, "a"], $ret); }
<?php include dirname(__DIR__) . "/vendor/autoload.php"; use Domain\Domain; function tell($msg) { echo "{$msg} \n"; } function format($value, $result, $pass, $desired) { $desired = is_string($desired) ? $desired : json_encode($desired); $result = is_string($result) ? $result : json_encode($result); $value = is_string($value) ? $value : json_encode($value); $pass = $pass ? " " : "-"; return "\t{$pass} {$value} => {$result} ({$desired})"; } $tests = array('isLocal' => array('1.0.0.0' => false, 'localhost' => true, 'domain' => true, 'domain.tld' => false), 'isIP' => array('1.9.0.0' => true, 'domain' => false, 'https://domain.com' => false), 'Host' => array('localhost' => 'localhost', 'http://localhost:3000/main.html?id=e#app' => 'localhost', 'domain.tld' => 'domain.tld', 'www.domain.tld' => 'www.domain.tld', 'http://a.b.www.domain.tld' => 'a.b.www.domain.tld'), 'Sub' => array('1.0.0.0' => null, 'localhost' => null, 'https://localhost' => null, 'a.localhost' => null, 'https://a.localhost' => null, 'a.b.localhost' => 'a', 'https://a.b.localhost' => 'a', 'www.domain.tld' => 'www', 'http://www.domain.tld' => 'www', 'a.b.www.domain.tld' => 'www', 'ftp://a.b.www.domain.tld' => 'www'), 'TLD' => array('1.0.0.0' => null, 'http://1.0.0.0' => null, 'localhost' => null, 'http://localhost' => null, 'a.localhost' => 'localhost', 'https://a.localhost' => 'localhost', 'a.localhost.tld' => 'tld', 'ftp://a.localhost.tld' => 'tld', 'a.b.c.domain.tld' => 'tld', 'file://a.b.c.domain.tld/file.pdf' => 'tld', 'http://a.b.c.domain.co.uk' => 'co.uk'), 'Identity' => array('1.0.0.0' => null, 'http://1.0.0.0' => null, 'localhost' => null, 'https://localhost' => null, 'domain.tld' => 'domain.tld', 'https://domain.tld' => 'domain.tld', 'www.domain.tld' => 'domain.tld', 'http://www.domain.tld/main.html' => 'domain.tld', 'a.b.c.domain.tld' => 'domain.tld')); foreach ($tests as $class => $expectation) { tell("Domain::{$class}"); foreach ($expectation as $value => $desired) { $result = Domain::$class($value); $msg = format($value, $result, $result === $desired, $desired); tell($msg); } }
} // returning a result into a variable $s = add(111, 222); echo $s; echo "<br>"; // global varaible // scope inside of php $x = "out "; function tell() { global $x; echo $x; $x = "in "; } echo $x; tell(); echo $x; echo "<br>"; // constants define('NAME', 'This is a constant'); echo NAME; echo "<br>"; // math func echo rand(1, 1000); echo rand(1, 1000); echo rand(1, 1000); echo rand(1, 1000); echo "<br>"; $string = "this is the a sentence yo222ee"; echo strlen($string); echo "<br>";