SPL-StandardPHPLibrary
callbackfilteriterator.inc
Go to the documentation of this file.
1 <?php
2 
20 {
21  const USE_FALSE = 0;
22  const USE_TRUE = 1;
23  const USE_VALUE = 2;
24  const USE_KEY = 3;
25  const USE_BOTH = 4;
27  const REPLACE = 0x00000001;
29  private $callback;
30  private $mode;
31  private $flags;
32  private $key;
33  private $current;
42  public function __construct(Iterator $it, $callback, $mode = self::USE_VALUE, $flags = 0)
43  {
44  parent::__construct($it);
45  $this->callback = $callback;
46  $this->mode = $mode;
47  $this->flags = $flags;
48  }
49 
53  public function accept()
54  {
55  $this->key = parent::key();
56  $this->current = parent::current();
57 
58  switch($this->mode) {
59  default:
60  case self::USE_FALSE;
61  return false;
62  case self::USE_TRUE:
63  return true;
64  case self::USE_VALUE:
65  if($this->flags & self::REPLACE) {
66  return (bool) call_user_func($this->callback, &$this->current);
67  } else {
68  return (bool) call_user_func($this->callback, $this->current);
69  }
70  case self::USE_KEY:
71  if($this->flags & self::REPLACE) {
72  return (bool) call_user_func($this->callback, &$this->key);
73  } else {
74  return (bool) call_user_func($this->callback, $this->key);
75  }
76  case SELF::USE_BOTH:
77  if($this->flags & self::REPLACE) {
78  return (bool) call_user_func($this->callback, &$this->key, &$this->current);
79  } else {
80  return (bool) call_user_func($this->callback, $this->key, $this->current);
81  }
82  }
83  }
84 
86  function key()
87  {
88  return $this->key;
89  }
90 
92  function current()
93  {
94  return $this->current;
95  }
96 
98  function getMode()
99  {
100  return $this->mode;
101  }
102 
104  function setMode($mode)
105  {
106  $this->mode = $mode;
107  }
108 
110  function getFlags()
111  {
112  return $this->flags;
113  }
114 
116  function setFlags($flags)
117  {
118  $this->flags = $flags;
119  }
120 }
121 
122 ?>
const USE_FALSE
mode: accept no elements, no callback
const USE_KEY
mode: pass key to callback
__construct(Iterator $it, $callback, $mode=self::USE_VALUE, $flags=0)
Construct a CallbackFilterIterator.
const USE_BOTH
mode: pass value and key to callback
const USE_VALUE
mode: pass value to callback
$mode
mode any of USE_VALUE, USE_KEY, USE_BOTH
A non abstract FiletrIterator that uses a callback foreach element.
Abstract filter for iterators.
accept()
Call the filter callback.
Basic iterator.
Definition: spl.php:549
const REPLACE
flag: pass key/value by reference
const USE_TRUE
mode: accept all elements, no callback