Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
PasteEntity
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 dateStringToCarbon
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Icawebdesign\Hibp\Paste;
6
7use Carbon\Carbon;
8use stdClass;
9
10readonly class PasteEntity
11{
12    public string $source;
13
14    public string $id;
15
16    public string $title;
17
18    public ?Carbon $date;
19
20    public int $emailCount;
21
22    public string $link;
23
24    /** @var array<string, string> */
25    public array $pasteSites;
26
27    public function __construct(stdClass $data)
28    {
29        $this->pasteSites = [
30            'pastebin' => 'https://pastebin.com/',
31        ];
32
33        $sourceKey = strtolower($data->Source);
34        $sourceLink = $data->Id;
35
36        if (array_key_exists($sourceKey, $this->pasteSites)) {
37            $sourceLink = "{$this->pasteSites[$sourceKey]}{$data->Id}";
38        }
39
40        $this->source = $data->Source;
41        $this->id = $data->Id;
42        $this->title = $data->Title ?? '';
43        $this->date = $this->dateStringToCarbon($data->Date ?? null);
44        $this->emailCount = $data->EmailCount;
45        $this->link = $sourceLink;
46    }
47
48    public function dateStringToCarbon(?string $date = null): ?Carbon
49    {
50        if ($date === null) {
51            return null;
52        }
53
54        return Carbon::createFromFormat('Y-m-d\TH:i:s\Z', $date);
55    }
56}