Changeset 893
- Timestamp:
- 09/05/07 00:42:55 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/org/simpletest/extensions/streamwrapper/directory.php
r892 r893 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> 6 * @package SimpleTest7 * @subpackage StreamWrapper6 * @package SimpleTest 7 * @subpackage StreamWrapper 8 8 */ 9 9 require_once dirname(__FILE__) . '/abstractcontent.php'; … … 12 12 * Directory container. 13 13 * 14 * @package SimpleTest15 * @subpackage StreamWrapper14 * @package SimpleTest 15 * @subpackage StreamWrapper 16 16 */ 17 class SimpleTestStreamDirectory extends SimpleTestStreamAbstractContent 17 class SimpleTestStreamDirectory extends SimpleTestStreamAbstractContent implements Iterator 18 18 { 19 19 /** … … 23 23 */ 24 24 protected $children = array(); 25 /**26 * pointer for iterating27 *28 * @var int29 */30 protected $pointer = 0;31 25 32 26 /** … … 37 31 public function __construct($name) 38 32 { 33 if (strstr($name, '/') !== false) { 34 throw new SimpleTestStreamException('Directory name can not contain /.'); 35 } 36 39 37 $this->type = SimpleTestStreamContent::TYPE_DIR; 40 38 parent::__construct($name); 39 } 40 41 /** 42 * factory method to create a directory structure for a given string 43 * 44 * @param string $dir 45 * @return SimpleTestStreamDirectory 46 */ 47 public static function create($dir) 48 { 49 if ('/' === $dir{0}) { 50 $dir = substr($dir, 1); 51 } 52 53 $dirParts = explode('/', $dir); 54 $content = new SimpleTestStreamDirectory($dirParts[0]); 55 if (count($dirParts) > 1) { 56 $content->addChild(self::create(self::getChildName($dir, $dirParts[0]))); 57 } 58 59 return $content; 60 } 61 62 /** 63 * renames the content 64 * 65 * @param string $newName 66 */ 67 public function rename($newName) 68 { 69 if (strstr($newName, '/') !== false) { 70 throw new SimpleTestStreamException('Directory name can not contain /.'); 71 } 72 73 parent::rename($newName); 41 74 } 42 75 … … 79 112 public function hasChild($name) 80 113 { 81 if ($this->appliesTo($childName) === false) { 82 return false; 83 } 84 85 if ($this->name === $name) { 86 return true; 87 } 88 89 $childName = $this->getChildName($name); 90 foreach ($this->children as $child) { 91 if ($child->appliesTo($childName) == false) { 92 continue; 93 } 94 95 if ($child->getName() === $childName || $child->hasChild($childName) === true) { 96 return true; 97 } 98 } 99 100 return false; 101 } 102 103 /** 104 * returns the child with the given name 105 * 106 * @param string $name 107 * @return SimpleTestStreamContent 108 */ 109 public function getChild($name) 110 { 111 if ($this->name === $name) { 112 return $this; 113 } 114 115 $childName = $this->getChildName($name); 114 if ($this->appliesTo($name) === true) { 115 $childName = self::getChildName($name, $this->name); 116 } else { 117 $childName = $name; 118 } 119 116 120 foreach ($this->children as $child) { 117 121 if ($child->appliesTo($childName) === false) { … … 119 123 } 120 124 125 if ($child->getName() === $childName || $child->hasChild($childName) === true) { 126 return true; 127 } 128 } 129 130 return false; 131 } 132 133 /** 134 * returns the child with the given name 135 * 136 * @param string $name 137 * @return SimpleTestStreamContent 138 */ 139 public function getChild($name) 140 { 141 if ($this->appliesTo($name) === true) { 142 $childName = self::getChildName($name, $this->name); 143 } else { 144 $childName = $name; 145 } 146 147 foreach ($this->children as $child) { 148 if ($child->appliesTo($childName) === false) { 149 continue; 150 } 151 121 152 if ($child->getName() === $childName) { 122 return $c ontent;123 } 124 125 if ($child->hasC ontent($childName) === true) {126 return $child->getC ontent($childName);127 } 128 } 129 130 throw new SimpleTestStreamException('Content ' . $name . ' is not available.');153 return $child; 154 } 155 156 if ($child->hasChild($childName) === true) { 157 return $child->getChild($childName); 158 } 159 } 160 161 return null; 131 162 } 132 163 … … 137 168 * @return string 138 169 */ 139 protected function getChildName($name)140 { 141 return substr($name, strlen($ this->name));142 } 143 144 /** 145 * returns a list of child sfor this directory170 protected static function getChildName($name, $ownName) 171 { 172 return substr($name, strlen($ownName) + 1); 173 } 174 175 /** 176 * returns a list of children for this directory 146 177 * 147 178 * @return array<SimpleTestStreamContent> 148 179 */ 149 public function getChild s()180 public function getChildren() 150 181 { 151 182 return $this->children; … … 161 192 162 193 /** 163 * returns the next child194 * returns the current child 164 195 * 165 196 * @return SimpleTestStreamContent 166 197 */ 198 public function current() 199 { 200 $child = current($this->children); 201 if (false === $child) { 202 return null; 203 } 204 205 return $child; 206 } 207 208 /** 209 * returns the name of the current child 210 * 211 * @return string 212 */ 213 public function key() 214 { 215 $child = current($this->children); 216 if (false === $child) { 217 return null; 218 } 219 220 return $child->getName(); 221 } 222 223 /** 224 * iterates to next child 225 */ 167 226 public function next() 168 227 { 169 $next = next($this->children); 170 if (false === $next) { 171 return null; 172 } 173 174 return $next; 228 next($this->children); 229 } 230 231 /** 232 * checks if the current value is valid 233 * 234 * @return bool 235 */ 236 public function valid() 237 { 238 return (false !== current($this->children)); 175 239 } 176 240 }
