Skip to content

Instantly share code, notes, and snippets.

@breadlesscode
Last active August 20, 2018 08:21
Show Gist options
  • Save breadlesscode/aaaa0c6496781bc9f0c650d5ee889ca5 to your computer and use it in GitHub Desktop.
Save breadlesscode/aaaa0c6496781bc9f0c650d5ee889ca5 to your computer and use it in GitHub Desktop.
<?php
abstract class AbstractDto
{
/**
* here the dto stores all the object specific data
*
* @var \ArrayObject
*/
protected $data;
/**
* this are all attributes that this DTO can have
*
* @var array
*/
protected $attributes = [];
/**
* AbstractDto constructor.
*/
public function __construct()
{
$this->data = new \ArrayObject();
}
/**
* @return \ArrayObject
*/
public function toArray()
{
return $this->data;
}
/**
* @param $name
* @return mixed|null
*/
public function __get($name)
{
if (!$this->data->offsetExists($name)) {
return null;
}
return $this->data[$name];
}
/**
* @param $name
* @param $value
* @return bool
*/
public function __set($name, $value)
{
if (!in_array($name, $this->attributes)) {
return false;
}
$this->data[$name] = $value;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment