Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move taxonomy from taxonomy library

  • Loading branch information...
commit 6de51d78edb05a87ff5694ae627e8abfd8f5d1ed 1 parent d9cbdf7
Richard Shank iampersistent authored
117 lib/Vespolina/Entity/Taxonomy/Taxonomy.php
View
@@ -0,0 +1,117 @@
+<?php
+/**
+ * (c) Vespolina Project http://www.vespolina-project.org
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Vespolina\Entity\Taxonomy;
+
+use Vespolina\Entity\Taxonomy\TaxonomyInterface;
+
+/**
+ * @author Daniel Kucharski <daniel@xerias.be>
+ */
+class Taxonomy implements TaxonomyInterface
+{
+ protected $isHierarchical;
+ protected $name;
+ protected $terms;
+ protected $type;
+
+ public function __construct($isHierarchical = false)
+ {
+ $this->isHierarchical = $isHierarchical;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function addTerm(TermInterface $term)
+ {
+ $this->terms[] = $term;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function addTerms(array $terms)
+ {
+ $this->terms = array_merge($this->terms, $terms);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function clearTerms()
+ {
+ $this->terms = array();
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getTerms()
+ {
+ return $this->terms;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function removeTerm(TermInterface $term)
+ {
+ foreach ($this->terms as $key => $termToCompare) {
+ if ($termToCompare == $term) {
+ unset($this->terms[$key]);
+ break;
+ }
+ }
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function setTerms(array $terms)
+ {
+ $this->terms = $terms;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getType()
+ {
+ return $this->type;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function setType($type)
+ {
+ $this->type = $type;
+ }
+
+ protected function slugify($text)
+ {
+ return preg_replace('/[^a-z0-9_\s-]/', '', preg_replace("/[\s_]/", "-", preg_replace('!\s+!', ' ', strtolower(trim($text)))));
+ }
+}
82 lib/Vespolina/Entity/Taxonomy/TaxonomyInterface.php
View
@@ -0,0 +1,82 @@
+<?php
+/**
+ * (c) Vespolina Project http://www.vespolina-project.org
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+namespace Vespolina\Entity\Taxonomy;
+
+use Vespolina\Entity\Taxonomy\TermInterface;
+
+
+/**
+ * @author Daniel Kucharski <daniel@xerias.be>
+ */
+interface TaxonomyInterface
+{
+ /**
+ * Add a term to the collection
+ *
+ * @param TermInterface $term
+ */
+ function addTerm(TermInterface $term);
+
+ /**
+ * Add a collection of terms
+ *
+ * @param array $terms
+ */
+ function addTerms(array $terms);
+
+ /**
+ * Remove all terms from the collection
+ */
+ function clearTerms();
+
+ /**
+ * Return a collection of terms
+ *
+ * @return array of terms
+ */
+ function getTerms();
+
+ /**
+ * Remove a term from the collection
+ *
+ * @param TermInterface $term
+ */
+ function removeTerm(TermInterface $term);
+
+ /**
+ * Set a collection of terms
+ *
+ * @param array $terms
+ */
+ function setTerms(array $terms);
+
+ /**
+ * Get the taxonomy name
+ * eg. Taxonomy_hierarchy
+ *
+ * @abstract
+ * @return string
+ */
+ function getName();
+
+ /**
+ * Retrieve the taxonomy type
+ *
+ * Possible options are:
+ * - hierarchical
+ * - tags
+ *
+ * @abstract
+ *
+ */
+ function getType();
+
+ function setName($name);
+
+ function setType($type);
+}
202 lib/Vespolina/Entity/Taxonomy/Term.php
View
@@ -0,0 +1,202 @@
+<?php
+/**
+ * (c) Vespolina Project http://www.vespolina-project.org
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Vespolina\Entity\Taxonomy;
+
+use Vespolina\Entity\Taxonomy\TermInterface;
+
+/**
+ * @author Daniel Kucharski <daniel@xerias.be>
+ */
+class Term implements TermInterface
+{
+ protected $attributes;
+ protected $children;
+ protected $name;
+ protected $path;
+ protected $parent;
+
+ public function __construct($name)
+ {
+ $this->name = $name;
+ }
+
+
+ /**
+ * @inheritdoc
+ */
+ public function addAttribute($name, $value)
+ {
+ $this->attributes[$name] = $value;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function addAttributes(array $attributes)
+ {
+ $this->attributes = array_merge($this->attributes, $attributes);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function clearAttributes()
+ {
+ $this->attributes = array();
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getAttribute($name)
+ {
+ if (isset($this->attributes[$name])) {
+
+ return $this->attributes[$name];
+ }
+
+ return null;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getAttributes()
+ {
+ return $this->attributes;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function removeAttribute($name)
+ {
+ unset($this->attributes[$name]);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function setAttributes(array $attributes)
+ {
+ $this->attributes = $attributes;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function addChild(TermInterface $term)
+ {
+ $this->children[] = $term;
+ $rc = new \ReflectionProperty($term, 'parent');
+ $rc->setAccessible(true);
+ $rc->setValue($term, $this);
+ $rc->setAccessible(false);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function addChildren(array $terms)
+ {
+ foreach ($terms as $term) {
+ $this->addChild($term);
+ }
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function clearChildren()
+ {
+ $rc = new \ReflectionProperty($this, 'parent');
+ $rc->setAccessible(true);
+
+ foreach ($this->children as $term) {
+ $rc->setValue($term, null);
+ }
+ $rc->setAccessible(false);
+
+ $this->children = array();
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getChildren()
+ {
+ return $this->children;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function removeChild(TermInterface $term)
+ {
+ foreach ($this->children as $key => $termToCompare) {
+ if ($termToCompare == $term) {
+ unset($this->children[$key]);
+ $rc = new \ReflectionProperty($term, 'parent');
+ $rc->setAccessible(true);
+ $rc->setValue($term, null);
+ $rc->setAccessible(false);
+
+ return;
+ }
+ }
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function setChildren(array $terms)
+ {
+ $this->clearChildren();
+ $this->addChildren($terms);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+ /**
+ * @inheritdoc
+ */
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getPath()
+ {
+ return $this->path;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function setPath($path)
+ {
+ $this->path = $path;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getParent()
+ {
+ return $this->parent;
+ }
+}
124 lib/Vespolina/Entity/Taxonomy/TermInterface.php
View
@@ -0,0 +1,124 @@
+<?php
+/**
+ * (c) Vespolina Project http://www.vespolina-project.org
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+namespace Vespolina\Entity\Taxonomy;
+
+/**
+ * @author Daniel Kucharski <daniel@xerias.be>
+ */
+interface TermInterface
+{
+ /**
+ * Add an attribute to the collection
+ *
+ * @param $name, $value
+ */
+ function addAttribute($name, $value);
+
+ /**
+ * Add a collection of Attribute
+ *
+ * @param array $attributes
+ */
+ function addAttributes(array $attributes);
+
+ /**
+ * Remove all attributes from the collection
+ */
+ function clearAttributes();
+
+ /**
+ * Return a specific attribute from the collection
+ *
+ * @param $name
+ */
+ function getAttribute($name);
+
+ /**
+ * Return a collection of Attribute
+ *
+ * @return array of attributes
+ */
+ function getAttributes();
+
+ /**
+ * Remove an attribute from the collection
+ *
+ * @param string $name
+ */
+ function removeAttribute($name);
+
+ /**
+ * Set a collection of Attribute
+ *
+ * @param array $attributes
+ */
+ function setAttributes(array $attributes);
+
+ /**
+ * Add a term to the collection
+ *
+ * @param TermInterface $term
+ */
+ function addChild(TermInterface $term);
+
+ /**
+ * Add a collection of terms
+ *
+ * @param array $terms
+ */
+ function addChildren(array $terms);
+
+ /**
+ * Remove all terms from the collection
+ */
+ function clearChildren();
+
+ /**
+ * Return the children terms
+ *
+ * @return array of terms
+ */
+ function getChildren();
+
+ /**
+ * Remove a child from the term
+ *
+ * @param TermInterface $term
+ */
+ function removeChild(TermInterface $term);
+
+ /**
+ * Set a collection of terms as the children
+ *
+ * @param array $terms
+ */
+ function setChildren(array $terms);
+
+ /**
+ * Get the taxonomy name
+ * eg. product_hierarchy
+ *
+ * @abstract
+ * @return string
+ */
+ function getName();
+
+ function getPath();
+
+ function setName($name);
+
+ function setPath($path);
+
+ /**
+ * Return the parent term of this term, if it is hierarchical. If it is the root, the containing Taxonomy class is
+ * returned. Null is returned if it is not hierarchical.
+ *
+ * @return mixed
+ */
+ function getParent();
+}
37 tests/Entity/Taxonomy/TaxonomyTest.php
View
@@ -0,0 +1,37 @@
+<?php
+
+use Vespolina\Entity\Taxonomy\Taxonomy;
+use Vespolina\Entity\Taxonomy\Term;
+
+class TaxonomyTest extends \PHPUnit_Framework_TestCase
+{
+ public function testTerm()
+ {
+ $taxonomy = new Taxonomy();
+ $this->assertNull($taxonomy->getTerms(), 'make sure we start out empty');
+
+ $term = new Term('term');
+ $taxonomy->addTerm($term);
+ $this->assertContains($term, $taxonomy->getTerms());
+ $this->assertCount(1, $taxonomy->getTerms());
+
+ $terms = array();
+ $terms[] = new Term('term1');
+ $terms[] = new Term('term2');
+ $taxonomy->addTerms($terms);
+ $this->assertCount(3, $taxonomy->getTerms());
+ $this->assertContains($term, $taxonomy->getTerms());
+
+ $taxonomy->removeTerm($term);
+ $this->assertNotContains($term, $taxonomy->getTerms());
+ $this->assertCount(2, $taxonomy->getTerms());
+
+ $taxonomy->clearTerms();
+ $this->assertEmpty($taxonomy->getTerms());
+
+ $taxonomy->addTerm($term);
+ $taxonomy->setTerms($terms);
+ $this->assertNotContains($term, $taxonomy->getTerms(), 'this should have been removed on setting a new array of items');
+ $this->assertCount(2, $taxonomy->getTerms());
+ }
+}
91 tests/Entity/Taxonomy/TermTest.php
View
@@ -0,0 +1,91 @@
+<?php
+
+use Vespolina\Entity\Taxonomy\Term;
+
+class TestTerm extends \PHPUnit_Framework_TestCase
+{
+ public function testChildren()
+ {
+ $rootTerm = new Term('root');
+ $this->assertNull($rootTerm->getChildren(), 'make sure we start out empty');
+
+ $child = new Term('child');
+ $rootTerm->addChild($child);
+ $this->assertContains($child, $rootTerm->getChildren());
+ $this->assertCount(1, $rootTerm->getChildren());
+ $this->assertSame($rootTerm, $child->getParent());
+
+ $children = array();
+ $children[0] = new Term('child0');
+ $children[1] = new Term('child1');
+ $rootTerm->addChildren($children);
+ $this->assertCount(3, $rootTerm->getChildren());
+ $this->assertContains($children[0], $rootTerm->getChildren());
+ $this->assertContains($children[1], $rootTerm->getChildren());
+ $this->assertSame($rootTerm, $children[0]->getParent());
+ $this->assertSame($rootTerm, $children[1]->getParent());
+
+ $rootTerm->removeChild($child);
+ $this->assertNotContains($child, $rootTerm->getChildren());
+ $this->assertCount(2, $rootTerm->getChildren());
+ $this->assertNull($child->getParent());
+
+ $rootTerm->clearChildren();
+ $this->assertEmpty($rootTerm->getChildren());
+ $this->assertNull($children[0]->getParent());
+ $this->assertNull($children[1]->getParent());
+
+ $rootTerm->addChild($child);
+ $rootTerm->setChildren($children);
+ $this->assertNotContains($child, $rootTerm->getChildren(), 'this should have been removed on setting a new array of items');
+ $this->assertNull($child->getParent());
+ $this->assertCount(2, $rootTerm->getChildren());
+ $this->assertContains($children[0], $rootTerm->getChildren());
+ $this->assertContains($children[1], $rootTerm->getChildren());
+ $this->assertSame($rootTerm, $children[0]->getParent());
+ $this->assertSame($rootTerm, $children[1]->getParent());
+ }
+
+ public function testAttributeMethods()
+ {
+ $term = new Term('term');
+
+ $this->assertNull($term->getAttribute('noAttribute'));
+
+ $term->addAttribute('attribute1', 1);
+ $this->assertCount(1, $term->getAttributes());
+ $this->assertSame(1, $term->getAttribute('attribute1'));
+
+ $term->addAttribute('attribute2', 2);
+ $this->assertCount(2, $term->getAttributes());
+ $this->assertSame(2, $term->getAttribute('attribute2'));
+
+ $term->removeAttribute('attribute2');
+ $this->assertCount(1, $term->getAttributes(), 'remove by attribute');
+ $this->assertNull($term->getAttribute('attribute2'));
+
+ $attributes = array(
+ 'attribute2' => 2,
+ 'attribute3' => 3
+ );
+
+ $term->addAttributes($attributes);
+ $this->assertCount(3, $term->getAttributes());
+ $this->assertSame(2, $term->getAttribute('attribute2'));
+ $this->assertSame(3, $term->getAttribute('attribute3'));
+
+ $term->setAttributes($attributes);
+ $this->assertCount(2, $term->getAttributes());
+ $this->assertNull($term->getAttribute('attribute1'));
+ $this->assertSame(2, $term->getAttribute('attribute2'));
+ $this->assertSame(3, $term->getAttribute('attribute3'));
+
+ $term->removeAttribute('attribute3');
+ $this->assertCount(1, $term->getAttributes(), 'attribute should be removed by type');
+ $term->removeAttribute('nada');
+ $this->assertCount(1, $term->getAttributes());
+
+ $term->clearAttributes();
+ $this->assertEmpty($term->getAttributes());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.