DataRequest

Class \Lib\DataRequest

Introduction

This class will allow you to extract GET or POST data that has been sent through the web service call.

Methods

getInstance

<?php public function \Lib\DataRequest::getInstance()

Description
Instanciate DataRequest class.

getRequest

<?php public static function \Lib\DataRequest::getRequest(bool $merged = false)

Description
Returns all content sent with POST or GET.

Parameters

  • merged : If is set to true, all content will be merged into one single array.

Return
An array representing the request's data.

get

<?php public function \Lib\DataRequest::get($property = '')

Description
Returns content sent with specified property.
Property needs to be specified HTTP method (post, get, put, patch, delete)

Parameters

  • property : The property's name. It's an HTTP method : post, get, put, patch or delete

Return
The property's value, null otherwise.


Examples

A complete webservice GET

Retrieves a user by id and returns it.

<?php

namespace App\Modules;

use \Lib\Controller\WSController as Controller;

class Users
{
    
    /**
     * @description Retrieve and return a user based on a GET parameters (id)
     * @param Controller $controller
     * @param array $params
     * @doc integer $id Identifiant du diagnostic
     * @return array|null
     */
    public static function get(Controller $controller, $params = array())
    {
        $get = \Lib\DataRequest::getInstance()->get('get');
      
        if (!empty($get['id'])) {

            $db = \Lib\DB::getInstance();
            $db->query('
    				SELECT
    				*
    				FROM
    				`users`
    				WHERE
    				`id` = :id
    				AND active = 1
    				');

            $db->bind('id', (int)$get['id'], \PDO::PARAM_INT);

            $user = $db->fetch();
          
          	return $user;
        }
      
      	return null;
		}
}

A complete webservice POST

Create a user inside database and return a success message.

<?php

namespace App\Modules;

use \Lib\Controller\WSController as Controller;

class Users
{
    
    /**
     * @description Create a user
     * @param WSController $controller
     * @param array $params
     * @doc string $firstname User's first name
     * @doc string $lastname User's last name
     * @doc string $email User's email
     * @return array
     */
    public static function post(Controller $controller, $params = array())
    {
        $post = \Lib\DataRequest::getInstance()->get('post');

        if (empty($post['firstname'])) {
            return \Lib\Tools::formatError('firstname_missing', true, 3, 403);
        }

        if (empty($post['lastname'])) {
            return \Lib\Tools::formatError('lastname_missing', true, 3, 403);
        }
  
        if (empty($post['email'])) {
            return \Lib\Tools::formatError('email_missing', true, 3, 403);
        }

        $db = \Lib\DB::getInstance();
        $db->query('
                    INSERT INTO
                    `users`
                    (
                    `lastname`,
                    `firstname`,
                    `email`,
                    `date_create`
                    )
                    VALUES
                    (
                    :lastname,
                    :firstname,
                    :email,
                    NOW()
                    )
        ');

        $db->bind(':lastname', $post['lastname']);
        $db->bind(':firstname', $post['firstname']);
        $db->bind(':email', $post['email']);

        $db->exec();

        return \Lib\Tools::formatSuccess('user_created',true,201);
    }
}