withData([ 'chrome' => $chrome, 'firefox' => $firefox, 'safari' => $safari, 'edge' => $edge, ]); } } /** * Create from legacy array format: ['Chrome' => 65, 'Firefox' => 20, ...] * @param array $data */ public static function fromArray(array $data): self { return new self( chrome: $data['Chrome'] ?? 0, firefox: $data['Firefox'] ?? 0, safari: $data['Safari'] ?? 0, edge: $data['Edge'] ?? 0, ); } public function getTotal(): int { return $this->chrome + $this->firefox + $this->safari + $this->edge; } public function getChromePercentage(): Percentage { return Percentage::fromRatio($this->chrome, $this->getTotal()); } public function getFirefoxPercentage(): Percentage { return Percentage::fromRatio($this->firefox, $this->getTotal()); } public function getSafariPercentage(): Percentage { return Percentage::fromRatio($this->safari, $this->getTotal()); } public function getEdgePercentage(): Percentage { return Percentage::fromRatio($this->edge, $this->getTotal()); } /** * @return array */ public function toArray(): array { return [ 'Chrome' => $this->chrome, 'Firefox' => $this->firefox, 'Safari' => $this->safari, 'Edge' => $this->edge, ]; } /** * Legacy format for backward compatibility * @return array */ public function toAnalyticsArray(): array { return [ 'Chrome' => $this->chrome, 'Firefox' => $this->firefox, 'Safari' => $this->safari, 'Edge' => $this->edge, 'chrome_percentage' => $this->getChromePercentage()->format(), 'firefox_percentage' => $this->getFirefoxPercentage()->format(), 'safari_percentage' => $this->getSafariPercentage()->format(), 'edge_percentage' => $this->getEdgePercentage()->format(), 'total' => $this->getTotal(), ]; } }