CollectionManager.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 count($object)
  34. {
  35. $count = 0;
  36. $method_name = 'get' . $this->object_reference_attribute;
  37. foreach ($this->content as $content_line)
  38. {
  39. if ($object->$method_name() == $content_line[lcfirst($this->object_reference_attribute)])
  40. {
  41. $count++;
  42. }
  43. }
  44. return $count;
  45. }
  46. public function have($object)
  47. {
  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. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. public function haveRefId($ref_id)
  59. {
  60. foreach ($this->content as $content_line)
  61. {
  62. if ($ref_id == $content_line[lcfirst($this->object_reference_attribute)])
  63. {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. public function remove($object)
  70. {
  71. $new_content = array();
  72. $method_name = 'get' . $this->object_reference_attribute;
  73. foreach ($this->content as $content_line)
  74. {
  75. if ($object->$method_name() != $content_line[lcfirst($this->object_reference_attribute)])
  76. {
  77. $new_content[] = $content_line;
  78. }
  79. }
  80. $this->content = $new_content;
  81. }
  82. public function removeWithIndex($index)
  83. {
  84. if (array_key_exists($index, $this->content))
  85. {
  86. unset($this->content[$index]);
  87. $this->content = array_values($this->content);
  88. }
  89. }
  90. public function getLineWithIndex($index)
  91. {
  92. if (array_key_exists($index, $this->content))
  93. {
  94. return $this->content[$index];
  95. }
  96. }
  97. public function index($object)
  98. {
  99. $method_name = 'get' . $this->object_reference_attribute;
  100. foreach ($this->content as $index => $content_line)
  101. {
  102. if ($object->$method_name() == $content_line[lcfirst($this->object_reference_attribute)])
  103. {
  104. return $index;
  105. }
  106. }
  107. return null;
  108. }
  109. public function removeWithReference($reference)
  110. {
  111. $new_content = array();
  112. foreach ($this->content as $content_line)
  113. {
  114. if ($reference != $content_line[lcfirst($this->object_reference_attribute)])
  115. {
  116. $new_content[] = $content_line;
  117. }
  118. }
  119. $this->content = $new_content;
  120. }
  121. public function getAttributes($attribute)
  122. {
  123. if (!in_array($attribute, $this->schema))
  124. throw new \Exception('This attribute is unknow !');
  125. $attributes = array();
  126. foreach ($this->content as $content_line)
  127. {
  128. $attributes[] = $content_line[lcfirst($attribute)];
  129. }
  130. return $attributes;
  131. }
  132. public function getContent()
  133. {
  134. return $this->content;
  135. }
  136. }