Passed
Push — master ( 1e77ab...d0f64e )
by Matthew
06:39
created

BaseInterface::fromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MySociety\TheyWorkForYou\DataClass;
4
5
use function Rutek\Dataclass\transform;
6
7
trait BaseInterface {
8
    public static function fromJson(string $json): self {
9
        $data = json_decode($json, true);
10
        try {
11
            return transform(static::class, $data);
12
        } catch (\Exception $transformException) {
13
            echo json_encode($transformException, JSON_PRETTY_PRINT);
14
            throw $transformException;
15
        }
16
    }
17
18
    public static function fromFile(string $file): self {
19
        $content = file_get_contents($file);
20
        return static::fromJson($content);
21
    }
22
23
    public static function fromArray(array $data): self {
24
        try {
25
            return transform(static::class, $data);
26
        } catch (\Exception $transformException) {
27
            echo json_encode($transformException, JSON_PRETTY_PRINT);
28
            throw $transformException;
29
        }
30
    }
31
}
32