123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
-
- namespace Muzich\CoreBundle\lib\Collection;
-
- use \Doctrine\Common\Collections\ArrayCollection;
-
- abstract class CollectionManager
- {
-
- protected $content;
- protected $schema = array();
- protected $object_reference_attribute;
- protected $allow_duplicates = false;
-
- public function __construct($content)
- {
- if (!is_array($content))
- throw new \Exception('Content must be array type !');
-
- if (!count($this->schema))
- throw new \Exception('Schema must be defined in child class !');
-
- if (!count($this->object_reference_attribute))
- throw new \Exception('Object reference attribute must be defined in child class !');
-
- $this->content = $content;
- }
-
- public function add($object)
- {
- if (!$this->have($object) || $this->allow_duplicates)
- {
- $content_line = array();
- foreach ($this->schema as $attribute)
- {
- $method_name = 'get' . $attribute;
- $content_line[lcfirst($attribute)] = (string)$object->$method_name();
- }
-
- $this->content[] = $content_line;
- }
- }
-
- public function count($object)
- {
- $count = 0;
- $method_name = 'get' . $this->object_reference_attribute;
- foreach ($this->content as $content_line)
- {
- if ($object->$method_name() == $content_line[lcfirst($this->object_reference_attribute)])
- {
- $count++;
- }
- }
-
- return $count;
- }
-
- public function have($object)
- {
- $method_name = 'get' . $this->object_reference_attribute;
- foreach ($this->content as $content_line)
- {
- if ($object->$method_name() == $content_line[lcfirst($this->object_reference_attribute)])
- {
- return true;
- }
- }
-
- return false;
- }
-
- public function haveRefId($ref_id)
- {
- foreach ($this->content as $content_line)
- {
- if ($ref_id == $content_line[lcfirst($this->object_reference_attribute)])
- {
- return true;
- }
- }
-
- return false;
- }
-
- public function remove($object)
- {
- $new_content = array();
- $method_name = 'get' . $this->object_reference_attribute;
- foreach ($this->content as $content_line)
- {
- if ($object->$method_name() != $content_line[lcfirst($this->object_reference_attribute)])
- {
- $new_content[] = $content_line;
- }
- }
-
- $this->content = $new_content;
- }
-
- public function removeWithIndex($index)
- {
- if (array_key_exists($index, $this->content))
- {
- unset($this->content[$index]);
- $this->content = array_values($this->content);
- }
- }
-
- public function getLineWithIndex($index)
- {
- if (array_key_exists($index, $this->content))
- {
- return $this->content[$index];
- }
- }
-
- public function index($object)
- {
- $method_name = 'get' . $this->object_reference_attribute;
- foreach ($this->content as $index => $content_line)
- {
- if ($object->$method_name() == $content_line[lcfirst($this->object_reference_attribute)])
- {
- return $index;
- }
- }
-
- return null;
- }
-
- public function removeWithReference($reference)
- {
- $new_content = array();
- foreach ($this->content as $content_line)
- {
- if ($reference != $content_line[lcfirst($this->object_reference_attribute)])
- {
- $new_content[] = $content_line;
- }
- }
-
- $this->content = $new_content;
- }
-
- public function getAttributes($attribute)
- {
- if (!in_array($attribute, $this->schema))
- throw new \Exception('This attribute is unknow !');
-
- $attributes = array();
- foreach ($this->content as $content_line)
- {
- $attributes[] = $content_line[lcfirst($attribute)];
- }
-
- return $attributes;
- }
-
- public function getContent()
- {
- return $this->content;
- }
-
- }
|