/** * @param string $dir The directory to hash * @throws \InvalidArgumentException If the directory is not readable */ public function __construct(string $dir) { $this->dir = Fs::dir($dir); if (!$this->dir->test(Fs::EXISTS | Fs::READABLE)) { throw new \InvalidArgumentException(sprintf("failed to hash directory; %s", $this->dir->getError())); } }
/** * {@inheritdoc} */ public function validate(&$value, \sndsgd\form\Validator $validator = null) : bool { $file = \sndsgd\Fs::file($value); $extension = strtolower($file->getExtension()); if (!in_array($extension, $this->extensions)) { return false; } return true; }
public function searchDir(string $dir, bool $recursive = false) : LocatorInterface { foreach ($this->getIterator($dir, $recursive) as $entity) { $entity = \sndsgd\Fs::createFromSplFileInfo($entity); $path = $entity->getPath(); if (isset($this->results[$path]) || $this->filter && !call_user_func($this->filter, $entity)) { continue; } $this->results[$path] = $entity; } return $this; }
/** * Create a temp file * * @param string $prefix A prefix for the filename * @return \sndsgd\fs\File */ public static function createFile(string $prefix, int $maxAttempts = 10) : entity\FileEntity { $tmpdir = static::getDir(); $prefix = \sndsgd\Fs::sanitizeName($prefix); $attempts = 1; do { if ($attempts > $maxAttempts) { throw new \RuntimeException("failed to create temp file; " . "reached max number ({$maxAttempts}) of attempts"); } $rand = \sndsgd\Str::random(10); $path = "{$tmpdir}/{$prefix}-{$rand}"; $attempts++; } while (file_exists($path)); touch($path); $file = new entity\FileEntity($path); static::registerEntity($file); return $file; }
/** * Get the filesize as a formatted string * * @param int $precision The number of decimal places to return * @param string $point The decimal point * @param string $sep The thousands separator * @return string An empty string if the size could not be determined */ public function getSize(int $precision = 0, string $point = ".", string $sep = ",") : string { $bytes = $this->getByteSize(); if ($bytes === -1) { return ""; } return \sndsgd\Fs::formatSize($bytes, $precision, $point, $sep); }