vendor/shopware/storefront/Page/Product/Review/RatingMatrix.php line 8

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Page\Product\Review;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Bucket\Bucket;
  4. use Shopware\Core\Framework\Struct\Struct;
  5. class RatingMatrix extends Struct
  6. {
  7.     private const MAX_POINTS 5;
  8.     /**
  9.      * @var MatrixElement[]
  10.      */
  11.     protected $matrix = [];
  12.     /**
  13.      * @var int
  14.      */
  15.     protected $totalReviewCount 0;
  16.     /**
  17.      * @var int
  18.      *
  19.      * @deprecated tag:v6.4.0 use $pointSum instead
  20.      */
  21.     protected $totalPoints 0;
  22.     /**
  23.      * @var float
  24.      */
  25.     protected $pointSum 0;
  26.     /**
  27.      * we expect an array of CountResult elements
  28.      * we are doing all calculation in constructor
  29.      *
  30.      * @param Bucket[] $matrix
  31.      */
  32.     public function __construct(array $matrix)
  33.     {
  34.         for ($points self::MAX_POINTS$points 0; --$points) {
  35.             foreach ($matrix as $rating) {
  36.                 $rawRatingKey = (float) $rating->getKey();
  37.                 if ($points === (int) round($rawRatingKey)) {
  38.                     $this->totalPoints += ($points $rating->getCount());
  39.                     $this->pointSum += ($rawRatingKey $rating->getCount());
  40.                     $this->totalReviewCount += $rating->getCount();
  41.                     $previousCount = isset($this->matrix[$points]) ? $this->matrix[$points]->getCount() : 0;
  42.                     $this->matrix[$points] = new MatrixElement($points$rating->getCount() + $previousCount);
  43.                 }
  44.             }
  45.             if (!array_key_exists($points$this->matrix)) {
  46.                 $this->matrix[$points] = new MatrixElement($points0);
  47.             }
  48.         }
  49.         array_walk($this->matrix, function (MatrixElement &$rating): void {
  50.             if ($this->totalReviewCount 0) {
  51.                 $rating->setPercent($rating->getCount() / $this->totalReviewCount 100);
  52.             } else {
  53.                 $rating->setPercent(0);
  54.             }
  55.         });
  56.     }
  57.     public function getMatrix(): array
  58.     {
  59.         return $this->matrix;
  60.     }
  61.     public function getTotalReviewCount(): int
  62.     {
  63.         return $this->totalReviewCount;
  64.     }
  65.     /**
  66.      * @deprecated tag:v6.4.0 use the getPointSum() method instead
  67.      */
  68.     public function getTotalPoints(): int
  69.     {
  70.         return $this->totalPoints;
  71.     }
  72.     public function getPointSum(): float
  73.     {
  74.         return $this->pointSum;
  75.     }
  76.     public function getMaxPoints(): int
  77.     {
  78.         return self::MAX_POINTS;
  79.     }
  80.     public function getAverageRating(): float
  81.     {
  82.         if ($this->getPointSum() > 0) {
  83.             return $this->getPointSum() / $this->getTotalReviewCount();
  84.         }
  85.         return 0;
  86.     }
  87. }