Changeset View
Changeset View
Standalone View
Standalone View
src/IO/File.php
<?php | <?php | ||||
declare(strict_types=1); | |||||
namespace Keruald\OmniTools\IO; | namespace Keruald\OmniTools\IO; | ||||
class File { | class File { | ||||
/** | /** | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
private $filename; | private $path; | ||||
/// | /// | ||||
/// Constructors | /// Constructors | ||||
/// | /// | ||||
public static function from (string $filename) : self { | public function __construct (string $path = null) { | ||||
$instance = new self; | $this->path = $path; | ||||
$instance->filename = $filename; | } | ||||
return $instance; | public static function from (string $path) : self { | ||||
return new self($path); | |||||
} | } | ||||
/// | /// | ||||
/// Getters and setters | /// Getters and setters | ||||
/// | /// | ||||
public function getFilename () : string { | public function getPath () : string { | ||||
return $this->filename; | return $this->path; | ||||
} | } | ||||
public function setFilename (string $filename) : self { | public function setPath (string $path) : self { | ||||
$this->filename = $filename; | $this->path = $path; | ||||
return $this; | return $this; | ||||
} | } | ||||
/// | /// | ||||
/// File properties methods | /// File properties methods | ||||
/// | /// | ||||
public function exists () : bool { | public function exists () : bool { | ||||
return file_exists($this->filename); | return file_exists($this->path); | ||||
} | } | ||||
public function isReadable () : bool { | public function isReadable () : bool { | ||||
return is_readable($this->filename); | return is_readable($this->path); | ||||
} | |||||
public function getPathInfo () : array { | |||||
return pathinfo($this->path); | |||||
} | |||||
public function getDirectory () : string { | |||||
return pathinfo($this->path, PATHINFO_DIRNAME); | |||||
} | |||||
public function getFileName () : string { | |||||
return pathinfo($this->path, PATHINFO_BASENAME); | |||||
} | |||||
public function getFileNameWithoutExtension () : string { | |||||
return pathinfo($this->path, PATHINFO_FILENAME); | |||||
} | |||||
public function getExtension () : string { | |||||
return pathinfo($this->path, PATHINFO_EXTENSION); | |||||
} | } | ||||
/// | /// | ||||
/// Include methods | /// Include methods | ||||
/// | /// | ||||
public function tryInclude () : bool { | public function tryInclude () : bool { | ||||
if (!$this->canBeIncluded()) { | if (!$this->canBeIncluded()) { | ||||
return false; | return false; | ||||
} | } | ||||
include($this->filename); | include($this->path); | ||||
return true; | return true; | ||||
} | } | ||||
public function canBeIncluded () : bool { | public function canBeIncluded () : bool { | ||||
return $this->exists() &&$this->isReadable(); | return $this->exists() &&$this->isReadable(); | ||||
} | } | ||||
} | } |
Nasqueron DevCentral · If it had been much bigger the moon would have had a core of ice. · Powered by Phabricator