Completed
Push — master ( c2d23a...f43081 )
by Jesse
02:42
created

VariadicConstructor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 35
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A forThe() 0 3 1
A classFor() 0 3 1
A fromArray() 0 7 2
A __construct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydrator;
5
6
use Throwable;
7
8
/**
9
 * Hydrates an object by calling its constructor with squashed array input.
10
 *
11
 * @package Stratadox\Hydrate
12
 * @author  Stratadox
13
 */
14
final class VariadicConstructor implements Hydrates
15
{
16
    private $class;
17
18
    private function __construct(string $forTheClass)
19
    {
20
        $this->class = $forTheClass;
21
    }
22
23
    /**
24
     * Creates a new variadic constructor calling hydrator.
25
     *
26
     * @param string $class The class to hydrate.
27
     * @return Hydrates     The variadic construction calling hydrator.
28
     */
29
    public static function forThe(string $class): Hydrates
30
    {
31
        return new self($class);
32
    }
33
34
    /** @inheritdoc */
35
    public function fromArray(array $input)
36
    {
37
        $class = $this->class;
38
        try {
39
            return new $class(...$input);
40
        } catch (Throwable $exception) {
41
            throw HydrationFailed::encountered($exception, $class);
42
        }
43
    }
44
45
    /** @inheritdoc */
46
    public function classFor(array $input): string
47
    {
48
        return $this->class;
49
    }
50
}
51