Follow up [How to check if JSON object is empty or the value] when required = true

230 views Asked by At

please pardon my way of asking question, but this is a follow up on my last question How to check if value is empty in PHP [JSON], I had 4 answers on it but I have not been able to solve it, please help me out, been trying for three days nothing,

I have Objects and value in my JSON below, what I am trying to achieve is simple, lets say "payment_type" object is empty, I want to tell the user its empty with response
payment_type parameter is required and if it's value is empty which is "1" I respond with payment_type value is required, I want to tell the user, my issue is that

// api.php

public function create_insert_new_delivery()
{
    $payment_type = $this->validateParameter(
        'payment_type',
        $this->param['payment_type'],
        STRING,
        true
    );

    $date_of_delivery = $this->validateParameter(
        'date_of_delivery',
        $this->param['date_of_delivery'],
        STRING,
        true
    );

    $waybill_number = $this->validateParameter(
        'your_generated_waybill_number',
        $this->param['your_generated_waybill_number'],
        STRING,
        true
    );
}



{
    "name":"create_insert_new_delivery",
    "param":{
       "payment_type":"1",
       "date_of_delivery":"B",
       "your_generated_waybill_number":"39ei40909444567avaaaaba",
    }
}

WHAT I TRIED BUT IT DOESN'T WORK WELL ANSWER FROM THE PREVIOUS QUESTION, it only shows this error "$this->throwError(EMPTY_PARAMETER, $fieldName . " value is required");"

// rest.php

/**
 * @param string $fieldName e.g payment_type
 * @param mixed $value e.g 1
 * @param bool $required is equal to true for strings
 */
public function validateParameter($fieldName, $value, $required = true){
    if ($required AND empty($fieldName)) {
        $this->throwError(API_PARAM_REQUIRED, $fieldName . " parameter is missing");
    } elseif ($required AND empty($value)) {
        $this->throwError(EMPTY_PARAMETER, $fieldName . " value is required");
    } elseif (!empty($fieldName) AND empty($value) AND $required){
        $this->throwError(EMPTY_PARAMETER, $fieldName . " value is required");
    }
}

In the screenshot of the postman call Image its responds with the  value is empty instead of field name is empty.

1

There are 1 answers

0
Christian On BEST ANSWER

If I understood your problem, you need to be careful when reading entries in an array that do not exist. PHP will trigger a warning/notice and pass a null value, which might not be what you expect. Here's the code that I'd use:

public function create_insert_new_delivery()
{
    $payment_type = $this->validateParameter(
        $this->param,
        'payment_type',
        STRING,
        true
    );

    $date_of_delivery = $this->validateParameter(
        $this->param,
        'date_of_delivery',
        STRING,
        true
    );

    $waybill_number = $this->validateParameter(
        $this->param,
        'your_generated_waybill_number',
        STRING,
        true
    );
}

public function validateParameter($params, $fieldName, $required = true)
{
    if ($required) {
        if (!array_key_exists($fieldName, $params)) {
            $this->throwError(API_PARAM_REQUIRED, "$fieldName parameter is missing");
        } elseif (empty($params[$fieldName])) {
            $this->throwError(EMPTY_PARAMETER, "$fieldName value is required");
        }
    }

    return $params[$fieldName];
}

What I did differently:

  • instead of passing the value, I passed the entire parameters array and did the check inside
  • I used array_key_exists, because empty() returns true when the value is a null (and you specifically want to check when the key is missing)
  • I simplified the conditions
  • I've returned the value (this was implied by the usage of the function, but missing in the original code)