1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * Filter out the files that do not respect a given predicat
6 *
7 * PHP versions 4 and 5
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA
22 *
23 * @category   File Formats
24 * @package    File_Archive
25 * @author     Vincent Lascaux <vincentlascaux@php.net>
26 * @copyright  1997-2005 The PHP Group
27 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL
28 * @version    CVS: $Id: Filter.php,v 1.10 2005/07/09 12:54:35 vincentlascaux Exp $
29 * @link       http://pear.php.net/package/File_Archive
30 */
31
32require_once "File/Archive/Reader/Relay.php";
33
34/**
35 * Filter out the files that do not respect a given predicat
36 */
37class File_Archive_Reader_Filter extends File_Archive_Reader_Relay
38{
39    /**
40     * @var File_Archive_Reader_Predicat
41     * @access private
42     */
43    var $predicate;
44
45    /**
46     * $source is the reader to filter
47     */
48    function File_Archive_Reader_Filter($predicate, &$source)
49    {
50        parent::File_Archive_Reader_Relay($source);
51        $this->predicate = $predicate;
52    }
53
54    /**
55     * @see File_Archive_Reader::next()
56     */
57    function next()
58    {
59        do {
60            $error = $this->source->next();
61            if ($error !== true) {
62                return $error;
63            }
64        } while (!$this->predicate->isTrue($this->source));
65        return true;
66    }
67
68    /**
69     * @see File_Archive_Reader::select()
70     */
71    function select($filename, $close = true)
72    {
73        if ($close) {
74            $error = $this->close();
75            if (PEAR::isError($error)) {
76                return $error;
77            }
78        }
79
80        do {
81            $error = $this->source->select($filename, false);
82            if ($error !== true) {
83                return $error;
84            }
85        } while (!$this->predicate->isTrue($this->source));
86        return true;
87    }
88}
89
90?>