Example #1
0
 /**
  * @param callable $callback
  *
  * @return Optional
  */
 public function ensure(callable $callback) : Optional
 {
     if ($callback($this->value)) {
         return $this;
     }
     return None::Instance();
 }
Example #2
0
 /**
  * @param $value
  * @param null $reject
  * @return Option
  */
 public static function wrap($value, $reject = null)
 {
     if ($value === $reject) {
         return None::instance();
     }
     return new Some($value);
 }
Example #3
0
 function filter($callable)
 {
     if (is_callable($callable)) {
         return $callable($this->get()) ? $this : None::instance();
     } elseif (is_array($callable)) {
         return in_array($this->get(), $callable) ? $this : None::instance();
     }
     throw new \InvalidArgumentException("Expected only callable or array type of argument.");
 }
Example #4
0
 /**
  * Creates a lazy-option with the given callback.
  *
  * This is also a helper constructor for lazy-consuming existing APIs where
  * the return value is not yet an option. By default, we treat ``null`` as
  * None case, and everything else as Some.
  *
  * @param callable $callback The callback to evaluate.
  * @param array $arguments
  * @param mixed $noneValue The value which should be considered "None"; null
  *                         by default.
  *
  * @return Option
  */
 public static function fromReturn($callback, array $arguments = array(), $noneValue = null)
 {
     return new LazyOption(function () use($callback, $arguments, $noneValue) {
         $return = call_user_func_array($callback, $arguments);
         if ($return === $noneValue) {
             return None::create();
         }
         return new Some($return);
     });
 }
Example #5
0
 protected final function dataTypeCheck($value, $variableName, $soft)
 {
     if (!is_numeric($value) || static::$strictNumberCheck && is_string($value)) {
         return static::handleTypeError($this->getTypeString(), $value, $variableName, $soft);
     }
     if (!$this->checkRange($value, $variableName, $soft)) {
         return None::getInstance();
     }
     return $this->subTypeCheck($value, $variableName, $soft);
 }
Example #6
0
 protected final function dataTypeCheck($value, $variableName, $soft)
 {
     if (!is_string($value)) {
         return static::handleTypeError($this->getTypeString(), $value, $variableName, $soft);
     }
     if ($this->validatorRegex) {
         if (!preg_match($this->validatorRegex, $value)) {
             if ($soft) {
                 return None::getInstance();
             }
             throw new TypeError("Value named: {$variableName} with data: \"{$value}\" does not conform to regex: {$this->validatorRegex}");
         }
     }
     return $this->additionalCheck($value, $variableName, $soft);
 }
Example #7
0
 /**
  * append() をテストします.
  * 以下を確認します.
  * 
  * - 任意のスカラー値および Node が追加できること
  * - null および None を指定した場合, 変化がない (追加されない) こと
  * - コンテナを追加した場合, 自身ではなくその子ノードが追加されること
  * 
  * @covers Peach\Markup\Container::append
  */
 public function testAppend()
 {
     $nList = new NodeList();
     $nList->append("foo");
     $nList->append("bar");
     $nList->append("baz");
     $test = $this->test;
     $obj = $this->object;
     $obj->append(null);
     $obj->append("TEXT");
     // (count: 1)
     $obj->append(new EmptyElement("test"));
     // (count: 2)
     $obj->append(None::getInstance());
     // added nothing (count: 2)
     $obj->append($nList);
     // added 3 nodes (count: 5)
     $obj->append(array("A", "B", array("C", "D")));
     // added 4 nodes (count: 9)
     $test->assertSame(9, count($obj->getChildNodes()));
 }
Example #8
0
 protected function dataTypeCheck($value, $variableName, $soft)
 {
     $isObject = is_object($value);
     $isArray = is_array($value);
     if (!($isObject || $isArray)) {
         if ($soft) {
             return None::getInstance();
         }
         throw new TypeError("Type must be object, type of value named: {$variableName} given: " . gettype($value) . " with data: " . static::safePrint($value));
     }
     //Set missing fields to null, let the child type check if it accepts it.
     foreach ($this->childrenType as $key => $element) {
         if ($isArray && !array_key_exists($key, $value)) {
             $value[$key] = null;
         } else {
             if ($isObject && !property_exists($value, $key)) {
                 $value->{$key} = null;
             }
         }
         //throw new TypeError("Required property: $key is missing from object: $variableName");
     }
     if ($this->childrenType !== null) {
         foreach ($value as $key => $element) {
             if (!isset($this->childrenType[$key])) {
                 throw new TypeError("Property {$key} in array: {$variableName} does not have a validator Type");
             }
             //$actualValue = $this->childrenType[$key]->check($element, "{$variableName}:{{$key}}");
             $actualValue = $this->childrenType[$key]->check($element, "{$variableName}:{$key}");
             if ($isArray) {
                 $value[$key] = $actualValue;
             } else {
                 $value->{$key} = $actualValue;
             }
         }
     }
     return $value;
 }
