/** * Split date by its parts * * @param string $value ISO datetime string * @return array[date, time, zone, timePartFound] */ public function splitDate($value) { $parts = Match::on($this->laxTime)->test(true, function () use($value) { $parts = explode(' ', $value); //join back zone part if found if (count($parts) == 3) { $parts[1] .= ' ' . $parts[2]; unset($parts[2]); } return $parts; })->test(false, function () use($value) { return explode('t', $value); })->value(); //guard if (count($parts) == 0 || count($parts) > 2 || count($parts) == 1 && empty($parts[0])) { return array(null, null, null, $this->timepartFound, $this->zonepartFound); } if (count($parts) == 1) { //we have a date only return array($parts[0], null, null, $this->timepartFound, $this->zonepartFound); } if (count($parts) == 2) { $this->timepartFound = true; //we have a date and something else list($time, $zone) = $this->splitTime($parts[1]); if (!is_null($zone)) { return array($parts[0], $time, $zone, $this->timepartFound, $this->zonepartFound); } elseif (!is_null($time)) { return array($parts[0], $time, null, $this->timepartFound, $this->zonepartFound); } return array($parts[0], null, null, $this->timepartFound, $this->zonepartFound); } return array($parts[0], null, null, $this->timepartFound, $this->zonepartFound); }
/** * Do the validation * * @param mixed $value * @return boolean */ protected function validate($value) { return Match::on(Option::create(preg_match(self::REGEX_DOUBLE, $value), 0))->Monad_Option_Some(true)->Monad_Option_None(function () { $this->messenger->add(new StringType(self::ERR_MSG)); return false; })->value(); }
/** * Do the validation * * @param mixed $value * @return boolean */ protected function validate($value) { return Match::on(Option::create(in_array($value, $this->enum), false))->Monad_Option_Some(true)->Monad_Option_None(function () { $this->messenger->add(new StringType(self::ERR_MSG)); return false; })->value(); }
/** * Validate * * @param mixed $value * @return boolean */ protected function validate($value) { $f = $this->function; return Match::on(Option::create((bool) $f($value, $this->messenger), false))->Monad_Option_Some(true)->Monad_Option_None(function () { $this->messenger->add($this->msg); return false; })->value(); }
/** * @param AccountType $type * @return FTry */ protected function checkType(AccountType $type) { return Match::on($type->getValue())->test(AccountType::CR, function () { return FTry::with(AccountType::CR()); })->test(AccountType::DR, function () { return FTry::with(AccountType::DR()); })->any(function () { return FTry::with(new AccountsException(self::ERR_NOTYPE)); })->value(); }
/** * Do the validation * * @param mixed $value * @return boolean */ protected function validate($value) { return Match::on(Option::create($this->validator->isValid($value), false))->Monad_Option_Some(true)->Monad_Option_None(function () { $msgs = $this->validator->getMessages(); array_walk($msgs, function ($msg) { $this->messenger->add(new StringType($msg)); }); return false; })->value(); }
/** * Do the validation * * @param mixed $value * @return boolean */ protected function validate($value) { return Match::on(Option::create(is_array($value), false))->Monad_Option_Some(function () use($value) { return Match::on(Option::create($this->checkForEmpty && empty($value), false))->Monad_Option_Some(function () { $this->messenger->add(new StringType(self::ERR_ARRAY_EMPTY)); return false; })->Monad_Option_None(true)->value(); })->Monad_Option_None(function () { $this->messenger->add(new StringType(self::ERR_INVALID)); return false; })->value(); }
/** * @param Assembler $assembler * @return \Closure */ public function giveMe(Assembler $assembler) { return $assembler->colour(Match::on(mt_rand(1, 2))->test(1, function () { return function () { return new PaintColour('red'); }; })->test(2, function () { return function () { return new PaintColour('black'); }; })->value()); }
/** * Do the validation * * @param mixed $ip IP Address to check - if null, then use current IP of requestor * * @return boolean */ protected function validate($ip = null) { $ip = empty($ip) ? IpUtil::getUserIp() : $ip; return Match::on(FTry::with(function () use($ip) { return array_reduce($this->netmasks, function (&$result, $cidr) use($ip) { return $result || IpUtil::cidrMatch($ip, $cidr); }, false); }))->Monad_FTry_Success(function ($value) { return Match::on(Option::create($value->flatten(), false))->Monad_Option_Some(true)->Monad_Option_None(function () { $this->messenger->add(new StringType(self::ERR_MSG1)); return false; })->value(); })->Monad_FTry_Failure(function ($e) { return Match::on(Option::create(strpos($e->value()->getMessage(), 'cidr'), false))->Monad_Option_Some(function () { $this->messenger->add(new StringType(self::ERR_MSG2)); return false; })->Monad_Option_None(function () { $this->messenger->add(new StringType(self::ERR_MSG1)); return false; })->value(); })->value(); }
*/ require_once '../vendor/autoload.php'; use Chippyash\Validation\Pattern\HasTypeMap; use Chippyash\Validation\Pattern\Repeater; use Chippyash\Validation\Common\ISO8601DateString; use Chippyash\Validation\Common\Email; use Chippyash\Validation\Common\UKPostCode; use Chippyash\Validation\Common\Lambda; use Chippyash\Validation\Messenger; use Chippyash\Validation\ValidationProcessor; use Monad\Match; use Monad\Option; use Chippyash\Type\Number\IntType; $requiredValidator = new HasTypeMap(['a' => new ISO8601DateString(), 'b' => 'boolean', 'c' => new Repeater(new HasTypeMap(['d' => 'string', 'e' => new UKPostCode()]), null, new IntType(4)), 'f' => new Repeater(new Email())]); $optionalValidator = new Lambda(function ($value, Messenger $messenger) { return Match::on(\Monad\Option::create(isset($value->g), false))->Monad_Option_Some(function () use($value) { return $value->g === 'foobar'; })->Monad_Option_None(true)->value(); }); $json1 = <<<EOT { "a": "2015-12-01", "b": false, "c": [ { "d": "fred", "e": "NN10 6HB" }, { "d": "jim", "e": "EC1V 7DA"
/** * @param array $value * * @return FTry */ protected function setTypeFromValue(array $value) { //required to be defined as a var so it can be called in next statement $basicTest = function () use($value) { if (count($value) > 0) { return array_values($value)[0]; } return null; }; //@var Option //firstValue is used twice below $firstValue = Option::create($basicTest()); //@var Option //NB - this separate declaration is not needed, but is provided only to // allow some separation between what can become a complex match pattern $type = Match::on($firstValue)->Monad_Option_Some(function ($option) { return Option::create(gettype($option->value())); })->Monad_Option_None(function () { return new None(); })->value(); //@var Option //MatchLegalType requires to be defined separately as it is used twice //in the next statement $matchLegalType = FTry::with(Match::on($type)->Monad_Option_None()->Monad_Option_Some(function ($v) use($firstValue) { Match::on($v->value())->test('object', function ($v) use($firstValue) { $this->setType(get_class($firstValue->value())); return new Some($v); })->test('string', function ($v) { $this->setType($v); return new Some($v); })->test('integer', function ($v) { $this->setType($v); return new Some($v); })->test('double', function ($v) { $this->setType($v); return new Some($v); })->test('boolean', function ($v) { $this->setType($v); return new Some($v); })->test('resource', function ($v) { $this->setType($v); return new Some($v); })->any(function () { return new None(); }); })->any(function () { return new None(); })); return FTry::with(function () use($matchLegalType) { return $matchLegalType->value(); }); }
/** * Get a Link entry's nominal id using its name * * @param StringType $name * @return Nominal|null */ public function getLinkId(StringType $name) { return Match::create(Option::create($this->getLink($name)))->Monad_Option_Some(function ($val) { return $val->value()->getId(); })->Monad_Option_None(function () { return null; })->value(); }
/** * Get Id of parent for an account * * @param Nominal $nId * @return null|IntType * * @throws AccountsException */ public function getParentId(Nominal $nId) { return Match::on(Match::on($this->tryGetNode($nId, self::ERR_INVALAC))->Monad_FTry_Success(function ($node) { return Match::on($node->flatten()->getParent()); })->value()->pass()->value())->Tree_Node_Node(function ($node) { $v = $node->getValue(); return is_null($v) ? null : $v->getId(); })->value(); }
/** * @param $date * @param $time * @param $zone * @return bool */ private function matchOnAvailableParts($date, $time, $zone) { $matcher = new MatchDate($this->format, $this->formatRegex, $this->messenger); return Match::on(Option::create($this->timepartFound, false))->Monad_Option_Some(function () use($matcher, $date, $time, $zone) { return Match::on(Option::create($this->zonepartFound, false))->Monad_Option_Some(function () use($matcher, $date, $time, $zone) { //date, time and zone return Match::on(Option::create($matcher->matchDateAndTimeAndZone($date, $time, $zone), false))->Monad_Option_Some(true)->Monad_Option_None(function () { $this->messenger->clear()->add(new StringType(C::ERR_INVALID)); return false; })->value(); })->Monad_Option_None(function () use($matcher, $date, $time) { //date and time return Match::on(Option::create($matcher->matchDateAndTime($date, $time), false))->Monad_Option_Some(true)->Monad_Option_None(function () { $this->messenger->clear()->add(new StringType(C::ERR_INVALID)); return false; })->value(); })->value(); })->Monad_Option_None(function () use($matcher, $date) { //date only return Match::on(Option::create($matcher->matchDate($date), false))->Monad_Option_Some(true)->Monad_Option_None(function () { $this->messenger->clear()->add(new StringType(C::ERR_INVALID)); return false; })->value(); })->value(); }
/** * Get parent id as an Option * * @return Option */ protected function optGetParentId() { return Match::on(FTry::with(function () { return $this->chart->getParentId($this->id); }))->Monad_FTry_Success(function ($id) { return Option::create($id->flatten()); })->Monad_FTry_Failure(function () { return Option::create(null); })->value(); }
/** * Get amount if the account is balanced * * @return Currency * @throw AccountsException */ public function getAmount() { return Match::create(Option::create($this->entries->checkBalance(), false))->Monad_Option_Some(function () { $tot = 0; foreach ($this->entries as $entry) { $tot += $entry->getAmount()->get(); } //use last entry to grab currency code from /** @noinspection PhpUndefinedVariableInspection */ return CFactory::create($entry->getAmount()->getCode()->get())->set($tot / 2); })->Monad_Option_None(function () { throw new AccountsException('No amount for unbalanced transaction'); })->value(); }
public function testYouCanTestForEquality() { $test = Match::on('foo')->test('foo')->value(); $this->assertEquals('foo', $test); $test = Match::on('foo')->test('bar')->value(); $this->assertEquals('foo', $test); $test = Match::on('foo')->test('foo', function ($v) { return new Some($v); })->flatten(); $this->assertEquals('foo', $test); $test = Match::on('bar')->test('foo', function ($v) { return new Some($v); })->any(function () { return new None(); })->value(); $this->assertInstanceOf('Monad\\Option\\None', $test); }