Completed
Branch feature/issue-13 (2c36a0)
by Mikaël
01:15
created

AbstractStructBase::__set_state()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace WsdlToPhp\PackageBase;
4
5
abstract class AbstractStructBase implements StructInterface, \JsonSerializable
6
{
7
    /**
8
     * Returns the properties of this object
9
     * @return mixed[]
10
     */
11 5
    public function jsonSerialize()
12
    {
13 5
        return \get_object_vars($this);
14
    }
15
    /**
16
     * Generic method called when an object has been exported with var_export() functions
17
     * It allows to return an object instantiated with the values
18
     * @uses ApiWsdlClass::_set()
19
     * @param array $array the exported values
20
     * @return Struct
21
     */
22 15
    public static function __set_state(array $array)
23
    {
24 15
        $className = get_called_class();
25 15
        $object = new $className();
26 15
        foreach ($array as $name => $value) {
27 15
            $object->_set($name, $value);
28 9
        }
29 10
        return $object;
30
    }
31
    /**
32
     * Generic method setting value
33
     * @throws \InvalidArgumentException
34
     * @param string $name property name to set
35
     * @param mixed $value property value to use
36
     * @return Struct
37
     */
38 65
    public function _set($name, $value)
39
    {
40 65
        $setMethod = 'set' . ucfirst($name);
41 65
        if (method_exists($this, $setMethod)) {
42 65
            $this->$setMethod($value);
43 39
        } else {
44 5
            throw new \InvalidArgumentException(sprintf('Setter does not exist for "%s" property', $name));
45
        }
46 65
        return $this;
47
    }
48
    /**
49
     * Generic method getting value
50
     * @throws \InvalidArgumentException
51
     * @param string $name property name to get
52
     * @return mixed
53
     */
54 65
    public function _get($name)
55
    {
56 65
        $getMethod = 'get' . ucfirst($name);
57 65
        if (method_exists($this, $getMethod)) {
58 60
            return $this->$getMethod();
59
        }
60 5
        throw new \InvalidArgumentException(sprintf('Getter does not exist for "%s" property', $name));
61
    }
62
}
63