95% of 245OPs |
100% of 44Lines |
87% of 30Branches |
86% of 22Paths |
| # | |
|---|---|
| 1 |
<?php |
| 2 |
|
| 3 |
namespace mageekguy\atoum\iterators\recursives\directory; |
| 4 |
|
| 5 |
use |
| 6 |
mageekguy\atoum, |
| 7 |
mageekguy\atoum\exceptions, |
| 8 |
mageekguy\atoum\iterators\filters |
| 9 |
; |
| 10 |
|
| 11 |
class factory implements \iteratorAggregate |
| 12 |
{
|
| 13 |
protected $dotFilterFactory = null; |
| 14 |
protected $iteratorFactory = null; |
| 15 |
protected $acceptDots = false; |
| 16 |
protected $extensionFilterFactory = null; |
| 17 |
protected $acceptedExtensions = array('php');
|
| 18 |
|
| 19 |
public function __construct(\closure $iteratorFactory = null, \closure $dotFilterFactory = null, \closure $extensionFilterFactory = null)100% |
| 20 |
{
|
| 21 |
$this |
| 22 |
->setIteratorFactory($iteratorFactory) |
| 23 |
->setDotFilterFactory($dotFilterFactory) |
| 24 |
->setExtensionFilterFactory($extensionFilterFactory) |
| 25 |
; |
| 26 |
} |
| 27 |
|
| 28 |
public function setIteratorFactory(\closure $factory = null)100% |
| 29 |
{
|
| 30 |
$this->iteratorFactory = $factory ?: function($path) { return new \recursiveDirectoryIterator($path); };
|
| 31 |
|
| 32 |
return $this; |
| 33 |
} |
| 34 |
|
| 35 |
public function getIteratorFactory()100% |
| 36 |
{
|
| 37 |
return $this->iteratorFactory; |
| 38 |
} |
| 39 |
|
| 40 |
public function setDotFilterFactory(\closure $factory = null)100% |
| 41 |
{
|
| 42 |
$this->dotFilterFactory = $factory ?: function($iterator) { return new filters\recursives\dot($iterator); };
|
| 43 |
|
| 44 |
return $this; |
| 45 |
} |
| 46 |
|
| 47 |
public function getDotFilterFactory()100% |
| 48 |
{
|
| 49 |
return $this->dotFilterFactory; |
| 50 |
} |
| 51 |
|
| 52 |
public function setExtensionFilterFactory(\closure $factory = null)100% |
| 53 |
{
|
| 54 |
$this->extensionFilterFactory = $factory ?: function($iterator, $extensions) { return new filters\recursives\extension($iterator, $extensions); };
|
| 55 |
|
| 56 |
return $this; |
| 57 |
} |
| 58 |
|
| 59 |
public function getExtensionFilterFactory()100% |
| 60 |
{
|
| 61 |
return $this->extensionFilterFactory; |
| 62 |
} |
| 63 |
|
| 64 |
public function getIterator($path)100% |
| 65 |
{
|
| 66 |
$iterator = call_user_func($this->iteratorFactory, $path); |
| 67 |
|
| 68 |
if ($this->acceptDots === false) |
| 69 |
{
|
| 70 |
$iterator = call_user_func($this->dotFilterFactory, $iterator); |
| 71 |
} |
| 72 |
|
| 73 |
if (sizeof($this->acceptedExtensions) > 0) |
| 74 |
{
|
| 75 |
$iterator = call_user_func($this->extensionFilterFactory, $iterator, $this->acceptedExtensions); |
| 76 |
} |
| 77 |
|
| 78 |
return $iterator; |
| 79 |
} |
| 80 |
|
| 81 |
public function dotsAreAccepted()100% |
| 82 |
{
|
| 83 |
return $this->acceptDots; |
| 84 |
} |
| 85 |
|
| 86 |
public function acceptDots()100% |
| 87 |
{
|
| 88 |
$this->acceptDots = true; |
| 89 |
|
| 90 |
return $this; |
| 91 |
} |
| 92 |
|
| 93 |
public function refuseDots()100% |
| 94 |
{
|
| 95 |
$this->acceptDots = false; |
| 96 |
|
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
public function getAcceptedExtensions()100% |
| 101 |
{
|
| 102 |
return $this->acceptedExtensions; |
| 103 |
} |
| 104 |
|
| 105 |
public function acceptExtensions(array $extensions)100% |
| 106 |
{
|
| 107 |
$this->acceptedExtensions = array(); |
| 108 |
|
| 109 |
foreach ($extensions as $extension) |
| 110 |
{
|
| 111 |
$this->acceptedExtensions[] = self::cleanExtension($extension); |
| 112 |
} |
| 113 |
|
| 114 |
return $this; |
| 115 |
} |
| 116 |
|
| 117 |
public function acceptAllExtensions()100% |
| 118 |
{
|
| 119 |
return $this->acceptExtensions(array()); |
| 120 |
} |
| 121 |
|
| 122 |
public function refuseExtension($extension)100% |
| 123 |
{
|
| 124 |
$key = array_search(self::cleanExtension($extension), $this->acceptedExtensions); |
| 125 |
|
| 126 |
if ($key !== false) |
| 127 |
{
|
| 128 |
unset($this->acceptedExtensions[$key]); |
| 129 |
|
| 130 |
$this->acceptedExtensions = array_values($this->acceptedExtensions); |
| 131 |
} |
| 132 |
|
| 133 |
return $this; |
| 134 |
} |
| 135 |
|
| 136 |
protected static function cleanExtension($extension)100% |
| 137 |
{
|
| 138 |
return trim($extension, '.'); |
| 139 |
} |
| 140 |
} |