Textpattern | PHP Cross Reference | Content Management Systems |
Description: Recursive filter iterator
1 <?php 2 3 /* 4 * Textpattern Content Management System 5 * https://textpattern.com/ 6 * 7 * Copyright (C) 2020 The Textpattern Development Team 8 * 9 * This file is part of Textpattern. 10 * 11 * Textpattern is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU General Public License 13 * as published by the Free Software Foundation, version 2. 14 * 15 * Textpattern is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with Textpattern. If not, see <https://www.gnu.org/licenses/>. 22 */ 23 24 /** 25 * Recursive filter iterator 26 * 27 * <code> 28 * $files = \Txp::get('Textpattern\Iterator\RecDirIterator', $root); 29 * $filter = \Txp::get('Textpattern\Iterator\RecFilterIterator', $files)->setNameIn($nameIn); 30 * $filteredFiles = \Txp::get('Textpattern\Iterator\RecIteratorIterator', $filter); 31 * $filteredFiles->setMaxDepth($maxDepth); 32 * 33 * foreach ($filteredFiles as $file) { 34 * echo $file->getPathname(); 35 * } 36 * </code> 37 * 38 * @since 4.7.0 39 * @package Iterator 40 */ 41 42 namespace Textpattern\Iterator { 43 44 class RecFilterIterator extends \RecursiveFilterIterator 45 { 46 protected $filter; 47 48 /** 49 * Constructor 50 * 51 * @param object RecDirIterator $iterator Instance of RecDirIterator. 52 * @param mixed $filter Array of filenames or regEx as a string. 53 */ 54 55 public function __construct(RecDirIterator $iterator, $filter) 56 { 57 parent::__construct($iterator); 58 59 $this->setFilter($filter); 60 } 61 62 /** 63 * {@inheritdoc} 64 * 65 * Get Children and pass the filter to them. 66 */ 67 68 public function getChildren() 69 { 70 return new self($this->getInnerIterator()->getChildren(), $this->getFilter()); 71 } 72 73 /** 74 * {@inheritdoc} 75 */ 76 77 public function accept() 78 { 79 return $this->isDir() || 80 $this->isReadable() && ($this->isFile() || $this->isLink()) && $this->isValid(); 81 } 82 83 /** 84 * Whether the filename is valid according to the provided $filter property value. 85 * 86 * @return bool FALSE on error 87 */ 88 89 public function isValid() 90 { 91 $filter = $this->getFilter(); 92 $filename = $this->getFilename(); 93 94 return is_array($filter) && in_array($filename, $filter) || 95 !is_array($filter) && preg_match($filter, $filename); 96 } 97 98 /** 99 * $filter property setter 100 * 101 * @param mixed $filter Array of filenames or regEx as a string. 102 * @return object $this. 103 */ 104 105 public function setFilter($info) 106 { 107 $this->filter = $info; 108 109 return $this; 110 } 111 112 /** 113 * $names property getter 114 * 115 * @return mixed $filter Array of filenames or regEx as a string. 116 */ 117 118 public function getFilter() 119 { 120 return $this->filter; 121 } 122 } 123 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
title