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

BaseInterface   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 22
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromJson() 0 7 2
A fromFile() 0 3 1
A fromArray() 0 6 2
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