Lines
100.00%
16 / 16
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| PasteEntity | 100.00% | 16 / 16 | 100.00% | 2 / 2 | 4 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 13 / 13 | 100.00% | 1 / 1 | 2 | |||||
| dateStringToCarbon | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Icawebdesign\Hibp\Paste; | |
| 6 | ||
| 7 | use Carbon\Carbon; | |
| 8 | use stdClass; | |
| 9 | ||
| 10 | readonly 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 | } |