Permalink
Browse files

[Filter] Creating basic interfaces and filter logic

[Tests] Base testing structure and phpunit configuration
  • Loading branch information...
rdohms committed Jul 15, 2011
0 parents commit 98cbcf198368893b81061fe6ef112f3894ea747d
@@ -0,0 +1,85 @@
+<?php
+
+namespace DMS\Filter;
+
+/**
+ *
+ *
+ * @package DMS
+ * @subpackage Filter
+ *
+ */
+class Filter implements FilterInterface
+{
+ /**
+ *
+ * @var Mapping\ClassMetadataFactory
+ */
+ protected $metadataFactory;
+
+ /**
+ * Constructor
+ *
+ * @param Mapping\ClassMetadataFactory $metadataFactory
+ */
+ public function __construct(Mapping\ClassMetadataFactory $metadataFactory)
+ {
+ $this->metadataFactory = $metadataFactory;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function filter($object)
+ {
+ $this->walkObject($object);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function filterProperty($object, $property)
+ {
+ $this->walkObject($object, $property);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function filterValue($value, Rules\Rule $filter)
+ {
+ return $filter->applyFilter($value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getMetadataFactory()
+ {
+ return $this->metadataFactory;
+ }
+
+ protected function walkObject($object, $limitProperty = null) {
+
+ if ( $object === null ) {
+ return;
+ }
+
+ $metadata = $this->metadataFactory->getClassMetadata(get_class($object));
+
+ //Get a Object Handler/Walker
+ $walker = new ObjectWalker($object);
+
+ //Get all filtered properties or limit with selected
+ $properties = ($limitProperty !== null)? array($limitProperty) : $metadata->getFilteredProperties();
+
+ //Iterate over properties with filters
+ foreach( $properties as $property ) {
+
+ $walker->applyFilterRules($property, $metadata->getPropertyRules($property));
+
+ }
+
+ }
+
+}
@@ -0,0 +1,43 @@
+<?php
+
+namespace DMS\Filter;
+
+/**
+ * Filters the values of a given object
+ */
+interface FilterInterface
+{
+ /**
+ * Iterates over the properties of the object applying filters and
+ * replacing values
+ *
+ * @param mixed $object
+ */
+ public function filter($object);
+
+ /**
+ * Filters a specific property in an object, replacing the current value
+ *
+ * @param mixed $object
+ * @param string $property
+ */
+ public function filterProperty($object, $property);
+
+ /**
+ * Runs a given value through a filter rule returning the filtered value
+ *
+ * @param mixed $value
+ * @param Rules\Rule $filter
+ *
+ * @return mixed
+ */
+ public function filterValue($value, Rules\Rule $filter);
+
+ /**
+ * Retrieves the metadata factory for class metdatas
+ *
+ * @return Mapping\ClassMetadataFactoryInterface
+ */
+ public function getMetadataFactory();
+
+}
@@ -0,0 +1,29 @@
+<?php
+
+namespace DMS\Filter\Mapping\Cache;
+
+interface CacheInterface
+{
+ /**
+ * Returns whether metadata for the given class exists in the cache
+ *
+ * @param string $class
+ */
+ function has($class);
+
+ /**
+ * Returns the metadata for the given class from the cache
+ *
+ * @param string $class Class Name
+ *
+ * @return ClassMetadata
+ */
+ function read($class);
+
+ /**
+ * Stores a class metadata in the cache
+ *
+ * @param ClassMetadata $metadata A Class Metadata
+ */
+ function write(ClassMetadata $metadata);
+}
@@ -0,0 +1,12 @@
+<?php
+
+namespace DMS\Filter\Mapping;
+
+class ClassMetadata
+{
+
+ public $name;
+ public $properties;
+ private $reflClass;
+
+}
@@ -0,0 +1,10 @@
+<?php
+
+namespace DMS\Filter\Mapping;
+
+class ClassMetadataFactory
+{
+ public function getClassMetadata($class){
+
+ }
+}
@@ -0,0 +1,14 @@
+<?php
+
+namespace DMS\Filter\Mapping;
+
+interface ClassMetadataFactoryInterface
+{
+ /**
+ * Retrieve metadata for the provided class
+ *
+ * @param string $class
+ * @return ClassMetadataInterface
+ */
+ public function getClassMetadata($class);
+}
@@ -0,0 +1,22 @@
+<?php
+
+namespace DMS\Filter\Mapping;
+
+interface ClassMetadataInterface
+{
+ /**
+ * Retrieve a list of the object's properties that have filters attached
+ * to them
+ *
+ * @return array
+ */
+ public function getFilteredProperties();
+
+ /**
+ * Retrieve s list of filtering rules attached to a property
+ *
+ * @param string $property
+ * @return array
+ */
+ public function getPropertyRules($property);
+}
@@ -0,0 +1,15 @@
+<?php
+
+namespace DMS\Filter\Mapping\Loader;
+
+interface LoaderInterface
+{
+ /**
+ * Load a Class Metadata.
+ *
+ * @param ClassMetadata $metadata A metadata
+ *
+ * @return Boolean
+ */
+ function loadClassMetadata(ClassMetadata $metadata);
+}
@@ -0,0 +1,53 @@
+<?php
+
+namespace DMS\Filter;
+
+class ObjectWalker
+{
+
+ protected $object;
+
+ public function __construct( $object )
+ {
+ $this->object = $object;
+ }
+
+ /**
+ *
+ * @param string $property
+ * @param array $filterRules
+ */
+ public function applyFilterRules($property, $filterRules = array())
+ {
+ foreach($filterRules as $rule ) {
+ $this->applyFilterRule($property, $rule);
+ }
+ }
+
+ /**
+ *
+ * @param string $property
+ * @param Rules\Rule $filterRule
+ */
+ public function applyFilterRule($property, Rules\Rule $filterRule)
+ {
+
+ $value = $this->getPropertyValue($property);
+
+ $filteredValue = $filterRule->applyFilter($value);
+
+ $this->setPropertyValue($property, $filteredValue);
+
+ }
+
+ private function getPropertyValue($property)
+ {
+ //make property accessible and read
+ return $value;
+ }
+
+ private function setPropertyValue($property, $value)
+ {
+ //make property accessible and override
+ }
+}
@@ -0,0 +1,10 @@
+# DMS Filter Component
+
+This library provides a service that can be used to filter object values based on annotations
+
+## 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.
+
+Symfony 2 Validator: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator
Oops, something went wrong.

0 comments on commit 98cbcf1

Please sign in to comment.