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

VariadicConstructor::classFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 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