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

AbstractValidatedClass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 11
c 1
b 0
f 0
dl 0
loc 25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 11 3
A __construct() 0 8 3
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