Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
BreachSiteTruncatedEntity
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 toArray
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 toJson
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Icawebdesign\Hibp\Breach;
6
7use Icawebdesign\Hibp\Interfaces\Arrayable;
8use Icawebdesign\Hibp\Interfaces\Jsonable;
9use RuntimeException;
10use stdClass;
11
12use function get_object_vars;
13
14use const JSON_THROW_ON_ERROR;
15
16readonly class BreachSiteTruncatedEntity implements Arrayable, Jsonable
17{
18    public string $name;
19
20    public function __construct(?stdClass $data = null)
21    {
22        if (null === $data) {
23            throw new RuntimeException('Invalid BreachSite data');
24        }
25
26        $this->name = $data->Name;
27    }
28
29    /**
30     * @return array<string, string>
31     */
32    public function toArray(): array
33    {
34        return [
35            'name' => $this->name,
36        ];
37    }
38
39    public function toJson(): string
40    {
41        return json_encode(get_object_vars($this), flags: JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
42    }
43}