BaseDto::launch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
nop 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Base Infra Class
6
 * @category    Ticaje
7
 * @author      Max Demian <[email protected]>
8
 */
9
10
namespace Ticaje\Contract\Traits;
11
12
/**
13
 * Trait BaseDto
14
 * @package Ticaje\Contract\Traits
15
 */
16
trait BaseDto
17
{
18
    use Utils;
19
20
    /**
21
     * @param $name
22
     * @param $arguments
23
     * @return mixed|BaseDto|void
24
     */
25
    public function __call($name, $arguments)
26
    {
27
        if ((!$prefix = $this->checkPrefix($name)) || (!($property = $this->checkPropertyExistence($name)))) {
28
            return;
29
        }
30
        return $this->launch($prefix, $property, $arguments);
31
    }
32
33
    /**
34
     * @param array $data
35
     * Not intrusive mode
36
     */
37
    public function setData(array $data)
38
    {
39
        if (is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
40
            foreach ($data as $property => $vale) {
41
                $this->set($this->camelize($property), $vale);
42
            }
43
        }
44
    }
45
46
    /**
47
     * @param $prefix
48
     * @param $property
49
     * @param $arguments
50
     * @return mixed|BaseDto
51
     */
52
    private function launch($prefix, $property, $arguments)
53
    {
54
        return $prefix == 'get' ? $this->get($property) : $this->set($property, $arguments[0]);
55
    }
56
57
    /**
58
     * @param $name
59
     * @return bool|string
60
     */
61
    private function checkPropertyExistence($name)
62
    {
63
        $property = lcfirst(substr($name, 3, strlen($name)));
64
        return $property;
65
    }
66
67
    /**
68
     * @param $method
69
     * @return bool|string
70
     */
71
    private function checkPrefix($method)
72
    {
73
        $prefix = substr($method, 0, 3);
74
        return in_array($prefix, array('get', 'set')) ? $prefix : false;
75
    }
76
77
    /**
78
     * @param $property
79
     * @param $value
80
     * @return $this
81
     */
82
    private function set($property, $value)
83
    {
84
        $this->$property = $value;
85
        return $this;
86
    }
87
88
    /**
89
     * @param $property
90
     * @return mixed
91
     */
92
    private function get($property)
93
    {
94
        return $this->{$property} ?? null;
95
    }
96
}
97