aggregateRating??null; } public function setAggregateRating(array|AggregateRating $aggregateRating):void { if (is_array($aggregateRating)){ if (!array_key_exists('type', $aggregateRating)) { $aggregateRating['type'] = 'JVBase\managers\SEO\render\Thing\Intangible\Rating\AggregateRating'; } $aggregateRating = SchemaHelper::classFromConfig($aggregateRating); } $this->aggregateRating = $aggregateRating; } public function setAggregateRatingField(Fields $fields):void { $fields->addField('average_rating', [ 'type' => 'select', 'label' => __('Average Rating', 'jvb'), 'options' => [ 'none' => 'None', '0.5' => '0.5', '1' => '1', '1.5' => '1.5', '2' => '2', '2.5' => '2.5', '3' => '3', '3.5' => '3.5', '4' => '4', '4.5' => '4.5', '5' => '5', ], 'default' => 'none', ]); $fields->addField('ratingCount', [ 'type' => 'number', 'label' => __('The total number of ratings', 'jvb'), 'condition' => [ 'field' => 'average_rating', 'operator' => '!=', 'value' => 'none', ] ]); $fields->addField('reviewCount', [ 'type' => 'number', 'label' => __('The total number of reviews (with text)', 'jvb'), 'condition' => [ 'field' => 'average_rating', 'operator' => '!=', 'value' => 'none', ] ]); $fields->addField('bestRating', [ 'type' => 'number', 'label' => __('The best possible rating value (top of scale)', 'jvb'), 'default' => 5, 'condition' => [ 'field' => 'average_rating', 'operator' => '!=', 'value' => 'none', ] ]); $fields->addField('worstRating', [ 'type' => 'number', 'label' => __('The worst possible rating value (bottom of scale)', 'jvb'), 'default' => 1, 'condition' => [ 'field' => 'average_rating', 'operator' => '!=', 'value' => 'none', ] ]); } public function formatAggregateRatingField(Meta $meta):void { [$average, $ratingCount, $reviewCount, $bestRating, $worstRating] = $meta->getAll([ 'average_rating', 'ratingCount', 'reviewCount', 'bestRating', 'worstRating' ]); if (!empty($average)) { $rating = new AggregateRating(); $rating->setRatingValue((float)$average); if (!empty($ratingCount)) { $rating->setRatingCount($ratingCount); } if (!empty($reviewCount)){ $rating->setReviewCount($reviewCount); } if ($bestRating !== 5) { $rating->setBestRating($bestRating); } if ($worstRating !== 1) { $rating->setWorstRating($worstRating); } $this->setAggregateRating($rating); } } }