Model::getConstructorArguments()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 1
b 0
f 1
cc 3
eloc 6
nc 3
nop 2
crap 3
1
<?php
2
3
namespace PHPieces\ANZGateway\models;
4
5
use ReflectionClass;
6
7
class Model
8
{
9
10 18
    private static function getConstants() : array
11
    {
12 18
        $class = static::$fields;
13 18
        return array_flip((new ReflectionClass($class))->getConstants());
14
    }
15
16 18
    private static function getConstructorArguments(array $params, array $constants) : array
17
    {
18 18
        $arguments = [];
19 18
        foreach ($constants as $key => $val) {
20 18
            if (isset($params[$key])) {
21 18
                $arguments[$key] = $params[$key];
22
            }
23
        }
24 18
        return $arguments;
25
    }
26
27 18
    public static function create(array $params) : self
28
    {
29 18
        $reflector = new ReflectionClass(static::class);
30 18
        $constants = self::getConstants();
31 18
        $data = self::getConstructorArguments($params, $constants);
32 18
        return $reflector->newInstanceArgs($data);
33
    }
34
35 9
    public static function getFields() : array
36
    {
37 9
        $fields = (new ReflectionClass(static::$fields))->getConstants();
38
39 9
        $final = [];
40 9
        foreach ($fields as $label => $name) {
41 9
            $label = str_replace("_", " ", $label);
42 9
            $label = strtolower($label);
43 9
            $label = ucwords($label);
44 9
            $final[$label] = $name;
45
        }
46 9
        return $final;
47
    }
48
}
49