CollectionManager.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Muzich\CoreBundle\lib\Collection;
  3. use \Doctrine\Common\Collections\ArrayCollection;
  4. abstract class CollectionManager
  5. {
  6. protected $content;
  7. protected $schema = array();
  8. protected $object_reference_attribute;
  9. protected $allow_duplicates = false;
  10. public function __construct($content)
  11. {
  12. if (!is_array($content))
  13. throw new \Exception('Content must be array type !');
  14. if (!count($this->schema))
  15. throw new \Exception('Schema must be defined in child class !');
  16. if (!count($this->object_reference_attribute))
  17. throw new \Exception('Object reference attribute must be defined in child class !');
  18. $this->content = $content;
  19. }
  20. public function add($object)
  21. {
  22. if (!$this->have($object) || $this->allow_duplicates)
  23. {
  24. $content_line = array();
  25. foreach ($this->schema as $attribute)
  26. {
  27. $method_name = 'get' . $attribute;
  28. $content_line[lcfirst($attribute)] = (string)$object->$method_name();
  29. }
  30. $this->content[] = $content_line;
  31. }
  32. }
  33. public function have($object)
  34. {
  35. $method_name = 'get' . $this->object_reference_attribute;
  36. foreach ($this->content as $content_line)
  37. {
  38. if ($object->$method_name() == $content_line[lcfirst($this->object_reference_attribute)])
  39. {
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. public function remove($object)
  46. {
  47. $new_content = array();
  48. $method_name = 'get' . $this->object_reference_attribute;
  49. foreach ($this->content as $content_line)
  50. {
  51. if ($object->$method_name() != $content_line[lcfirst($this->object_reference_attribute)])
  52. {
  53. $new_content[] = $content_line;
  54. }
  55. }
  56. $this->content = $new_content;
  57. }
  58. public function removeWithIndex($index)
  59. {
  60. if (array_key_exists($index, $this->content))
  61. {
  62. unset($this->content[$index]);
  63. $this->content = array_values($this->content);
  64. }
  65. }
  66. public function index($object)
  67. {
  68. $method_name = 'get' . $this->object_reference_attribute;
  69. foreach ($this->content as $index => $content_line)
  70. {
  71. if ($object->$method_name() == $content_line[lcfirst($this->object_reference_attribute)])
  72. {
  73. return $index;
  74. }
  75. }
  76. return null;
  77. }
  78. public function removeWithReference($reference)
  79. {
  80. $new_content = array();
  81. foreach ($this->content as $content_line)
  82. {
  83. if ($reference != $content_line[lcfirst($this->object_reference_attribute)])
  84. {
  85. $new_content[] = $content_line;
  86. }
  87. }
  88. $this->content = $new_content;
  89. }
  90. public function getAttributes($attribute)
  91. {
  92. if (!in_array($attribute, $this->schema))
  93. throw new \Exception('This attribute is unknow !');
  94. $attributes = array();
  95. foreach ($this->content as $content_line)
  96. {
  97. $attributes[] = $content_line[lcfirst($attribute)];
  98. }
  99. return $attributes;
  100. }
  101. public function getContent()
  102. {
  103. return $this->content;
  104. }
  105. }