Applying Data Filtering and Aggregation in a User Management System Using PHP

Introduction

Welcome to today's lesson on applying data filtering and aggregation in a real-world scenario using a user management system. We'll start by building a foundational structure that can handle basic user operations. Then, we'll expand it by introducing more advanced functionalities that allow filtering and aggregating user data.

Starter Task Methods

In our starter task, we will implement a class that manages basic operations on a collection of user data, specifically handling adding new users, retrieving user profiles, and updating user profiles.

Here are the starter task methods rewritten for PHP:

  • addUser($user_id, $age, $country, $subscribed) - Adds a new user with the specified attributes and returns true if the user was added successfully, or false if a user with the same $user_id already exists.

  • getUser($user_id) - Returns the user's profile if the user exists; otherwise, returns null.

  • updateUser($user_id, $age = null, $country = null, $subscribed = null) - Updates the user's profile based on non-null parameters. Returns true if the user exists and was updated, false otherwise.

To store the user data, we will use associative arrays.

Starter Task Implementation

Here is the implementation of our starter task in PHP:

<?php

class UserManager {
    private $users = [];

    public function addUser($user_id, $age, $country, $subscribed) {
        if (isset($this->users[$user_id])) {
            return false;
        }
        $this->users[$user_id] = [
            'age' => $age,
            'country' => $country,
            'subscribed' => $subscribed
        ];
        return true;
    }

    public function getUser($user_id) {
        return $this->users[$user_id] ?? null;
    }

    public function updateUser($user_id, $age = null, $country = null, $subscribed = null) {
        if (!isset($this->users[$user_id])) {
            return false;
        }
        if ($age !== null) {
            $this->users[$user_id]['age'] = $age;
        }
        if ($country !== null) {
            $this->users[$user_id]['country'] = $country;
        }
        if ($subscribed !== null) {
            $this->users[$user_id]['subscribed'] = $subscribed;
        }
        return true;
    }
}

// Example usage
$um = new UserManager();
echo $um->addUser("u1", 25, "USA", true) ? "true\n" : "false\n";  // true
echo $um->addUser("u2", 30, "Canada", false) ? "true\n" : "false\n";  // true
echo $um->addUser("u1", 22, "Mexico", true) ? "true\n" : "false\n";  // false

$user = $um->getUser("u1");
if ($user !== null) {
    echo $user['age'] . "\n";  // 25
}

echo $um->updateUser("u1", 26) ? "true\n" : "false\n";  // true
echo $um->updateUser("u3", 19, "UK", false) ? "true\n" : "false\n";  // false
?>

This implementation covers all our starter methods. Let's move forward and introduce more complex 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