collection.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php namespace Client\Request;
  2. class SearchParamException extends \Exception {};
  3. abstract class Collection extends Base implements \Lib\ArrayIterator {
  4. private $objs = array();
  5. public function add($obj, $key = null) {
  6. if ($obj == false)
  7. echo "OBJ ($key) = FALSE\n";
  8. if (!isset($key))
  9. array_push($this->objs, $obj);
  10. else
  11. $this->objs[$key] = $obj;
  12. }
  13. public function remove($any) {
  14. if (isset($this->objs[$any])) {
  15. unset($this->objs[$any]);
  16. return true;
  17. } else if (in_array($any, $this->objs)) {
  18. $key = array_search($any, $this->objs, true);
  19. return $this->remove($key);
  20. }
  21. return false;
  22. }
  23. public function all() {
  24. return $this->objs;
  25. }
  26. public function len() {
  27. return $this->objs;
  28. }
  29. public function by_key($idx) {
  30. return isset($this->objs[$idx]) ? $this->objs[$idx] : false;
  31. }
  32. public function next() {
  33. return next($this->objs);
  34. }
  35. public function prev() {
  36. return prev($this->objs);
  37. }
  38. public function current() {
  39. return current($this->objs);
  40. }
  41. public function reset() {
  42. return reset($this->objs);
  43. }
  44. //abstract public function create(...$args);
  45. abstract public function search(array $params = array());
  46. }
  47. class CollectionResult implements \Lib\ArrayIterator {
  48. private $objs = array();
  49. public function set($val, $key = null) {
  50. if ($key == null && is_array($val))
  51. $this->objs = $val;
  52. else if ($key != null)
  53. $this->objs[$key] = $val;
  54. else
  55. array_push($this->objs, $val);
  56. }
  57. public function by_key($idx) {
  58. return isset($this->objs[$idx]) ? $this->objs[$idx] : false;
  59. }
  60. public function all() {
  61. return $this->objs;
  62. }
  63. public function len() {
  64. return count($this->objs);
  65. }
  66. public function next() {
  67. return next($this->objs);
  68. }
  69. public function current() {
  70. return current($this->objs);
  71. }
  72. public function reset() {
  73. return current($this->objs);
  74. }
  75. }
  76. ?>