| @@ -1,0 +1,35 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Http\Metricool\DTOs; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * This class represents a DistributionDTO of one of the results of | |
| 9 | + * the DistributionDTO Entity. | |
| 10 | + */ | |
| 11 | +class DistributionDTO extends DTO | |
| 12 | +{ | |
| 13 | + public string $name; | |
| 14 | + public string $value; | |
| 15 | + public int $amount; | |
| 16 | + | |
| 17 | + public function __construct(string $name, string $value, int $amount) | |
| 18 | + { | |
| 19 | + $this->name = $name; | |
| 20 | + $this->value = $value; | |
| 21 | + $this->amount = $amount; | |
| 22 | + } | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Calculates the percentage of the amount in relation to the total number | |
| 26 | + * @see \Metricool\Http\Endpoints\Responses\Statistics\CountriesResponse::getSingleItem() | |
| 27 | + */ | |
| 28 | + public function calculatePercentageFromTotal(int $total): float | |
| 29 | + { | |
| 30 | + if ($total === 0 || $this->amount === 0) { | |
| 31 | + return 0; | |
| 32 | + } | |
| 33 | + return round((float) (($this->amount / $total) * 100), 3, PHP_ROUND_HALF_UP); | |
| 34 | + } | |
| 35 | +} | |