Collection class


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. class Collection {
  3. private $items;
  4. private $attributes;
  5.  
  6. public function __construct() {
  7. $this->items = array();
  8. $this->attributes = array();
  9. $this->attributes['Count'] = 0;
  10. $this->attributes['IsFixedSize'] = false;
  11. $this->attributes['FixedSize'] = 0;
  12. $this->attributes['IsReadOnly'] = false;
  13. }
  14.  
  15. public function __get($var) {
  16. if(key_exists($var, (array)$this->attributes)) {
  17. return $this->attributes[$var];
  18. }
  19. else {
  20. throw new Exception("The property {$var} does not exist", 0);
  21. }
  22. }
  23.  
  24. public function __set($var, $value) {
  25. if(key_exists($var, (array)$this->attributes)) {
  26. $this->attributes[$var] = $value;
  27. }
  28. else {
  29. throw new Exception("The property {$var} cannot be set as it does not exist", 0);
  30. }
  31. }
  32.  
  33. public function Add($item) {
  34. if($this->IsFixedSize) {
  35. if($this->Count < $this->FixedSize) {
  36. $this->items[] = $item;
  37. $this->Count += 1;
  38. }
  39. else {
  40. throw new Exception("Cannot not add more items to collection. Max size is {$this->FixedSize}", 0);
  41. }
  42. }
  43. else if($this->IsReadOnly) {
  44. throw new Exception("Cannot add item to a read only collection", 0);
  45. }
  46. else {
  47. $this->items[] = $item;
  48. $this->Count += 1;
  49. }
  50. }
  51.  
  52. public function AddRange(array $items) {
  53. foreach($items as $item) {
  54. self::Add($item);
  55. }
  56. }
  57.  
  58. public function Contains($item) {
  59. foreach($this->items as $i) {
  60. if($i == $item) {
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66.  
  67. public function Get($index) {
  68. if(key_exsits($index, $this->items)) {
  69. return $this->items[$index];
  70. }
  71. return false;
  72. }
  73.  
  74. public function GetCollectionAsArray() {
  75. return $this->items;
  76. }
  77.  
  78. public function GetCollectionAsString() {
  79. return implode(", ", $this->items);
  80. }
  81.  
  82. public function IndexOf($item, $startIndex = 0) {
  83. for($i = $startIndex; $i < $this->Count; $i++) {
  84. if($this->items[$i] == $item) {
  85. return $i;
  86. break;
  87. }
  88. }
  89. return -1;
  90. }
  91.  
  92. public function LastIndexOf($item) {
  93. $lastIndex = -1;
  94.  
  95. for($i = 0; $i < $this->Count; $i++) {
  96. if($this->items[$i] == $item) {
  97. $lastIndex = $i;
  98. }
  99. }
  100. return $lastIndex;
  101. }
  102.  
  103. public function Insert($index, $item) {
  104. if($this->IsFixedSize) {
  105. if($index < $this->FixedSize) {
  106. $this->items[$index] = $item;
  107. $this->Count += 1;
  108. }
  109. else {
  110. throw new Exception("Cannot insert item at {$index}. Max size is {$this->FixedSize}", 0);
  111. }
  112. }
  113. else if($this->IsReadOnly) {
  114. throw new Exception("Cannot insert an item into a read only collection", 0);
  115. }
  116. else {
  117. $this->items[$index] = $item;
  118. $this->Count += 1;
  119. }
  120. }
  121.  
  122. public function Remove($item) {
  123. $index = self::IndexOf($item);
  124. self::RemoveAt($index);
  125. }
  126.  
  127. public function RemoveAt($index) {
  128. if(!$this->IsReadOnly) {
  129. if(key_exists($index, $this->items)) {
  130. unset($this->items[$index]);
  131. $this->Count -= 1;
  132. }
  133. else {
  134. throw new Exception("Index out of range. The index {$index} is out of range of the collection", 0);
  135. }
  136. }
  137. else {
  138. throw new Exception("Cannot remove item from read only collection", 0);
  139. }
  140. }
  141.  
  142. public function RemoveRange($startIndex, $endIndex) {
  143. for($i = $startIndex; $i < $endIndex; $i++) {
  144. self::RemoveAt($i);
  145. }
  146. }
  147.  
  148. public function Sort() {
  149. sort($this->items, SORT_STRING);
  150. }
  151. }
  152. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.