/** * @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(); }
public function testTheWithMethodProxiesToCreate() { $this->assertInstanceOf('Monad\\FTry\\Success', FTry::with('foo')); $this->assertInstanceOf('Monad\\FTry\\Success', FTry::with(function () { return true; })); $this->assertInstanceOf('Monad\\FTry\\Success', FTry::with(new Identity('foo'))); $this->assertInstanceOf('Monad\\FTry\\Failure', FTry::with(new \Exception())); $this->assertInstanceOf('Monad\\FTry\\Failure', FTry::with(function () { throw new \Exception(); })); $this->assertInstanceOf('Monad\\FTry\\Failure', FTry::with(new Identity(function () { throw new \Exception(); }))); }
/** * 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(); }
/** * @param Nominal $nId * @param $exceptionMessage * * @return FTry */ protected function tryGetNode(Nominal $nId, $exceptionMessage) { return FTry::with(function () use($nId, $exceptionMessage) { $node = $this->findNode($nId); if (is_null($node)) { throw new AccountsException($exceptionMessage); } return $node; }); }
/** * Validation * * @param mixed $value * @return boolean True if value is valid else false */ protected function validate($value) { return Match::on(Option::create($this->validateISO($value), false))->Monad_Option_Some(function ($opt) use($value) { return Match::on(Option::create($opt->value() && $this->phpCheck, false))->Monad_Option_Some(function () use($value) { return Match::on(FTry::with(function () use($value) { new \DateTime($value); }))->Monad_FTry_Success(true)->Monad_FTry_Failure(function () { $this->messenger->clear()->add(new StringType(C::ERR_FAILED_PHP_CHECK)); return false; })->value(); })->Monad_Option_None(function () use($opt) { return $opt->value(); })->value(); })->Monad_Option_None(false)->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(); }
/** * @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(); }); }