Example #9
0
    public function reject($value)
    {
        if ($this->value === $value) {
            return None::create();
        }

        return $this;
    }
Example #10
0
 /**
  * @param $method
  * @param $args
  * @return $this
  */
 public function call($method, ...$args)
 {
     return is_object($this->get()) && method_exists($this->get(), $method) ? Option::Some($this->get()->{$method}(...$args)) : None::instance();
 }
Example #11
0
 /**
  * Create a new file cache store instance.
  *
  * @param   array  $options
  * @return  void
  */
 public function __construct(array $options = array())
 {
     parent::__construct($options);
 }
Example #12
0
 /**
  * @inheritdoc
  */
 public function find(callable $hof)
 {
     return $hof($this->value, 0, $this) ? new Some($this->value) : None::getInstance();
 }
Example #13
0
/**
 * @return None
 */
function None()
{
    return None::getInstance();
}
Example #14
0
 public function testTryingAgain()
 {
     $backoff = new None();
     $this->assertFalse($backoff->shouldWeTryAgain(), 'Never try again with this strategy');
 }
Example #15
0
 /**
  * @inheritdoc
  * @return None
  */
 public function left()
 {
     return None::getInstance();
 }
Example #16
0
/**
 * @return None
 */
function none() : None
{
    return None::Instance();
}
Example #17
0
 /**
  * Context の handleNone() が呼び出されることを確認します.
  * 
  * @covers Peach\Markup\None::accept
  */
 public function testAccept()
 {
     $context = new TestContext();
     $this->object->accept($context);
     $this->assertSame("handleNone", $context->getResult());
 }
Example #18
0
 /**
  * @inheritdoc
  * @return Some|None
  */
 public function filterNot(callable $hof)
 {
     return !call_user_func($hof, $this->x, 0, $this) ? $this : None::getInstance();
 }
Example #19
0
 /**
  * @param $filePath
  * @return Option
  */
 public static function ofFile($filePath)
 {
     return file_exists($filePath) ? new Some($filePath) : None::getInstance();
 }
Example #20
0
 public function selectInstance($object)
 {
     return $this->get() instanceof $object ? $this : None::instance();
 }
Example #21
0
 /**
  * handleNone() のテストです.
  * 
  * @covers Peach\Markup\DebugContext::handleNone
  * @covers Peach\Markup\DebugContext::append
  */
 public function testHandleNone()
 {
     $expected = "None\r\n";
     $context = $this->object;
     $none = None::getInstance();
     $this->expectOutputString($expected);
     $context->handleNone($none);
     $this->assertSame($expected, $context->getResult());
 }
Example #22
0
 /**
  * @dataProvider serializerProvider
  */
 public function testIsSerialized($var)
 {
     $formatter = new None();
     $this->assertFalse($formatter->isSerialized($var));
 }
Example #23
0
 /**
  * @inheritdoc
  * @see QueryInterface::find
  * This is an constant on perceived nothings
  * @return None
  */
 public final function find(callable $hof)
 {
     return None::getInstance();
 }
Example #24
0
 /**
  * @return Option
  */
 public static function None()
 {
     return None::instance();
 }
Example #25
0
 /**
  * 結果に何も追加されないことを確認します。
  * 
  * @covers Peach\Markup\DefaultContext::handleNone
  */
 public function testHandleNone()
 {
     $none = None::getInstance();
     $obj = $this->object;
     $obj->handle($none);
     $this->assertSame("", $obj->getResult());
 }
Example #26
0
 /**
  * @inheritdoc
  * @return None
  */
 public function right()
 {
     return None::getInstance();
 }