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

AbstractStructBase   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 58
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 4 1
A __set_state() 0 9 2
A _set() 0 10 2
A _get() 0 8 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