Page MenuHomeDevCentral

Directory.php
No OneTemporary

Directory.php

<?php
namespace Keruald\OmniTools\IO;
use Keruald\OmniTools\Collections\Vector;
class Directory {
///
/// Constructors
///
public function __construct (
private string $path,
) {}
public static function fromPathFragments (iterable $fragments) : self {
$path = Vector::from($fragments)
->implode(DIRECTORY_SEPARATOR)
->getValue();
return new self($path);
}
///
/// Getters and setters
///
public function getPath () : string {
return $this->path;
}
public function setPath (string $path) : self {
$this->path = $path;
return $this;
}
///
/// Directory properties methods
///
public function exists () : bool {
return is_dir($this->path);
}
public function isReadable () : bool {
return is_readable($this->path);
}
public function isWritable () : bool {
return is_writable($this->path);
}
/**
* @return array<string, string>
*/
public function getPathInfo () : array {
return pathinfo($this->path);
}
public function getParentDirectory () : string {
return pathinfo($this->path, PATHINFO_DIRNAME);
}
public function getDirectoryName () : string {
return pathinfo($this->path, PATHINFO_BASENAME);
}
///
/// Search files
///
/**
* Gets files in the directory matching a specific pattern,
* using the PHP glob function.
*
* @return File[]
*/
public function glob (string $pattern) : array {
return array_map(
function ($file) {
return new File($file);
}, glob("$this->path/$pattern")
);
}
/**
* Count files in the directory matching a specific pattern.
* Non-recursive.
*
* @param string $pattern Glob pattern to match files, defaults to "*"
* @return int
*/
public function countFiles (string $pattern = "*") : int {
$matches = $this->glob($pattern);
$filter = fn (File $file) => is_file($file->getPath());
return count(array_filter($matches, $filter));
}
/**
* @return Directory[]
*/
public function getSubdirectories () : array {
return array_map(
function ($dir) {
return new Directory($dir);
}, glob("$this->path/*", GLOB_ONLYDIR)
);
}
///
/// Generate File from path
///
public function getFile (string $filename) : File {
return new File($this->path . DIRECTORY_SEPARATOR . $filename);
}
}

File Metadata

Mime Type
text/x-php
Expires
Fri, Nov 6, 07:37 (2 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4227539
Default Alt Text
Directory.php (2 KB)

Event Timeline