Jake Vanderwerf
5 hours ago 3baf3d2545ba6ece6b74a64c0def59bd0774cf54
inc/managers/SEO/render/Traits/_Properties/aggregateRatingTrait.php
@@ -1,7 +1,10 @@
<?php
namespace JVBase\managers\SEO\render\Traits\_Properties;
use JVBase\base\SchemaHelper;
use JVBase\managers\SEO\render\Thing\Intangible\Rating\AggregateRating;
use JVBase\meta\Meta;
use JVBase\registrar\Fields;
if (!defined('ABSPATH')) {
   exit;
@@ -16,40 +19,106 @@
   {
      return $this->aggregateRating??null;
   }
   public function setAggregateRating(array|AggregateRating $aggregateRating):void
   {
      if (is_array($aggregateRating)){
         $aggregateRating =AggregateRating::fromArray($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 getAggregateRatingFieldConfig():array
   public function setAggregateRatingField(Fields $fields):void
   {
      return [
         'type'   => 'group',
         'label'  => 'Aggregate/Average Rating',
         'wrap'   => 'details',
         'fields' => [
            'ratingCount'  => [
               'type'   => 'number',
               'label'  => 'The total number of ratings (without text)',
            ],
            'reviewCount'  => [
               'type'   => 'number',
               'label'  => 'The total number of reviews (with text)'
            ],
            'bestRating'   => [
               'type'   => 'number',
               'label'  => 'The best rating',
               'default'=> 5,
            ],
            'worstRating'  => [
               'type'   => 'number',
               'label'  => 'The worst rating',
               'default'=> 1,
            ]
      $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);
      }
   }
}