| 1 | <?php |
||
| 20 | class FileWriter implements Writer |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var \SplFileObject |
||
| 25 | */ |
||
| 26 | private $file; |
||
| 27 | private $writeSize = 0; |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $filePath |
||
| 32 | */ |
||
| 33 | public function __construct($filePath) |
||
| 34 | { |
||
| 35 | $directoryPath = dirname($filePath); |
||
| 36 | |||
| 37 | if (file_exists($directoryPath) === false) { |
||
| 38 | throw new DirectoryNotFoundException("$directoryPath directory not found"); |
||
| 39 | } |
||
| 40 | |||
| 41 | if (is_writable($directoryPath) === false) { |
||
| 42 | throw new DirectoryNotWritableException("Can not write to the directory $directoryPath"); |
||
| 43 | } |
||
| 44 | |||
| 45 | $this->file = new SplFileObject($filePath, 'w'); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param string $text |
||
| 50 | */ |
||
| 51 | public function writeText($text) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $text |
||
| 58 | */ |
||
| 59 | public function writeLine($text) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Write a blank line |
||
| 66 | */ |
||
| 67 | public function writeEOL() |
||
| 71 | |||
| 72 | private function write($text) |
||
| 73 | { |
||
| 74 | $writeBytes = $this->file->fwrite($text); |
||
| 75 | $this->writeSize += $writeBytes; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return int |
||
| 80 | */ |
||
| 81 | public function getWriteSize() |
||
| 85 | |||
| 86 | } |
||
| 87 |