<?php
 class vB_Entity_CachedUrl extends vB_Entity { use vB_Trait_NoSerialize; public string $hash; public string $host; public function __construct( public string $url, public array $data, public int $expires, public int $lastfetch, public string $contenttype, public int $statuscode = 0, public ?int $urlid = null ) { $urlarr = []; [ 'url' => $url, 'host' => $host, ] = static::normalizeUrlAndHost($url, $urlarr); $this->hash = static::hashNormalizedUrl($url); $this->host = $host; } public static function hashNormalizedUrl(string $url) : string { return md5($url); } public static function normalizeUrlAndHost(string $url, array &$urlarr = []) : array { $stringUtil = vB::getString(); $urlarr = $stringUtil->parseUrl($url); if (!$urlarr) { return [ 'url' => '', 'host' => '', 'scheme' => '', ]; } unset($urlarr['fragment']); $url = $stringUtil->unparseUrl($urlarr, false, 'fragment'); $host = $stringUtil->unparseUrl($urlarr, true, 'port'); return [ 'url' => $url, 'host' => $host, 'scheme' => $urlarr['scheme'], ]; } public static function createFromArray(array $dbrecord) : static { $dbrecord['data'] ??= []; if (!is_array($dbrecord['data'])) { $dbrecord['data'] = json_decode($dbrecord['data'], true); if (!is_array($dbrecord['data'])) { $dbrecord['data'] = []; } } return new vB_Entity_CachedUrl( $dbrecord['url'], $dbrecord['data'], $dbrecord['expires'] ?? 0, $dbrecord['lastfetch'] ?? 0, $dbrecord['contenttype'] ?? '', $dbrecord['statuscode'] ?? 0, $dbrecord['urlid'] ?? null ); } } 