diff options
Diffstat (limited to 'ext/spl/examples')
| -rwxr-xr-x | ext/spl/examples/callbackfilteriterator.inc | 122 | ||||
| -rwxr-xr-x | ext/spl/examples/directoryfilterdots.inc | 10 | ||||
| -rwxr-xr-x | ext/spl/examples/dualiterator.inc | 5 | ||||
| -rwxr-xr-x | ext/spl/examples/phar_from_dir.php | 50 | ||||
| -rwxr-xr-x | ext/spl/examples/recursivecomparedualiterator.inc | 69 | ||||
| -rwxr-xr-x | ext/spl/examples/recursivedualiterator.inc | 2 | ||||
| -rwxr-xr-x | ext/spl/examples/tests/dualiterator_001.phpt | 1 |
7 files changed, 251 insertions, 8 deletions
diff --git a/ext/spl/examples/callbackfilteriterator.inc b/ext/spl/examples/callbackfilteriterator.inc new file mode 100755 index 000000000..51757012e --- /dev/null +++ b/ext/spl/examples/callbackfilteriterator.inc @@ -0,0 +1,122 @@ +<?php + +/** @file callbackfilteriterator.inc + * @ingroup Examples + * @brief class CallbackFilterIterator + * @author Marcus Boerger + * @author Kevin McArthur + * @date 2006 - 2006 + * + * SPL - Standard PHP Library + */ + +/** @ingroup Examples + * @brief A non abstract FiletrIterator that uses a callback foreach element + * @author Marcus Boerger + * @author Kevin McArthur + * @version 1.0 + */ +class CallbackFilterIterator extends FilterIterator +{ + const USE_FALSE = 0; /**< mode: accept no elements, no callback */ + const USE_TRUE = 1; /**< mode: accept all elements, no callback */ + const USE_VALUE = 2; /**< mode: pass value to callback */ + const USE_KEY = 3; /**< mode: pass key to callback */ + const USE_BOTH = 4; /**< mode: pass value and key to callback */ + + const REPLACE = 0x00000001; /**< flag: pass key/value by reference */ + + private $callback; /**< callback to use */ + private $mode; /**< mode any of USE_VALUE, USE_KEY, USE_BOTH */ + private $flags; /**< flags (REPLACE) */ + private $key; /**< key value */ + private $current; /**< current value */ + + /** Construct a CallbackFilterIterator + * + * @param it inner iterator (iterator to filter) + * @param callback callback function + * @param mode any of USE_VALUE, USE_KEY, USE_BOTH + * @param flags any of 0, REPLACE + */ + public function __construct(Iterator $it, $callback, $mode = self::USE_VALUE, $flags = 0) + { + parent::__construct($it); + $this->callback = $callback; + $this->mode = $mode; + $this->flags = $flags; + } + + /** Call the filter callback + * @return result of filter callback + */ + public function accept() + { + $this->key = parent::key(); + $this->current = parent::current(); + + switch($this->mode) { + default: + case self::USE_FALSE; + return false; + case self::USE_TRUE: + return true; + case self::USE_VALUE: + if($this->flags & self::REPLACE) { + return (bool) call_user_func($this->callback, &$this->current); + } else { + return (bool) call_user_func($this->callback, $this->current); + } + case self::USE_KEY: + if($this->flags & self::REPLACE) { + return (bool) call_user_func($this->callback, &$this->key); + } else { + return (bool) call_user_func($this->callback, $this->key); + } + case SELF::USE_BOTH: + if($this->flags & self::REPLACE) { + return (bool) call_user_func($this->callback, &$this->key, &$this->current); + } else { + return (bool) call_user_func($this->callback, $this->key, $this->current); + } + } + } + + /** @return current key value */ + function key() + { + return $this->key; + } + + /** @return current value */ + function current() + { + return $this->current; + } + + /** @return operation mode */ + function getMode() + { + return $this->mode; + } + + /** @param $mode set new mode, @see mode */ + function setMode($mode) + { + $this->mode = $mode; + } + + /** @return operation flags */ + function getFlags() + { + return $this->flags; + } + + /** @param $flags set new flags, @see flags */ + function setFlags($flags) + { + $this->flags = $flags; + } +} + +?>
\ No newline at end of file diff --git a/ext/spl/examples/directoryfilterdots.inc b/ext/spl/examples/directoryfilterdots.inc index 740c2303c..aa4b6ddd6 100755 --- a/ext/spl/examples/directoryfilterdots.inc +++ b/ext/spl/examples/directoryfilterdots.inc @@ -4,7 +4,7 @@ * @ingroup Examples
* @brief class DirectoryFilterDots
* @author Marcus Boerger
- * @date 2003 - 2005
+ * @date 2003 - 2006
*
* SPL - Standard PHP Library
*/
@@ -12,9 +12,9 @@ /** @ingroup Examples
* @brief A filtered DirectoryIterator
* @author Marcus Boerger
- * @version 1.1
+ * @version 1.2
*
- * This Iteraotr takes a pathname from which it creates a DirectoryIterator
+ * This Iterator takes a pathname from which it creates a RecursiveDirectoryIterator
* and makes it recursive. Further more it filters the entries '.' and '..'.
*/
class DirectoryFilterDots extends RecursiveFilterIterator
@@ -24,7 +24,7 @@ class DirectoryFilterDots extends RecursiveFilterIterator */
function __construct($path)
{
- parent::__construct(new DirectoryIterator($path));
+ parent::__construct(new RecursiveDirectoryIterator($path));
}
/** @return whether the current entry is neither '.' nor '..'
@@ -42,4 +42,4 @@ class DirectoryFilterDots extends RecursiveFilterIterator }
}
-?>
\ No newline at end of file +?>
diff --git a/ext/spl/examples/dualiterator.inc b/ext/spl/examples/dualiterator.inc index 544034856..9d14328d7 100755 --- a/ext/spl/examples/dualiterator.inc +++ b/ext/spl/examples/dualiterator.inc @@ -12,7 +12,7 @@ /** @ingroup Examples * @brief Synchronous iteration over two iterators * @author Marcus Boerger - * @version 1.1 + * @version 1.3 */ class DualIterator implements Iterator { @@ -174,6 +174,7 @@ class DualIterator implements Iterator { $it = new RecursiveDualIterator($lhs, $rhs, self::CURRENT_0 | self::KEY_0); + $it = new RecursiveCompareDualIterator($it); } else { @@ -187,7 +188,7 @@ class DualIterator implements Iterator if ($identical) { - foreach(new RecursiveIteratorIterator($it) as $n) + foreach($it as $n) { if (!$it->areIdentical()) { diff --git a/ext/spl/examples/phar_from_dir.php b/ext/spl/examples/phar_from_dir.php new file mode 100755 index 000000000..2ee15ca1e --- /dev/null +++ b/ext/spl/examples/phar_from_dir.php @@ -0,0 +1,50 @@ +<?php + +/** @file phar_from_dir.php + * @brief Create phar archive from directory + * @ingroup examples + * @author Marcus Boerger + * @date 2003 - 2007 + * @version 1.0 + * + * Usage: php phar_create_from_dir.php \<archive\> \<directory\> [\<regex\>] + * + * Create phar archive \<archive\> using entries from \<directory\> that + * optionally match \<regex\>. + */ + +if ($argc < 3) +{ + echo <<<EOF +php phar_from_dir.php archive directory [regex] + +Packs files in a given directory into a phar archive. + +archive name of the archive to create +directory input directory to pack +regex optional expression to match files in directory + +EOF; + exit(1); +} + +$phar = new Phar($argv[1], 0, 'newphar'); + +$dir = new RecursiveDirectoryIterator($argv[2]); +$dir = new RecursiveIteratorIterator($dir); +if ($argc > 3) +{ + $dir = new RegexIterator($dir, '/'.$argv[3].'/'); +} + +$phar->begin(); + +foreach($dir as $file) +{ + echo "$file\n"; + copy($file, "phar://newphar/$file"); +} + +$phar->commit(); + +?>
\ No newline at end of file diff --git a/ext/spl/examples/recursivecomparedualiterator.inc b/ext/spl/examples/recursivecomparedualiterator.inc new file mode 100755 index 000000000..75265c1d5 --- /dev/null +++ b/ext/spl/examples/recursivecomparedualiterator.inc @@ -0,0 +1,69 @@ +<?php + +/** @file recursivecomparedualiterator.inc + * @ingroup Examples + * @brief class DualIterator + * @author Marcus Boerger + * @date 2003 - 2006 + * + * SPL - Standard PHP Library + */ + +/** @ingroup Examples + * @brief Recursive comparison iterator for a RecursiveDualIterator + * @author Marcus Boerger + * @version 1.0 + */ +class RecursiveCompareDualIterator extends RecursiveIteratorIterator +{ + /** Used to keep end of recursion equality. That is en leaving a nesting + * level we need to check whether both child iterators are at their end. + */ + protected $equal = false; + + /** Construct from RecursiveDualIterator + * + * @param $it RecursiveDualIterator + * @param $mode should be LEAVES_ONLY + * @param $flags should be 0 + */ + function __construct(RecursiveDualIterator $it, $mode = self::LEAVES_ONLY, $flags = 0) + { + parent::__construct($it); + } + + /** Rewind iteration andcomparison process. Starting with $equal = true. + */ + function rewind() + { + $this->equal = true; + parent::rewind(); + } + + /** Calculate $equal + * @see $equal + */ + function endChildren() + { + $this->equal &= !$this->getInnerIterator()->getLHS()->valid() + && !$this->getInnerIterator()->getRHS()->valid(); + } + + /** @return whether both inner iterators are valid and have identical + * current and key values or both are non valid. + */ + function areIdentical() + { + return $this->equal && $this->getInnerIterator()->areIdentical(); + } + + /** @return whether both inner iterators are valid and have equal current + * and key values or both are non valid. + */ + function areEqual() + { + return $this->equal && $this->getInnerIterator()->areEqual(); + } +} + +?> diff --git a/ext/spl/examples/recursivedualiterator.inc b/ext/spl/examples/recursivedualiterator.inc index 702e0cd74..cfa3bccbb 100755 --- a/ext/spl/examples/recursivedualiterator.inc +++ b/ext/spl/examples/recursivedualiterator.inc @@ -18,7 +18,7 @@ class RecursiveDualIterator extends DualIterator implements RecursiveIterator { private $ref; - /** construct iterator from two iterators + /** construct iterator from two RecursiveIterator instances * * @param lhs Left Hand Side Iterator * @param rhs Right Hand Side Iterator diff --git a/ext/spl/examples/tests/dualiterator_001.phpt b/ext/spl/examples/tests/dualiterator_001.phpt index 5577c4dc1..9150d76ae 100755 --- a/ext/spl/examples/tests/dualiterator_001.phpt +++ b/ext/spl/examples/tests/dualiterator_001.phpt @@ -33,6 +33,7 @@ test(array(1,array(21,22),3), array(1,array(21,"22"),3), true); ?> ===DONE=== +<?php exit(0); ?> --EXPECT-- bool(true) bool(false) |
