file_path)) { return false; } $this->filePath = $this->file_path; if (@\file_exists($this->file_path)) { return $this->filePath; } return $this->filePath; } /** * Sets the file path. * * @param string $filePath File path incl. file name and optional extension */ public function setFilePath(string $filePath): void { $this->file_path = $filePath; } /** * Sets the data part info. * * @param DataPartInfo $dataInfo Date info (file content) */ public function addDataPartInfo(DataPartInfo $dataInfo): void { $this->dataInfo = $dataInfo; } /** * Gets information about a file. * * @param int $fileinfo_const Any predefined constant. See https://www.php.net/manual/en/fileinfo.constants.php * * @psalm-param fileinfoconst $fileinfo_const */ public function getFileInfo(int $fileinfo_const = FILEINFO_NONE): string { $finfo = new finfo($fileinfo_const); return $finfo->buffer($this->getContents()); } /** * Gets the file content. */ public function getContents(): string { if (null === $this->dataInfo) { throw new UnexpectedValueException(static::class.'::$dataInfo has not been set by calling '.self::class.'::addDataPartInfo()'); } return $this->dataInfo->fetch(); } /** * Saves the attachment object on the disk. * * @return bool True, if it could save the attachment on the disk */ public function saveToDisk(): bool { if (null === $this->dataInfo) { return false; } if (false === \file_put_contents($this->__get('filePath'), $this->dataInfo->fetch())) { unset($this->filePath, $this->file_path); return false; } return true; } }