Mastering Data Aggregation and Formatting in PHP

Introduction

Welcome to our lesson on mastering data aggregation and data streams with PHP. In this lesson, you'll learn to build a basic sales records aggregator using PHP's associative arrays and classes. Then, we'll extend its functionality to handle more complex operations such as filtering, data aggregation, and formatting. By the end of this session, you'll be proficient in managing and formatting data streams efficiently in PHP.

Starter Task Methods and Their Definitions

To get started, we'll create a simple sales record aggregator in PHP. Here are the methods we'll focus on:

  • public function addSale(string $saleId, float $amount, string $date): void; — Adds or updates a sale record with a unique identifier saleId, amount, and a date in the format "YYYY-MM-DD".

  • public function getSale(string $saleId): ?float; — Retrieves the sale amount associated with the saleId. If the sale does not exist, it returns null.

  • public function deleteSale(string $saleId): bool; — Deletes the sale record with the given saleId. Returns true if the sale was deleted and false if the sale does not exist.

Are these methods clear so far? Great! Let's now look at how we would implement them.

Starter Task Implementation

Here is the complete code for the starter task:

PHP
<?php

class SalesAggregator {
    private $sales = [];

    public function addSale(string $saleId, float $amount, string $date): void {
        $this->sales[$saleId] = ['amount' => $amount, 'date' => $date];
    }

    public function getSale(string $saleId): ?float {
        if (isset($this->sales[$saleId])) {
            return $this->sales[$saleId]['amount'];
        }
        return null;
    }

    public function deleteSale(string $saleId): bool {
        if (isset($this->sales[$saleId])) {
            unset($this->sales[$saleId]);
            return true;
        }
        return false;
    }
}

// Example Usage
$aggregator = new SalesAggregator();

// Add sales
$aggregator->addSale("001", 100.50, "2023-01-01");
$aggregator->addSale("002", 200.75, "2023-01-15");

// Get sale
$sale = $aggregator->getSale("001");
if ($sale !== null) {
    echo $sale . PHP_EOL; // Output: 100.5
}

// Delete sale
echo $aggregator->deleteSale("002") ? 'true' : 'false'; // Output: true
echo PHP_EOL;
$sale = $aggregator->getSale("002");
if ($sale === null) {
    echo "Sale not found" . PHP_EOL; // Output: Sale not found
}
?>

Explanation:

  • The $sales array stores sale records with saleId as the key and an associative array of amount and date as the value.
  • addSale adds a new sale or updates an existing sale ID.
  • getSale retrieves the amount for a given sale ID or returns null if the sale does not exist.
  • deleteSale removes the sale record for the given sale ID or returns false if the sale does not exist.

Now that we have our basic aggregator, let's extend it to include more advanced functionalities.

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