Skip to content

Instantly share code, notes, and snippets.

@aufa
Created September 18, 2016 16:10
Show Gist options
  • Save aufa/715e107337ea5aa87cf33a4954d1f68b to your computer and use it in GitHub Desktop.
Save aufa/715e107337ea5aa87cf33a4954d1f68b to your computer and use it in GitHub Desktop.
(Data Storage Object) Safely Save & Serve
<?php
namespace Aufa\Gist\Component;
/**
* Class DataStorage
* @package Aufa\Gist\Component
*
* add backslash to use default core function - to avoid user create new functions instead of
*/
class DataStorage implements \ArrayAccess, \Serializable, \Countable, \JsonSerializable
{
/**
* @param string $offset
*
* @return bool
*/
public function offsetExists($offset)
{
return \property_exists($this, $offset);
}
/**
* Getting Property
*
* @param string $offset
*
* @return mixed|null
*/
public function offsetGet($offset)
{
if ($this->offsetExists($offset)) {
return $this->$offset;
}
return null;
}
/**
* Set the property
*
* @param string $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
if (\is_string($offset) || \is_numeric($offset)) {
$this->$offset = $value;
}
}
/**
* unset Property
*
* @param string $offset
*/
public function offsetUnset($offset)
{
if ($this->offsetExists($offset)) {
unset($this->$offset);
}
}
/**
* getting Data
*
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public function get($name, $default = null)
{
if ($this->offsetExists($name)) {
return $this->offsetGet($name);
}
return $default;
}
/**
* Getting All Properties
*
* @return array
*/
public function all()
{
return \get_object_vars($this);
}
/**
* @return int
*/
public function count()
{
return \count($this->all());
}
/**
* @param string $serialized
*/
public function unserialize($serialized)
{
$serialized = @\unserialize($serialized);
if (\is_array($serialized)) {
foreach ($serialized as $key => $value) {
$this->offsetSet($key, $value);
}
}
}
/**
* Serializable
*
* @return string
*/
public function serialize()
{
// markup to make it not thrown the error
$serial = @\serialize($this->all());
if (empty($serial)) {
$serial = \serialize(array());
}
return $serial;
}
/**
* @return array
*/
public function jsonSerialize()
{
return $this->all();
}
/**
* @return string
*/
public function __toString()
{
return $this->serialize();
}
/**
* Magic Method Method Call
*
* Prevent Data Error
*
* @param string $name
* @param array $args
* @return mixed
*/
public function __call($name, $args)
{
if (\is_array($args) && \is_string($name) && \method_exists($this, $name)) {
return \call_user_func(array($this, $name), $args);
}
return null;
}
/**
* Getting Property
*
* @param string $name
*
* @return mixed
*/
public function __get($name)
{
return $this->offsetGet($name);
}
/**
* Magic Method Set Property
*
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
$this->offsetSet($name, $value);
}
/**
* Magic Method Static Call
*
* Prevent Data Error
*
* @param string $name
* @param array $args
*/
public function __callStatic($name, $args)
{
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment