Created
September 24, 2018 13:25
-
-
Save ijortengab/3dd72d6348c933e277d92e4276d3ada7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace IjorTengab; | |
class Directory | |
{ | |
protected $path; | |
protected $list = array(); | |
protected $filter_dir_only = false; | |
protected $has_listing = false; | |
public function __construct($path) | |
{ | |
$this->path = $path; | |
} | |
public static function init($path) | |
{ | |
$object = new static($path); | |
return $object; | |
} | |
public function filterDirOnly() | |
{ | |
$this->filter_dir_only = true; | |
return $this; | |
} | |
public function refresh() | |
{ | |
$this->has_listing = false; | |
$this->filter_dir_only = false; | |
$this->list = array(); | |
} | |
public function list($regex = null) | |
{ | |
if ($this->has_listing) { | |
return; | |
} | |
$this->has_listing = true; | |
$list = scandir($this->path); | |
$this->list = array_filter($list, function ($value) use ($regex) { | |
switch ($value) { | |
case '.': | |
case '..': | |
return false; | |
} | |
if ($this->filter_dir_only && !is_dir($this->path . DIRECTORY_SEPARATOR . $value)){ | |
return false; | |
} | |
if ($regex === null) { | |
return true; | |
} | |
return preg_match($regex, $value); | |
}); | |
unset($list); | |
return $this; | |
} | |
public function isEmpty() | |
{ | |
if ($this->has_listing === false) { | |
$this->list(); | |
} | |
return count($this->list) === 0 ? true : false; | |
} | |
public function each($callback) | |
{ | |
foreach ($this->list as $value) { | |
call_user_func($callback, $this->path . DIRECTORY_SEPARATOR . $value); | |
} | |
} | |
} | |
$path = 'W:\'; | |
$regex = '/^\d{8}\s{1}/'; | |
$fita = Directory::init($path)->filterDirOnly()->list($regex); | |
$fita->each(function($full_path){ | |
$each = Directory::init($full_path); | |
if ($each->isEmpty()) { | |
echo $full_path . PHP_EOL; | |
// $from = $full_path; | |
// $to = $full_path . ' - empty'; | |
// rename($from, $to); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment