Esempio n. 1
0
 /**
  * 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();
 }
Esempio n. 2
0
 /**
  * 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();
 }
Esempio n. 3
0
 /**
  * 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();
 }
Esempio n. 4
0
 /**
  * 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();
 }
Esempio n. 5
0
 /**
  * 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();
 }
Esempio n. 6
0
 /**
  * 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();
 }
Esempio n. 7
0
 /**
  * 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();
 }
Esempio n. 8
0
 /**
  * Delete an account
  *
  * @param Nominal $nId Id of account
  *
  * @return $this
  * @throws AccountsException
  */
 public function delAccount(Nominal $nId)
 {
     Assembler::create()->accnt(function () use($nId) {
         return $this->tryGetNode($nId, self::ERR_INVALAC)->pass()->flatten();
     })->account(function ($accnt) {
         return FTry::with(function () use($accnt) {
             $account = $accnt->getValue();
             if ($account->getBalance()->get() !== 0) {
                 throw new AccountsException(self::ERR_NODELETE);
             }
             return $account;
         })->pass()->flatten();
     })->transact(function ($account) {
         Match::on(Option::create(($account->getType()->getValue() & AccountType::DR) == AccountType::DR, false))->Monad_Option_Some($account->debit($account->getDebit()->negate()))->Monad_Option_None($account->credit($account->getCredit()->negate()));
     })->removeChild(function ($accnt) {
         $accnt->getParent()->removeChild($accnt);
     })->assemble();
     return $this;
 }
Esempio n. 9
0
 /**
  * @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 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();
 }
 */
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"
Esempio n. 12
0
 public function testYouCanNestMatches()
 {
     $this->assertEquals('foo', $this->nestedMatcher('foo')->value());
     $this->assertEquals('bar', $this->nestedMatcher(Option::create('bar'))->value());
     try {
         $this->nestedMatcher(Option::create());
         $this->fail('Expected an Exception but got none');
     } catch (\Exception $e) {
         $this->assertTrue(true);
     }
     $this->assertEquals('foobar', $this->nestedMatcher(Identity::create('foo'))->value());
     //expecting match on any() as integer won't be matched
     $this->assertEquals('any', $this->nestedMatcher(2)->value());
 }
Esempio n. 13
0
 /**
  * Return Some or None as a result of bind
  *
  * @param \Closure $function
  * @param array $args
  * @param mixed $noneValue Optional value to test for None
  *
  * @return Some|None
  */
 public function bind(\Closure $function, array $args = [], $noneValue = null)
 {
     return Option::create($this->callFunction($function, $this->value, $args), $noneValue);
 }
Esempio n. 14
0
 /**
  * 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();
 }
Esempio n. 15
0
 /**
  * @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();
     });
 }
Esempio n. 16
0
 public function testGetOrElseWillReturnElseValueIfOptionIsANone()
 {
     $sut = Option::create(true, true);
     $this->assertFalse($sut->getOrElse(false));
 }