Exemplo n.º 1
0
 /**
  * @param callable $callback
  *
  * @return Optional
  */
 public function ensure(callable $callback) : Optional
 {
     if ($callback($this->value)) {
         return $this;
     }
     return None::Instance();
 }
Exemplo n.º 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);
 }
Exemplo n.º 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.");
 }
Exemplo n.º 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);
     });
 }
Exemplo n.º 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);
 }
Exemplo n.º 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);
 }
Exemplo n.º 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()));
 }
Exemplo n.º 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;
 }
Exemplo n.º 9
0
    public function reject($value)
    {
        if ($this->value === $value) {
            return None::create();
        }

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