DTO.php
1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace FootyRoom\App;
use Exception;
abstract class Dto
{
/**
* Convenience method with construction by mapping an array input to class
* properties. If a passed property does not exist on the class an exception
* is thrown.
*
* @param array $data
*
* @throws \Exception
*/
public function __construct(array $data = [])
{
foreach ($data as $key => $value) {
if (!property_exists($this, $key)) {
throw new Exception(
sprintf('Property "%s" is not a valid property on "%s".', $key, get_class($this))
);
}
$this->$key = $value;
}
}
/**
* Prevents setting undefined public properties.
*
* @param string $name
* @param mixed $value
*
* @throws \Exception
*/
public function __set($name, $value)
{
throw new Exception('Attempting to set non-existent public propety.');
}
}