pquery.array.php 1.8 KB

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