Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
BreachSiteEntity
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
2
 dateStringToCarbon
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
21 / 21
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 Carbon\Carbon;
8use Icawebdesign\Hibp\Exception\InvalidBreachSiteDataException;
9use Icawebdesign\Hibp\Interfaces\Arrayable;
10use Icawebdesign\Hibp\Interfaces\Jsonable;
11use Illuminate\Support\Collection;
12use stdClass;
13
14use function get_object_vars;
15
16use const JSON_THROW_ON_ERROR;
17use const JSON_UNESCAPED_SLASHES;
18
19readonly class BreachSiteEntity implements Arrayable, Jsonable
20{
21    public string $title;
22
23    public string $name;
24
25    public string $domain;
26
27    public Carbon $breachDate;
28
29    public Carbon $addedDate;
30
31    public Carbon $modifiedDate;
32
33    public int $pwnCount;
34
35    public string $description;
36
37    /** @var Collection<int, string> */
38    public Collection $dataClasses;
39
40    public bool $verified;
41
42    public bool $fabricated;
43
44    public bool $sensitive;
45
46    public bool $retired;
47
48    public bool $spamList;
49
50    public bool $malware;
51
52    public string $logoPath;
53
54    public bool $subscriptionFree;
55
56    public bool $stealerLog;
57
58    public ?string $attribution;
59
60    public function __construct(?stdClass $data = null)
61    {
62        if ($data === null) {
63            throw new InvalidBreachSiteDataException('Invalid BreachSite data');
64        }
65
66        /** @var array<int, string> $dataClasses */
67        $dataClasses = $data->DataClasses;
68
69        $this->title = $data->Title;
70        $this->name = $data->Name;
71        $this->domain = $data->Domain;
72        $this->breachDate = new Carbon($data->BreachDate);
73        $this->addedDate = $this->dateStringToCarbon(date: $data->AddedDate);
74        $this->modifiedDate = $this->dateStringToCarbon(date: $data->ModifiedDate);
75        $this->pwnCount = $data->PwnCount;
76        $this->description = $data->Description;
77        $this->dataClasses = Collection::make($dataClasses);
78        $this->verified = $data->IsVerified;
79        $this->fabricated = $data->IsFabricated;
80        $this->sensitive = $data->IsSensitive;
81        $this->retired = $data->IsRetired;
82        $this->spamList = $data->IsSpamList;
83        $this->malware = $data->IsMalware;
84        $this->subscriptionFree = $data->IsSubscriptionFree;
85        $this->logoPath = $data->LogoPath;
86        $this->stealerLog = $data->IsStealerLog;
87        $this->attribution = $data->Attribution;
88    }
89
90    private function dateStringToCarbon(string $date): Carbon
91    {
92        $dateObject = Carbon::createFromFormat(
93            'Y-m-d\TH:i:s\Z',
94            $date,
95        );
96
97        return $dateObject ?? Carbon::now();
98    }
99
100    /**
101     * @return array<string, mixed>
102     */
103    public function toArray(): array
104    {
105        return [
106            'title' => $this->title,
107            'name' => $this->name,
108            'domain' => $this->domain,
109            'breach_date' => $this->breachDate->toDateString(),
110            'added_date' => $this->addedDate->toIso8601ZuluString(),
111            'modified_date' => $this->modifiedDate->toIso8601ZuluString(),
112            'pwn_count' => $this->pwnCount,
113            'description' => $this->description,
114            'data_classes' => $this->dataClasses->toArray(),
115            'verified' => $this->verified,
116            'fabricated' => $this->fabricated,
117            'sensitive' => $this->sensitive,
118            'retired' => $this->retired,
119            'spam_list' => $this->spamList,
120            'malware' => $this->malware,
121            'logo_path' => $this->logoPath,
122            'subscription_free' => $this->subscriptionFree,
123            'stealer_log' => $this->stealerLog,
124            'attribution' => $this->attribution,
125        ];
126    }
127
128    public function toJson(): string
129    {
130        return json_encode(get_object_vars($this), flags: JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
131    }
132}