diff options
author | Philip Nicolcev <frugen@gmail.com> | 2014-08-24 13:12:44 -0400 |
---|---|---|
committer | Philip Nicolcev <frugen@gmail.com> | 2014-08-24 13:12:44 -0400 |
commit | f5fe739314de24e3a9cb94b9c8c64e11ed9cc14c (patch) | |
tree | be46c6120d6dc0d08a7112a8dc80840281cbad33 /source/File.php | |
parent | 9ffeed7646620ae7de51b929a3b44f2cdffb677f (diff) | |
parent | 5d8755faf06fd2ff9d3977f103e641b0ba9f714e (diff) | |
download | AJAX-Chat-origin/cli_testing.zip AJAX-Chat-origin/cli_testing.tar.gz AJAX-Chat-origin/cli_testing.tar.bz2 |
Merge pull request #178 from stevleibelt/masterorigin/cli_testing
Implemented Command Line Tools in cli_testing branch
Diffstat (limited to 'source/File.php')
-rw-r--r-- | source/File.php | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/source/File.php b/source/File.php new file mode 100644 index 0000000..076592c --- /dev/null +++ b/source/File.php @@ -0,0 +1,122 @@ +<?php +/** + * @author stev leibelt <artodeto@bazzline.net> + * @since 2014-08-13 + */ + +/** + * Class File + */ +class File +{ + /** + * @var string + */ + private $path; + + /** + * @param null|string $path + */ + public function __construct($path = null) + { + if (!is_null($path)) { + $this->setPath($path); + } + } + + /** + * @param string|array $content + * @throws Exception + */ + public function append($content) + { + if (is_array($content)) { + $content = implode(PHP_EOL, $content); + } else { + $content .= PHP_EOL; + } + + $numberOfBytes = file_put_contents($this->getPath(), $content, FILE_APPEND); + + if ($numberOfBytes === false) { + throw new Exception( + 'can not append content to file "' . $this->getPath() . '"' + ); + } + } + + /** + * @param string $path + * @throws Exception + */ + public function copy($path) + { + $couldBeCopied = copy($this->getPath(), $path); + + if ($couldBeCopied === false) { + throw new Exception( + 'could not copy file from path "' . $this->getPath() . '" to "' . $path . '"' + ); + } + } + + /** + * @return bool + * @throws Exception + */ + public function exists() + { + return (is_file($this->getPath())); + } + + /** + * @return string + * @throws Exception + */ + public function getPath() + { + if (is_null($this->path)) { + throw new Exception( + 'no path set' + ); + } + + return $this->path; + } + + /** + * @return array + * @throws Exception + */ + public function read() + { + return explode("\n", file_get_contents($this->getPath())); + } + + /** + * @param string $path + */ + public function setPath($path) + { + $this->path = (string) $path; + } + + /** + * @param string|array $content + * @throws Exception + */ + public function write($content) + { + if (is_array($content)) { + $content = implode("\n", $content); + } + + $numberOfBytes = file_put_contents($this->getPath(), $content); + + if ($numberOfBytes === false) { + throw new Exception( + 'can not append content to file "' . $this->getPath() . '"' + ); + } + } +}
\ No newline at end of file |