Completed
Push — master ( f18951...94bfb9 )
by Randy
03:34
created

Method::of()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
use ICanBoogie\Inflector;
6
7
/**
8
 * Class Method
9
 * @package Dgame\Soap\Hydrator
10
 */
11
final class Method
12
{
13
    const PREFIXES = ['set', 'append'];
14
15
    /**
16
     * @var object
17
     */
18
    private $object;
19
    /**
20
     * @var null|string
21
     */
22
    private $method;
23
24
    /**
25
     * Method constructor.
26
     *
27
     * @param string $postifx
28
     * @param object $object
29
     */
30
    public function __construct(string $postifx, $object)
31
    {
32
        $this->object = $object;
33
        $this->method = $this->getMethod($postifx);
34
    }
35
36
    /**
37
     * @param string $postfix
38
     * @param object $object
39
     *
40
     * @return Method
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...
41
     */
42
    public static function of(string $postfix, $object): self
43
    {
44
        return new self($postfix, $object);
45
    }
46
47
    /**
48
     * @return bool
49
     */
50
    public function isValid(): bool
51
    {
52
        return $this->method !== null;
53
    }
54
55
    /**
56
     * @param $value
57
     *
58
     * @return bool
59
     */
60
    public function assign($value): bool
61
    {
62
        if ($this->isValid()) {
63
            call_user_func([$this->object, $this->method], $value);
64
65
            return true;
66
        }
67
68
        return false;
69
    }
70
71
    /**
72
     * @param string $postfix
73
     *
74
     * @return null|string
75
     */
76
    private function getMethod(string $postfix)
77
    {
78
        foreach (self::PREFIXES as $prefix) {
79
            $method = $prefix . Inflector::get()->camelize($postfix, Inflector::UPCASE_FIRST_LETTER);
80
            if (method_exists($this->object, $method)) {
81
                return $method;
82
            }
83
        }
84
85
        return null;
86
    }
87
}