Skip to content

Commit

Permalink
Add Folder & Recursive Sanitize
Browse files Browse the repository at this point in the history
  • Loading branch information
kranack committed Apr 17, 2018
1 parent 3ad1dda commit bc2bf9d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 65 deletions.
60 changes: 60 additions & 0 deletions lib/File/AbstractFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace File;

use Exception;

abstract class FileType {
const Folder = 0;
const File = 1;
const Link = 2;
}

abstract class AbstractFile {

protected $path;
protected $_content;
protected $_type;

public function __construct($path, $create = false) {
$this->path = $path;
$this->_content = null;
$this->getType();

if (!$create) {
$this->exists($path);
}
}

public function getPath() {
return $this->path;
}

public function getType() {
if (is_dir($this->path)) $this->_type = FileType::Folder;
else if (is_link($this->path)) $this->_type = FileType::Link;
else $this->_type = FileType::File;

return $this->_type;
}

public function isFile() { return $this->_type === FileType::File; }
public function isFolder() { return $this->_type === FileType::Folder; }
public function isLink() { return $this->_type === FileType::Link; }

private function exists() {
if (!file_exists($this->path)) {
throw new Exception("File not found");
}
if (!is_readable($this->path)) {
throw new Exception("File not readable");
}
}

abstract public function getContent();
abstract public function setContent($content);

abstract public function read();
abstract public function save();

}
61 changes: 10 additions & 51 deletions lib/File/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,34 @@

use Exception;

abstract class FileType {
const Folder = 0;
const File = 1;
const Link = 2;
}

class File {

private $filename;
private $_content;
private $_type;

public function __construct($filename, $create = false) {
$this->filename = $filename;
$this->_content = null;
$this->getType();

if (!$create) {
$this->exists($filename);
}
}
class File extends AbstractFile {

public function getContent() {
if ($this->_content === null) {
$this->_content = file_get_contents($this->filename);
$this->_content = file_get_contents($this->path);
}

if ($this->_content === false) { throw new Exception("Cannot get file content"); }

return $this->_content;
}

public function setContent($content) {
$this->_content = $content;
return $this;
}

public function read() {
$handle = fopen($this->filename, "rb");
$contents = fread($handle, filesize($this->filename));
$handle = fopen($this->path, "rb");
$contents = fread($handle, filesize($this->path));
fclose($handle);

return $contents;
}

public function getType() {
if (is_dir($this->filename)) $this->_type = FileType::Folder;
else if (is_link($this->filename)) $this->_type = FileType::Link;
else $this->_type = FileType::File;

return $this->_type;
}

public function isFile() { return $this->_type === FileType::File; }
public function isFolder() { return $this->_type === FileType::Folder; }
public function isLink() { return $this->_type === FileType::Link; }

public function setContent($content) {
$this->_content = $content;
return $this;
}

public function save() {
file_put_contents($this->filename, $this->_content);
file_put_contents($this->path, $this->_content);
return $this;
}

private function exists() {
if (!file_exists($this->filename)) {
throw new Exception("File not found");
}
if (!is_readable($this->filename)) {
throw new Exception("File not readable");
}
}

}
27 changes: 21 additions & 6 deletions lib/File/Folder.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
<?php

class Folder {

private $path;
namespace File;

public function __construct($folderPath) {
$this->path = $folderPath;
class Folder extends AbstractFile {

public function getContent() {
// NYI
return null;
}

public function setContent($content) {
// NYI
return $this;
}

public function read() {

$files = array_map(function($file) {
return new File($file);
}, glob($this->getPath() . DIRECTORY_SEPARATOR . "*"));

return $files;
}

public function save() {
// NYI
return $this;
}

}
25 changes: 17 additions & 8 deletions lib/UTF8/UTF8.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
namespace UTF8;

use File\File;
use File\Folder;

class UTF8 {

public static function sanitize(File $file) {
$fileContents = $file->read();

// Sanitize
foreach (static::getUTF8ToASCII() as $k => $v) {
$fileContents = str_replace($k, $v, $fileContents);
// Apply to all files
if ($file->isFolder()) {
$file = new Folder($file->getPath());
foreach ($file->read() as $_file) {
static::sanitize($_file);
}
} else {
$fileContents = $file->read();

// Sanitize
foreach (static::getUTF8ToASCII() as $k => $v) {
$fileContents = str_replace($k, $v, $fileContents);
}

// Save
$file->setContent($fileContents)->save();
}

// Save
$file->setContent($fileContents)->save();
}

public static function getUTF8ToASCII() {
Expand Down

0 comments on commit bc2bf9d

Please sign in to comment.