pquery.array.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * pQuery plugin for executing common array functions.
  4. *
  5. * @package pQuery
  6. */
  7. /**
  8. * @todo Documentation
  9. */
  10. class pQueryArray extends pQuery implements pQueryExtension {
  11. static $accepts = array('array');
  12. /**
  13. * Get the value of an array alement at the given index.
  14. *
  15. * @param int|string $index The index to get the element of.
  16. * @returns mixed The array value at the given index if it exists, or NULL otherwise.
  17. */
  18. function get($index) {
  19. return isset($this->variable[$index]) ? $this->variable[$index] : null;
  20. }
  21. /**
  22. * Check if the array is empty.
  23. *
  24. * @returns bool Whether the array is empty.
  25. */
  26. function is_empty() {
  27. return !$this->count();
  28. }
  29. /**
  30. * Get the number of elementsin the array.
  31. *
  32. * @returns int The number of elements.
  33. */
  34. function count() {
  35. return count($this->variable);
  36. }
  37. /**
  38. * Reverse the array.
  39. *
  40. * @returns pQueryArray The current object.
  41. */
  42. function reverse() {
  43. $this->variable = array_reverse($this->variable);
  44. return $this;
  45. }
  46. /**
  47. * Execute an existing array function on the array.
  48. *
  49. * @var $method string A (part of the) function name to execute.
  50. * @var method $args Additional arguments to pass to the called function.
  51. * @returns mixed Either the current object, or the return value
  52. * of the called array function.
  53. */
  54. function __call($method, $args) {
  55. $function = 'array_'.$method;
  56. if( function_exists($function) ) {
  57. array_unshift($args, &$this->variable);
  58. return call_user_func_array($function, $args);
  59. }
  60. if( in_array($method, array('shuffle', 'sort')) ) {
  61. $method($this->variable);
  62. return $this;
  63. }
  64. return self::error('Plugin "%s" has no method "%s".', __CLASS__, $method);
  65. }
  66. }
  67. /**
  68. * Shortcut constructor for {@link pQueryArray}.
  69. *
  70. * @returns pQueryArray A new pQueryArray instance.
  71. */
  72. function _arr($array) {
  73. return pQuery::create('array', $array);
  74. }
  75. pQuery::extend('pQueryArray', 'array');
  76. ?>