-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
976 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
)); | ||
} | ||
} |
Oops, something went wrong.