示例#1
0
 public function testSetDefaultValue()
 {
     $nullable = null;
     rawr_set_default_value($nullable, 1);
     $this->assertNotNull($nullable);
     $this->assertEquals(rawr_get_primitive_type($nullable), rawr_integer);
     $this->assertEquals($nullable, 1);
 }
示例#2
0
 public function __construct($value = NULL)
 {
     rawr_set_default_value($value, 0);
     if (!is_numeric($value)) {
         $type = rawr_get_primitive_type($value);
         throw new \Exception("[rawr-core] [{$type}] is not a number");
     }
     $this->value = (int) $value;
 }
/**
 * Reduces a value to a primitive and applies type checking.
 * @author Marcelo Camargo
 * @param mixed $op
 * @param string $type
 * @return mixed
 */
function rawr_reduce($op, $type)
{
    $op_type = rawr_get_primitive_type($op);
    switch ($op_type) {
        case rawr_object:
        case rawr_callable:
        case rawr_integer:
            // When we want any object
            if ($type === rawr_object) {
                return $op;
            }
            // When it is possibly a rawr-wrapped value
            if (method_exists($op, "value") && $op instanceof \Rawr\DataType\BaseType) {
                $class_prefix = "Rawr\\DataType\\";
                $unwrapped_value = $op->value();
                $op_class = get_class($op);
                // Verify if we can cast it to a primitive value *with type assertion*
                switch ($op_class) {
                    case $class_prefix . "Bool" && $type === rawr_boolean:
                    case $class_prefix . "Action" && $type === rawr_callable:
                    case $class_prefix . "Int" && $type === rawr_integer:
                        break;
                    default:
                        throw new Exception("[rawr-core] Cannot cast [{$op_class}] to primitive [{$type}]");
                }
                return $unwrapped_value;
            } else {
                if (is_callable($op) && $type === rawr_callable) {
                    return $op;
                }
                throw new Exception("[rawr-core] Trying to extract value from a non-rawr type");
            }
            break;
        case "null":
            throw new Exception("[rawr-core] Unable to reduce null value");
        default:
            if ($op_type === $type) {
                return $op;
            } else {
                throw new Exception("[rawr-core] Cannot cast [{$op_type}] to [{$type}]");
            }
    }
}
示例#4
0
 /**
  * Magic method called when we use this object as a function.
  * it applies partialization on the function, where, if the
  * number of received arguments is lesser than the number of
  * the declared function, we'd return a new action pre-filled
  * with the passed arguments.
  * @author Marcelo Camargo
  * @return mixed
  */
 public function __invoke()
 {
     $start_parameters = $this->args;
     $rest_parameters = func_get_args();
     $total_parameters = sizeof($start_parameters) + sizeof($rest_parameters);
     $remaining_size = $this->length - $total_parameters;
     // For internal use only
     $this->remaining_size =& $remaining_size;
     $all_params = array_merge($start_parameters, $rest_parameters);
     if ($remaining_size <= 0) {
         $result = call_user_func_array($this->value, $all_params);
         return rawr_from_primitive($result, rawr_get_primitive_type($result));
     }
     $action = new Action($this->value);
     call_user_func_array([$action, 'setArgs'], $all_params);
     return $action;
 }