Created
October 3, 2011 19:16
-
-
Save nerdsrescueme/1259964 to your computer and use it in GitHub Desktop.
Storage and Encoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Atom\Encoder; | |
use Atom\Encodable; | |
class Base64 implements Encodable { | |
public function decode($data) | |
{ | |
return base64_decode(unserialize($data)); | |
} | |
public function encode($data) | |
{ | |
return base64_encode(serialize($data)); | |
} | |
} | |
/* End of file: base64.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Atom; | |
interface Encodable { | |
public function decode($data); | |
public function encode($data); | |
} | |
/* End of file: encodable.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Atom; | |
class Encoder { | |
public static function get($encodable) | |
{ | |
try | |
{ | |
// @TODO Allow other namespaces. | |
$encoder = new '\\Atom\\Encoder\\'.ucfirst($encodable); | |
} | |
catch(\Exception $e) | |
{ | |
throw new \OutOfBoundsException("Encoder [$encodable] does not exist."); | |
} | |
// @TODO New namespaces added, add instanceof \Atom\Encodable check | |
return $encoder; | |
} | |
} | |
/* End of file: encoder.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Atom\Storage; | |
use Atom\Storable; | |
class File implements Storable { | |
protected $_path; | |
private $_append = false; | |
public function __construct($path, $create = false) | |
{ | |
if(!file_exists($path)) | |
{ | |
if(!$create) | |
{ | |
throw new \OutOfBoundsException('The file ['.str_replace(LIBRARY_PATH, 'LIBRARY_PATH', $path).'] does not exist.'); | |
} | |
if(!touch($path)) | |
{ | |
throw new \Exception('The file ['.str_replace(LIBRARY_PATH, 'LIBRARY_PATH', $path).'] could not be created.'); | |
} | |
} | |
$this->_path = $path; | |
} | |
public function append($data) | |
{ | |
$this->_append = true; | |
return $this->write($data); | |
} | |
public function delete() | |
{ | |
return unlink($this->path); | |
} | |
public function read() | |
{ | |
return file_get_contents($this->_path); | |
} | |
public function write($data) | |
{ | |
if(!is_writable($this->_path)) | |
{ | |
throw new \OutOfBoundsException('The file ['.str_replace(LIBRARY_PATH, 'LIBRARY_PATH', $this->_path).'] is not writable.'); | |
} | |
$flags = LOCK_EX; | |
if($this->_append) | |
{ | |
$flags = LOCK_EX | FILE_APPEND; | |
$this->_append = false; // Reset append | |
} | |
return file_put_contents($this->path, $data, $flags); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Atom\Encoder; | |
use Atom\Encodable; | |
class Json implements Encodable { | |
public function decode($data) | |
{ | |
return json_decode($data); | |
} | |
public function encode($data) | |
{ | |
return json_encode($data); | |
} | |
} | |
/* End of file: json.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Atom\Encoder; | |
use Atom\Encodable; | |
class Passthrough implements Encodable { | |
public function decode($data) | |
{ | |
return $data; | |
} | |
public function encode($data) | |
{ | |
return $data; | |
} | |
} | |
/* End of file: passthrough.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Atom\Encoder; | |
use Atom\Encodable; | |
class Serialize implements Encodable { | |
public function decode($data) | |
{ | |
return unserialize($data); | |
} | |
public function encode($data) | |
{ | |
return serialize($data); | |
} | |
} | |
/* End of file: serialize.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Atom; | |
interface Storable { | |
public function __construct($identifier, $create); | |
public function append($data); | |
public function delete(); | |
public function read(); | |
public function write($data); | |
} | |
/* End of file storable.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Atom; | |
class Storage { | |
public static function get($storable, $identifier, $create = false) | |
{ | |
try | |
{ | |
// @TODO Allow other namespaces. | |
$storage = '\\Atom\\Storage\\'.ucfirst($storable); | |
$storage = new $storage($identifier, $create); | |
} | |
catch(\Exception $e) | |
{ | |
throw new \OutOfBoundsException("Storage type [$storable] does not exist."); | |
} | |
// @TODO Namespaces added, add instanceof \Atom\Storable check. | |
return $storage; | |
} | |
} | |
/* End of file: storage.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage