Completed
Push — master ( e80ea0...ba2e48 )
by Randy
02:42
created

Hydrate::getAppendNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
use Dgame\Object\ObjectFacade;
6
use Dgame\Soap\AssignableInterface;
7
8
/**
9
 * Class Hydrate
10
 * @package Dgame\Soap\Hydrator
11
 */
12
final class Hydrate extends ObjectFacade
13
{
14
    /**
15
     * @var string
16
     */
17
    private $name;
18
19
    /**
20
     * Hydrate constructor.
21
     *
22
     * @param string $name
23
     * @param string $class
24
     */
25 9
    public function __construct(string $name, string $class)
26
    {
27 9
        parent::__construct(new $class());
28
29 9
        $this->name = $name;
30 9
    }
31
32
    /**
33
     * @param string $name
34
     * @param string $class
35
     *
36
     * @return Hydrate
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
37
     */
38 9
    public static function new(string $name, string $class): self
39
    {
40 9
        return new self($name, $class);
41
    }
42
43
    /**
44
     * @param Hydrate $hydrate
0 ignored issues
show
Documentation introduced by
Should the type for parameter $hydrate not be \self?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
45
     *
46
     * @return bool
47
     */
48 5
    public function append(self $hydrate): bool
49
    {
50 5
        return $this->setValueWithVariantNames([$hydrate->getName(), $hydrate->getClassName()], $hydrate->getObject());
51
    }
52
53
    /**
54
     * @param AssignableInterface $assignable
55
     *
56
     * @return bool
57
     */
58 8
    public function assign(AssignableInterface $assignable): bool
59
    {
60 8
        if ($assignable->hasValue()) {
61 8
            return $this->setValueWithVariantNames([$assignable->getName()], $assignable->getValue());
62
        }
63
64 1
        return false;
65
    }
66
67
    /**
68
     * @param array $names
69
     * @param       $value
70
     *
71
     * @return bool
72
     */
73 9
    private function setValueWithVariantNames(array $names, $value)
74
    {
75 9
        foreach (self::getAppendNames($names) as $name) {
76 9
            if ($this->setValue($name, $value)) {
77 9
                return true;
78
            }
79
        }
80
81 1
        return false;
82
    }
83
84
    /**
85
     * @param array $names
86
     *
87
     * @return array
88
     */
89 9
    private static function getAppendNames(array $names): array
90
    {
91 9
        $output = [];
92 9
        foreach ($names as $name) {
93 9
            $output[] = lcfirst($name);
94 9
            $output[] = ucfirst($name);
95
        }
96
97 9
        return $output;
98
    }
99
100
    /**
101
     * @return string
102
     */
103 5
    public function getClassName(): string
104
    {
105 5
        return $this->getReflection()->getShortName();
106
    }
107
108
    /**
109
     * @return string
110
     */
111 5
    public function getName(): string
112
    {
113 5
        return $this->name;
114
    }
115
}