Passed
Push — master ( a435d0...3e0df0 )
by Radu
02:08
created

AbstractValidatedClass::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 0
1
<?php
2
namespace WebServCo\Framework\Arrays;
3
4
abstract class AbstractValidatedClass
5
{
6
    abstract protected function getRequiredProperties();
7
8
    public function __construct($data = [])
9
    {
10
        foreach (get_object_vars($this) as $property => $default) {
11
            if (array_key_exists($property, $data)) {
12
                $this->{$property} = $data[$property];
13
            }
14
        }
15
        $this->validate();
16
    }
17
18
    protected function validate()
19
    {
20
        $required = $this->getRequiredProperties();
21
        foreach ($required as $item) {
22
            if (empty($this->{$item})) {
23
                throw new \WebServCo\Framework\Exceptions\Validation\RequiredArgumentException(
24
                    sprintf('Empty required parameter: %s', $item)
25
                );
26
            }
27
        }
28
        return true;
29
    }
30
}
31