Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Filter] Finished initial implementation and documented all classes

[Filter] Added a new Rule: Trim
[DMS] Moved vendors to root
  • Loading branch information...
commit 76df592a6b9b8b454f5bfdfde0c4d49c92b0e975 1 parent ca0012c
@rdohms authored
View
3  Filter.php
@@ -3,7 +3,8 @@
namespace DMS\Filter;
/**
- *
+ * Filter Object, responsible for retrieving the filtering rules
+ * for the object and applying them
*
* @package DMS
* @subpackage Filter
View
3  FilterInterface.php
@@ -4,6 +4,9 @@
/**
* Filters the values of a given object
+ *
+ * @package DMS
+ * @subpackage Filter
*/
interface FilterInterface
{
View
3  Mapping/ClassMetadata.php
@@ -4,6 +4,9 @@
/**
* Represents a class that has Annotations
+ *
+ * @package DMS
+ * @subpackage Filter
*/
class ClassMetadata implements ClassMetadataInterface
{
View
33 Mapping/ClassMetadataFactory.php
@@ -2,16 +2,39 @@
namespace DMS\Filter\Mapping;
+use Doctrine\Common\Cache\Cache;
+
+/**
+ * Responsible for loading metadata for selected classes
+ *
+ * @package DMS
+ * @subpackage Filter
+ */
class ClassMetadataFactory
{
-
+ /**
+ * @var Loader\LoaderInterface
+ */
protected $loader;
+ /**
+ * @var Doctrine\Common\Cache\Cache
+ */
protected $cache;
+ /**
+ * @var array
+ */
protected $parsedClasses = array();
- public function __construct(Loader\LoaderInterface $loader, Cache\CacheInterface $cache = null)
+ /**
+ * Constructor
+ * Receives a Loader and a Doctrine Compatible cache instance
+ *
+ * @param Loader\LoaderInterface $loader
+ * @param Cache $cache
+ */
+ public function __construct(Loader\LoaderInterface $loader, Cache $cache = null)
{
$this->loader = $loader;
$this->cache = $cache;
@@ -31,9 +54,9 @@ public function getClassMetadata($class)
}
//Check Cache for it
- if ($this->cache !== null && $this->cache->has($class)) {
+ if ($this->cache !== null && $this->cache->contains($class)) {
- $this->setParsedClass($class, $this->cache->read($class));
+ $this->setParsedClass($class, $this->cache->fetch($class));
return $this->getParsedClass($class);
}
@@ -63,7 +86,7 @@ private function parseClassMetadata($class)
$this->setParsedClass($class, $metadata);
if ($this->cache !== null) {
- $this->cache->write($metadata);
+ $this->cache->write($class, $metadata);
}
return $metadata;
View
6 Mapping/ClassMetadataFactoryInterface.php
@@ -2,6 +2,12 @@
namespace DMS\Filter\Mapping;
+/**
+ * Required methods of a Metadata Factory class
+ *
+ * @package DMS
+ * @subpackage Filter
+ */
interface ClassMetadataFactoryInterface
{
/**
View
6 Mapping/ClassMetadataInterface.php
@@ -2,6 +2,12 @@
namespace DMS\Filter\Mapping;
+/**
+ * Method required by a ClassMetadata class
+ *
+ * @package DMS
+ * @subpackage Filter
+ */
interface ClassMetadataInterface
{
/**
View
65 Mapping/Loader/AnnotationLoader.php
@@ -6,23 +6,34 @@
DMS\Filter\Rules;
/**
- * @todo document
+ * Loader that reads filtering data from Annotations
+ *
+ * @package DMS
+ * @subpackage Filter
*/
class AnnotationLoader implements LoaderInterface
{
-
+ /**
+ *
+ * @var Doctrine\Common\Annotations\Reader
+ */
protected $reader;
+ /**
+ * Constructor
+ *
+ * @param Doctrine\Common\Annotations\Reader $reader
+ */
public function __construct(Reader $reader)
{
$this->reader = $reader;
}
/**
- *
- * @param ClassMetadata $metadata
+ * Loads annotations data present in the class, using a Doctrine
+ * annotation reader
*
- * @todo calesthenics
+ * @param ClassMetadata $metadata
*/
public function loadClassMetadata(ClassMetadata $metadata)
{
@@ -31,19 +42,41 @@ public function loadClassMetadata(ClassMetadata $metadata)
//Iterate over properties to get annotations
foreach($reflClass->getProperties() as $property) {
- if ($property->getDeclaringClass()->getName() == $reflClass->getClassName()) {
-
- foreach($this->reader->getPropertyAnnotations($property) as $rule) {
-
- if ( $rule instanceof Rules\Rule ) {
- $metadata->addPropertyRule($property->getName(), $rule);
- }
-
- }
-
- }
+
+ $this->readProperty($property, $metadata);
+
}
}
+
+ /**
+ * Reads annotations for a selected property in the class
+ *
+ * @param ReflectionProperty $property
+ * @param ClassMetadata $metadata
+ */
+ private function readProperty($property, $metadata)
+ {
+ $reflClass = $metadata->getReflectionClass();
+
+ // Skip if this property is not from this class
+ if ($property->getDeclaringClass()->getName()
+ != $reflClass->getClassName()
+ ) {
+ return;
+ }
+
+ //Iterate over all annotations
+ foreach($this->reader->getPropertyAnnotations($property) as $rule) {
+
+ //Skip is its not a rule
+ if ( ! $rule instanceof Rules\Rule ) continue;
+
+ //Add Rule
+ $metadata->addPropertyRule($property->getName(), $rule);
+
+ }
+
+ }
}
View
6 Mapping/Loader/LoaderInterface.php
@@ -2,6 +2,12 @@
namespace DMS\Filter\Mapping\Loader;
+/**
+ * Interface for a Loader
+ *
+ * @package DMS
+ * @subpackage Filter
+ */
interface LoaderInterface
{
/**
View
65 ObjectWalker.php
@@ -2,18 +2,39 @@
namespace DMS\Filter;
+/**
+ * Walks over the properties of an object applying the filters
+ * that were defined for them
+ *
+ * @package DMS
+ * @subpackage Filter
+ */
class ObjectWalker
{
-
+ /**
+ * @var object
+ */
protected $object;
+ /**
+ * @var ReflectionClass
+ */
+ protected $reflClass;
+
+ /**
+ * Constructor
+ *
+ * @param object $object
+ */
public function __construct( $object )
{
$this->object = $object;
+ $this->reflClass = new \ReflectionClass($object);
}
/**
- *
+ * Applies the selected rules to a property in the object
+ *
* @param string $property
* @param array $filterRules
*/
@@ -25,7 +46,8 @@ public function applyFilterRules($property, $filterRules = array())
}
/**
- *
+ * Applies a Filtering Rule to a property
+ *
* @param string $property
* @param Rules\Rule $filterRule
*/
@@ -40,14 +62,41 @@ public function applyFilterRule($property, Rules\Rule $filterRule)
}
- private function getPropertyValue($property)
+ /**
+ * Retrieves the value of the property, overcoming visibility problems
+ *
+ * @param string $property
+ * @return mixed
+ */
+ private function getPropertyValue($propertyName)
+ {
+ return $this->getAccessibleReflectionProperty($propertyName)
+ ->getValue($this->object);
+ }
+
+ /**
+ * Overrides the value of a property, overcoming visibility problems
+ *
+ * @param string $property
+ * @param mixed $value
+ */
+ private function setPropertyValue($propertyName, $value)
{
- //make property accessible and read
- return $value;
+ $this->getAccessibleReflectionProperty($propertyName)
+ ->setValue($this->object, $value);
}
- private function setPropertyValue($property, $value)
+ /**
+ * Retrieves a property from the object and makes it visible
+ *
+ * @param string $propertyName
+ * @return ReflectionProperty
+ */
+ private function getAccessibleReflectionProperty($propertyName)
{
- //make property accessible and override
+ $property = $this->reflClass->getProperty($propertyName);
+ $property->setAccessible(true);
+
+ return $property;
}
}
View
12 README.md
@@ -2,9 +2,19 @@
This library provides a service that can be used to filter object values based on annotations
+## Usage
+
+TODO
+
+## Dependencies
+
+This package relies on these external libraries:
+
+* Doctrine Common: Reader and Cache
+
## Credits
This library is inspired by the Symfony 2 Validator component and is meant to work alongside it. Some
-of the code was inspired on it, so credit is due to the excelent work done over there.
+of the code was inspired on it, so credit is due to the excellent work done over there.
Symfony 2 Validator: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator
View
24 Rules/StripTags.php
@@ -2,13 +2,35 @@
namespace DMS\Filter\Rules;
+/**
+ * StripTags Rule
+ *
+ * @package DMS
+ * @subpackage Filter
+ *
+ */
class StripTags extends Rule
{
- public $allowed = "";
+ /**
+ * Comma separated string of allowed tags
+ *
+ * @var string
+ */
+ public $allowed = null;
+ /**
+ * {@inheritDoc}
+ */
public function applyFilter($value)
{
return strip_tags($value, $this->allowed);
}
+ /**
+ * {@inheritDoc}
+ */
+ public function getDefaultOption()
+ {
+ return 'allowed';
+ }
}
View
36 Rules/Trim.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace DMS\Filter\Rules;
+
+/**
+ * Trim Rule
+ *
+ * @package DMS
+ * @subpackage Filter
+ *
+ */
+class Trim extends Rule
+{
+ /**
+ * Comma separated string of allowed tags
+ *
+ * @var string
+ */
+ public $trimCharlist = null;
+
+ /**
+ * {@inheritDoc}
+ */
+ public function applyFilter($value)
+ {
+ return trim($value, $this->trimCharlist);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getDefaultOption()
+ {
+ return 'trimCharlist';
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.