Advertisement
Guest User

Untitled

a guest
May 21st, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. class DataSet implements DataSetInterface
  2. {
  3.     private $object;
  4.  
  5.     private function __construct($object)
  6.     {
  7.         $this->object = $object;
  8.     }
  9.  
  10.     public static function fromObject($object)
  11.     {
  12.         return new self($object);
  13.     }
  14.  
  15.     public function getAttributeValue(string $attribute)
  16.     {
  17.         if (!$this->hasAttribute($attribute)) {
  18.             throw new MissingAttributeException('$attribute is missing');
  19.         }
  20.  
  21.         return get_object_vars($this->object)[$attribute];
  22.     }
  23.  
  24.     public function hasAttribute(string $attribute): bool
  25.     {
  26.         return array_key_exists($attribute, get_object_vars($this->object));
  27.     }
  28. }
  29.  
  30. class Post
  31. {
  32.     public string $title;
  33.     public string $description;
  34. }
  35.  
  36. $dataSet = DataSet::fromObject(new Post());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement