Skip to content

Commit

Permalink
refactoring the validator component
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannis committed Jun 7, 2016
1 parent 70cb448 commit 6469981
Show file tree
Hide file tree
Showing 11 changed files with 976 additions and 74 deletions.
5 changes: 5 additions & 0 deletions Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
*/
class Assert extends Constraints
{
/**
* @var string
*/
const DEFAULT_GROUP = 'Default';

/**
* Create a constraints instance.
*
Expand Down
128 changes: 128 additions & 0 deletions Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Validator\Mapping;

use Cubiche\Core\Validator\Assert;
use Metadata\ClassMetadata as BaseClassMetadata;

/**
* ClassMetadata class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class ClassMetadata extends BaseClassMetadata
{
/**
* @var string
*/
public $defaultGroup;

/**
* @return string
*/
public function getClassName()
{
return $this->name;
}

/**
* @param $property
*
* @return PropertyMetadata|null
*/
protected function getPropertyMetadata($property)
{
return isset($this->propertyMetadata[$property]) ? $this->propertyMetadata[$property] : null;
}

/**
* @param string $property
* @param Assert $constraint
* @param string $group
*/
public function addPropertyConstraint($property, Assert $constraint, $group = null)
{
$propertyMetadata = $this->getPropertyMetadata($property);
if ($propertyMetadata === null) {
$propertyMetadata = new PropertyMetadata($this->getClassName(), $property);
$propertyMetadata->defaultGroup = $this->defaultGroup;
}

$propertyMetadata->addConstraint($constraint, $group);

$this->addPropertyMetadata($propertyMetadata);
}

/**
* @param string $property
* @param array $constraints
* @param string $group
*/
public function addPropertyConstraints($property, array $constraints, $group = null)
{
foreach ($constraints as $constraint) {
$this->addPropertyConstraint($property, $constraint, $group);
}
}

/**
* @return PropertyMetadata[]
*/
public function getPropertiesMetadata()
{
return $this->propertyMetadata;
}

/**
* Merges the constraints of the given metadata into this object.
*
* @param ClassMetadata $source
*/
public function mergeConstraints(ClassMetadata $source)
{
foreach ($source->getPropertiesMetadata() as $property => $propertyMetadata) {
foreach ($propertyMetadata->getConstraints() as $group => $constraints) {
$this->addPropertyConstraints($property, $constraints, $group);
}
}
}

/**
* {@inheritdoc}
*/
public function serialize()
{
return serialize(array(
$this->name,
$this->methodMetadata,
$this->propertyMetadata,
$this->fileResources,
$this->createdAt,
$this->defaultGroup,
));
}

/**
* {@inheritdoc}
*/
public function unserialize($str)
{
list(
$this->name,
$this->methodMetadata,
$this->propertyMetadata,
$this->fileResources,
$this->createdAt,
$this->defaultGroup) = unserialize($str);

$this->reflection = new \ReflectionClass($this->name);
}
}
89 changes: 89 additions & 0 deletions Mapping/Driver/StaticDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Validator\Mapping\Driver;

use Cubiche\Core\Validator\Assert;
use Cubiche\Core\Validator\Mapping\ClassMetadata;
use Cubiche\Core\Validator\Mapping\Exception\MappingException;
use Metadata\Driver\DriverInterface;

/**
* StaticDriver class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class StaticDriver implements DriverInterface
{
/**
* The name of the method to call.
*
* @var string
*/
protected $methodName;

/**
* @var string
*/
protected $defaultGroup;

/**
* StaticDriver constructor.
*
* @param string $methodName
* @param string $defaultGroup
*/
public function __construct($methodName = 'loadValidatorMetadata', $defaultGroup = Assert::DEFAULT_GROUP)
{
$this->methodName = $methodName;
$this->defaultGroup = $defaultGroup;
}

/**
* {@inheritdoc}
*/
public function loadMetadataForClass(\ReflectionClass $reflClass)
{
if (!$reflClass->isInterface() && $reflClass->hasMethod($this->methodName)) {
$reflMethod = $reflClass->getMethod($this->methodName);

if ($reflMethod->isAbstract()) {
throw MappingException::withMessage(
'The class %s should not be and abstract class',
$reflClass->name,
$this->methodName
);
}

if (!$reflMethod->isStatic()) {
throw MappingException::withMessage(
'The method %s::%s should be static',
$reflClass->name,
$this->methodName
);
}

if ($reflMethod->getDeclaringClass()->name != $reflClass->name) {
throw MappingException::withMessage(
'The method %s should be declared in %s class',
$this->methodName,
$reflClass->name
);
}

$metadata = new ClassMetadata($reflClass->getName());
$metadata->defaultGroup = $this->defaultGroup;

$reflMethod->invoke(null, $metadata);

return $metadata;
}
}
}
34 changes: 34 additions & 0 deletions Mapping/Exception/MappingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Validator\Mapping\Exception;

/**
* MappingException class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class MappingException extends \Exception
{
/**
* @param string $message
* @param string $className
* @param string $fieldName
*
* @return static
*/
public static function withMessage($message, $className, $fieldName)
{
return new static(sprintf(
$message,
$className,
$fieldName
));
}
}

0 comments on commit 6469981

Please sign in to comment.