Managing Product Reviews and Data Aggregation in PHP

Introduction

Hello, and welcome to today's lesson! Today, we are going to dive into the world of managing product reviews and applying data aggregation in practice. We will start with a relatively simple Starter Task to set up our base and then gradually build up to a more complex solution involving data aggregation. Let's jump in!

Starter Task: Methods and Their Definitions

For our starter task, we will lay the foundation by implementing basic operations for managing product reviews. We will be using classes to encapsulate the details of a product review. Our Review class will consist of the following properties:

  • $text — The textual content of the review.
  • $rating — The rating score of the review, ranging from 1 to 5.
  • $flagged — A boolean indicating whether the review is flagged as inappropriate.

These are the methods we will implement in a ReviewManager class:

  • addReview($productId, $reviewId, $reviewText, $rating) — Adds a review to the product specified by $productId. If a review with $reviewId already exists, it updates the existing review. Returns true if the review was added or updated successfully, false otherwise. For newly added items, the $flagged attribute is set to false initially.

  • getReview($productId, $reviewId) — Returns the review details ($text, $rating, and $flagged fields) for the review specified by $reviewId under the given $productId. If the review or product does not exist, returns null.

  • deleteReview($productId, $reviewId) — Deletes the review specified by $reviewId under the given $productId. Returns true if the review was deleted, false otherwise.

Starter Task Implementation

Let's look at the code that implements these functionalities in PHP:

PHP
<?php

class Review {
    public $text;
    public $rating;
    public $flagged;

    public function __construct($text, $rating, $flagged = false) {
        $this->text = $text;
        $this->rating = $rating;
        $this->flagged = $flagged;
    }
}

class ReviewManager {
    private $products = [];

    public function addReview($productId, $reviewId, $reviewText, $rating) {
        if ($rating < 1 || $rating > 5) {
            return false; // Invalid rating
        }
        $this->products[$productId][$reviewId] = new Review($reviewText, $rating);
        return true;
    }

    public function getReview($productId, $reviewId) {
        if (isset($this->products[$productId][$reviewId])) {
            return $this->products[$productId][$reviewId];
        }
        return null;
    }

    public function deleteReview($productId, $reviewId) {
        if (isset($this->products[$productId][$reviewId])) {
            unset($this->products[$productId][$reviewId]);
            if (empty($this->products[$productId])) {
                unset($this->products[$productId]); // Remove product if no reviews left
            }
            return true;
        }
        return false;
    }
}

// Example usage
$reviewManager = new ReviewManager();

// Adding some reviews
$reviewManager->addReview("p1", "r1", "Great product!", 5);
$reviewManager->addReview("p1", "r2", "Not bad", 3);

// Testing getReview method
$reviewR1 = $reviewManager->getReview("p1", "r1");
if ($reviewR1 !== null) {
    echo "Review r1 for p1: {";
    echo "text: " . $reviewR1->text . ", ";
    echo "rating: " . $reviewR1->rating . ", ";
    echo "flagged: " . ($reviewR1->flagged ? "true" : "false") . "}\n";
} else {
    echo "Review r1 for p1 not found.\n";
}

$reviewR3 = $reviewManager->getReview("p1", "r3");
if ($reviewR3 !== null) {
    echo "Review r3 for p1 found.\n";
} else {
    echo "Review r3 for p1 not found.\n";
}

// Testing deleteReview method
if ($reviewManager->deleteReview("p1", "r2")) {
    echo "Review r2 for p1 deleted successfully.\n";
} else {
    echo "Failed to delete review r2 for p1.\n";
}

$reviewR2 = $reviewManager->getReview("p1", "r2");
if ($reviewR2 !== null) {
    echo "Review r2 for p1 found.\n";
} else {
    echo "Review r2 for p1 not found.\n";
}

This code establishes the foundational methods needed for managing product reviews within a ReviewManager class. The addReview method allows for adding a new review or updating an existing one, ensuring each review contains valid rating values between 1 and 5. The getReview method retrieves the review details for a specific product, including the review text and rating, returning null if the product or review doesn't exist. The deleteReview method facilitates the removal of a specific review, and if no reviews are left for a product, the product itself is removed from the product list. Together, these methods form the basic operations required to manage a collection of product reviews efficiently.

Now, let's extend this with new features.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal