Skip to content
This repository has been archived by the owner on Nov 21, 2017. It is now read-only.

Commit

Permalink
Updating Typhoon's own validators.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Jan 15, 2013
1 parent f6f8872 commit e1b22d6
Show file tree
Hide file tree
Showing 52 changed files with 2,477 additions and 3,204 deletions.
12 changes: 6 additions & 6 deletions src-typhoon/Eloquent/Typhoon/TypeCheck/AbstractValidator.php
@@ -1,21 +1,21 @@
<?php
namespace Eloquent\Typhoon\TypeCheck;


abstract class AbstractValidator
{
public function __construct()
{
($this->reflector = (new \ReflectionObject($this)));
$this->reflector = new \ReflectionObject($this);
}

public function __call($name, array $arguments)
{
($validatorMethodName = \sprintf('validate%s', \ucfirst(\ltrim($name, '_'))));
if ((!$this->reflector->hasMethod($validatorMethodName)))
{
throw (new \BadMethodCallException(\sprintf('Call to undefined method %s::%s().', __CLASS__, $name)));
$validatorMethodName = \sprintf('validate%s', \ucfirst(\ltrim($name, '_')));
if (!$this->reflector->hasMethod($validatorMethodName)) {
throw new \BadMethodCallException(\sprintf('Call to undefined method %s::%s().', __CLASS__, $name));
}
return $this->reflector->getMethod($validatorMethodName)->invokeArgs($this, $arguments);
}

private $reflector;
}
2 changes: 1 addition & 1 deletion src-typhoon/Eloquent/Typhoon/TypeCheck/DummyValidator.php
@@ -1,10 +1,10 @@
<?php
namespace Eloquent\Typhoon\TypeCheck;


class DummyValidator extends AbstractValidator
{
public function __call($name, array $arguments)
{
}

}
@@ -1,14 +1,13 @@
<?php
namespace Eloquent\Typhoon\TypeCheck\Exception;


final class MissingArgumentException extends UnexpectedInputException
{
public function __construct($parameterName, $index, $expectedType, \Exception $previous = null)
{
($this->parameterName = $parameterName);
($this->index = $index);
($this->expectedType = $expectedType);
$this->parameterName = $parameterName;
$this->index = $index;
$this->expectedType = $expectedType;
parent::__construct(
\sprintf(
'Missing argument for parameter \'%s\' at index %d. Expected \'%s\'.',
Expand All @@ -19,18 +18,22 @@ public function __construct($parameterName, $index, $expectedType, \Exception $p
$previous
);
}

public function parameterName()
{
return $this->parameterName;
}

public function index()
{
return $this->index;
}

public function expectedType()
{
return $this->expectedType;
}

private $parameterName;
private $index;
private $expectedType;
Expand Down
@@ -1,37 +1,40 @@
<?php
namespace Eloquent\Typhoon\TypeCheck\Exception;


final class UnexpectedArgumentException extends UnexpectedInputException
{
public function __construct($index, $value, \Exception $previous = null, \Eloquent\Typhoon\TypeCheck\TypeInspector $typeInspector = null)
{
if ((null === $typeInspector))
{
($typeInspector = (new \Eloquent\Typhoon\TypeCheck\TypeInspector()));
if (null === $typeInspector) {
$typeInspector = new \Eloquent\Typhoon\TypeCheck\TypeInspector();
}
($this->index = $index);
($this->value = $value);
($this->typeInspector = $typeInspector);
($this->unexpectedType = $typeInspector->type($this->value));
$this->index = $index;
$this->value = $value;
$this->typeInspector = $typeInspector;
$this->unexpectedType = $typeInspector->type($this->value);
parent::__construct(\sprintf('Unexpected argument of type \'%s\' at index %d.', $this->unexpectedType, $index), $previous);
}

public function index()
{
return $this->index;
}

public function value()
{
return $this->value;
}

public function typeInspector()
{
return $this->typeInspector;
}

public function unexpectedType()
{
return $this->unexpectedType;
}

private $index;
private $value;
private $typeInspector;
Expand Down
@@ -1,21 +1,19 @@
<?php
namespace Eloquent\Typhoon\TypeCheck\Exception;


final class UnexpectedArgumentValueException extends UnexpectedInputException
{
public function __construct($parameterName, $index, $value, $expectedType, \Exception $previous = null, \Eloquent\Typhoon\TypeCheck\TypeInspector $typeInspector = null)
{
if ((null === $typeInspector))
{
($typeInspector = (new \Eloquent\Typhoon\TypeCheck\TypeInspector()));
if (null === $typeInspector) {
$typeInspector = new \Eloquent\Typhoon\TypeCheck\TypeInspector();
}
($this->parameterName = $parameterName);
($this->index = $index);
($this->value = $value);
($this->expectedType = $expectedType);
($this->typeInspector = $typeInspector);
($this->unexpectedType = $typeInspector->type($this->value));
$this->parameterName = $parameterName;
$this->index = $index;
$this->value = $value;
$this->expectedType = $expectedType;
$this->typeInspector = $typeInspector;
$this->unexpectedType = $typeInspector->type($this->value);
parent::__construct(
\sprintf(
'Unexpected argument of type \'%s\' for parameter \'%s\' at index %d. Expected \'%s\'.',
Expand All @@ -27,30 +25,37 @@ public function __construct($parameterName, $index, $value, $expectedType, \Exce
$previous
);
}

public function parameterName()
{
return $this->parameterName;
}

public function index()
{
return $this->index;
}

public function value()
{
return $this->value;
}

public function expectedType()
{
return $this->expectedType;
}

public function typeInspector()
{
return $this->typeInspector;
}

public function unexpectedType()
{
return $this->unexpectedType;
}

private $parameterName;
private $index;
private $value;
Expand Down
@@ -1,11 +1,11 @@
<?php
namespace Eloquent\Typhoon\TypeCheck\Exception;


abstract class UnexpectedInputException extends \InvalidArgumentException
{
public function __construct($message, \Exception $previous = null)
{
parent::__construct($message, 0, $previous);
}

}
45 changes: 23 additions & 22 deletions src-typhoon/Eloquent/Typhoon/TypeCheck/TypeCheck.php
@@ -1,61 +1,62 @@
<?php
namespace Eloquent\Typhoon\TypeCheck;


abstract class TypeCheck
{
public static function get($className, array $arguments = null)
{
if (static::$dummyMode)
{
return (new DummyValidator());
if (static::$dummyMode) {
return new DummyValidator();
}
if ((!\array_key_exists($className, static::$instances)))
{
if (!\array_key_exists($className, static::$instances)) {
static::install($className, static::createValidator($className));
}
($validator = static::$instances[$className]);
if ((null !== $arguments))
{
$validator = static::$instances[$className];
if (null !== $arguments) {
$validator->validateConstruct($arguments);
}
return $validator;
}

public static function install($className, $validator)
{
(static::$instances[$className] = $validator);
static::$instances[$className] = $validator;
}

public static function setRuntimeGeneration($runtimeGeneration)
{
(static::$runtimeGeneration = $runtimeGeneration);
static::$runtimeGeneration = $runtimeGeneration;
}

public static function runtimeGeneration()
{
return static::$runtimeGeneration;
}

protected static function createValidator($className)
{
($validatorClassName = ('Eloquent\\Typhoon\\TypeCheck\\Validator\\' . $className . 'TypeCheck'));
if ((static::runtimeGeneration() && (!\class_exists($validatorClassName))))
{
(static::$dummyMode = true);
$validatorClassName = 'Eloquent\\Typhoon\\TypeCheck\\Validator\\' . $className . 'TypeCheck';
if (static::runtimeGeneration() && !\class_exists($validatorClassName)) {
static::$dummyMode = true;
static::defineValidator($className);
(static::$dummyMode = false);
static::$dummyMode = false;
}
return (new $validatorClassName);
return new $validatorClassName;
}

protected static function defineValidator($className, \Eloquent\Typhoon\Generator\ValidatorClassGenerator $classGenerator = null)
{
if ((null === $classGenerator))
{
($classGenerator = (new \Eloquent\Typhoon\Generator\ValidatorClassGenerator));
if (null === $classGenerator) {
$classGenerator = new \Eloquent\Typhoon\Generator\ValidatorClassGenerator;
}
eval(('?>' . $classGenerator->generateFromClass(static::configuration(), (new \ReflectionClass($className)))));
eval('?>' . $classGenerator->generateFromClass(static::configuration(), new \ReflectionClass($className)));
}

protected static function configuration()
{
return (new \Eloquent\Typhoon\Configuration\RuntimeConfiguration('Eloquent\\Typhoon\\TypeCheck', false));
return new \Eloquent\Typhoon\Configuration\RuntimeConfiguration('Eloquent\\Typhoon\\TypeCheck', false);
}

private static $instances = array();
private static $dummyMode = false;
private static $runtimeGeneration = false;
Expand Down

0 comments on commit e1b22d6

Please sign in to comment.