I have created a custom api to get review and rating summary:
public function getRatingSummary($productId)
{
 $result =[];
 $product = $this->_productRepository->getById($productId);
 $idArray = [$productId];
 $reviewsCollection = $this->_reviewCollectionFactory->create()
 ->addFieldToFilter('entity_pk_value',array("in" => $idArray))
 ->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED)
 ->addRateVotes();
 $ratingSummary = $reviewsCollection->getSelect();
 $starsData = array();
 foreach ($reviewsCollection->getItems() as $review) {
 foreach( $review->getRatingVotes() as $_vote ) {
 $rating = [];
 $percent = $_vote->getPercent();
 $star = ($percent/20);
 $productId = $_vote->getEntityPkValue();
 $countReview = $this->_reviewFactoryCustom->create()->getTotalReviews($productId,false);
 $review_id = $_vote->getReviewId();
 $rating_id = $_vote->getRatingId();
 $rating['rating_id'] = $rating_id;
 $rating['review_id'] = $review_id;
 $rating['product_id'] = $productId; 
 $rating['percent'] = $percent;
 $rating['star'] = $star;
 $rating['nickname'] = $review->getNickname(); 
 $items[] = $rating;
 $starData[$star][] = $rating;
 $starsData[] = $rating;
 }
 }
 if(count($items) > 0) { 
 $results['all'] = $items;
 $result[1]['star'] = $starsData; 
 if(isset($starData[1]))
 $result[2]['count'][1] = count($starData[1]);
 else
 $result[2]['count'][1] = 0;
 if(isset($starData[2]))
 $result[2]['count'][2] = count($starData[2]);
 else
 $result[2]['count'][2] = 0;
 if(isset($starData[3]))
 $result[2]['count'][3] = count($starData[3]);
 else
 $result[2]['count'][3] = 0;
 if(isset($starData[4]))
 $result[2]['count'][4] = count($starData[4]);
 else
 $result[2]['count'][4] = 0;
 if(isset($starData[5]))
 $result[2]['count'][5] = count($starData[5]);
 else
 $result[2]['count'][5] = 0;
 $sum = $startSum = 0;
 foreach($result[2]['count'] as $number => $count) {
 $sum += $number * $count;
 $startSum += $count; 
 }
 $avg = $sum/$startSum;
 $result [3]['rating']['all'] = count($results['all']);
 $result [3]['rating']['avg'] = round($avg,1);
 }
 return $result;
}
But the response I get is :
 [
{
 "star": [
 {
 "rating_id": "1",
 "review_id": "3",
 "product_id": "9",
 "percent": "100",
 "star": 5,
 "nickname": "XXXXX"
 },
 {
 "rating_id": "3",
 "review_id": "3",
 "product_id": "9",
 "percent": "60",
 "star": 3,
 "nickname": "XXXXX"
 },
 {
 "rating_id": "2",
 "review_id": "3",
 "product_id": "9",
 "percent": "100",
 "star": 5,
 "nickname": "XXXXX"
 },
 {
 "rating_id": "1",
 "review_id": "4",
 "product_id": "9",
 "percent": "40",
 "star": 2,
 "nickname": "YYYY"
 },
 {
 "rating_id": "3",
 "review_id": "4",
 "product_id": "9",
 "percent": "80",
 "star": 4,
 "nickname": "YYYY"
 },
 {
 "rating_id": "2",
 "review_id": "4",
 "product_id": "9",
 "percent": "80",
 "star": 4,
 "nickname": "YYYY"
 }
 ]
},
{
 "count": {
 "1": 0,
 "2": 1,
 "3": 1,
 "4": 2,
 "5": 2
 }
},
{
 "rating": {
 "all": 6,
 "avg": 3.8
 }
}
]
I want my response as an array of same type of object or something like this:
{
"star": [
 {
 "rating_id": "1",
 "review_id": "3",
 "product_id": "9",
 "percent": "100",
 "star": 5,
 "nickname": "XXXX"
 },
 {
 "rating_id": "3",
 "review_id": "3",
 "product_id": "9",
 "percent": "60",
 "star": 3,
 "nickname": "XXXX"
 },
 {
 "rating_id": "2",
 "review_id": "3",
 "product_id": "9",
 "percent": "100",
 "star": 5,
 "nickname": "XXXX"
 },
 {
 "rating_id": "1",
 "review_id": "4",
 "product_id": "9",
 "percent": "40",
 "star": 2,
 "nickname": "YYYY"
 },
 {
 "rating_id": "3",
 "review_id": "4",
 "product_id": "9",
 "percent": "80",
 "star": 4,
 "nickname": "YYYY"
 },
 {
 "rating_id": "2",
 "review_id": "4",
 "product_id": "9",
 "percent": "80",
 "star": 4,
 "nickname": "YYYY"
 }
],
"count": {
 "1": 0,
 "2": 1,
 "3": 1,
 "4": 2,
 "5": 2
},
"rating": {
 "all": 6,
 "avg": 3.8
}
}
This is my first time working in magento. Can somebody please help me to achieve the desired output?
1 Answer 1
You need represent any staff that is {...} in json as PHP DTO Interaface(object).
Please pay attention that Magento supports only homogenous array (all elements must be the same type)
interface Some Service {
 function getRatingSummary(int $productId): RatingSummary { ... }
}
interface StatInfo {
 function getReviewId(): int
 function getProductId(): int
 function getPercent(): int,
 function getStar(): int
 function getNickname(): string
}
interface RateStat {
 function getAll(): int
 unction getAvg(): int
}
interface RatingSummary {
 function getStar(): StatInfo
 function getCount(): int[]
 function getRating() RateStat
}
Explore related questions
See similar questions with these tags.