0

I am having a little issue when I try to receive multiple array values in PHP for validation. I have this object with values in POSTMAN raw form, application/json :

{
  "name":"create_insert_new_delivery",
  "param":{
       "email_of_shipment_owner":"[email protected]",
       "shipment_type ":"B",
       "item_name": ["50", "60"],
       "item_weight ": ["H", "I"],
       "item_length": ["70", "90"]
 }
}

When I try to receive $item_name = $this->validateParameter('item_name', $this->param['item_name'], ARRAY, true); like the two strings below are received I get this

**ERROR : **

[04-Jul-2018 13:16:58 UTC] PHP Parse error: syntax error, unexpected ',', expecting '(' in /home/osconliz/public_html/Osconlizapicall/api.php on line 304

The receiving code below.

LINE 302

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

LINE 303

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

LINE 304

$item_name = $this->validateParameter('item_name', $this->param['item_name'], ARRAY, true);

LINE 305

 $item_weight = $this->validateParameter('item_weight', $this->param['item_weight'], ARRAY, true);

LINE 306

$item_length = $this->validateParameter('item_length', $this->param['item_length'], ARRAY, true);

but the STRINGS are received properly :

public function validateParameter($fieldName, $value, $dataType, $required = true){
        switch ($dataType) {
            case BOOLEAN:
            if (!is_bool($value)){
                $this->throwError(VALIDATE_PARAMETER_DATATYPE, "Datatype is not valid for " . $fieldName . ' it should be boolean.');
            }
            break;
            case INTEGER:
            if (!is_numeric($value)){
                $this->throwError(VALIDATE_PARAMETER_DATATYPE, "Datatype is not valid for " . $fieldName . ' it should be integer.');
            }
            break;
            case STRING:
            if (!is_string($value)){
                $this->throwError(VALIDATE_PARAMETER_DATATYPE, "Datatype is not valid for " . $fieldName . ' it should be string.');
            }
            break;
            case ARRAY:
            if (!is_array($value)){
                $this->throwError(VALIDATE_PARAMETER_DATATYPE, "Datatype is not valid for " . $fieldName . ' it should be array.');
            }
            break;

            default:
            $this->throwError(VALIDATE_PARAMETER_DATATYPE, "Datatype is not valid for " . $fieldName);
            break;

        }

        return $value;
  }

After receiving I want validate data like this

    if ($this->item_category == ""){

          } else {


          if ($this->item_category == "A" || $this->item_category == "B" || $this->item_category == "C" || $this->item_category == "D" || $this->item_category == "E" || $this->item_category == "F" || $this->item_category == "G" || $this->item_category == "H") {

          } else {
            $this->throwError(INVALID_DATA_TTT, "Invalid item category");
           exit();  
          }
          } 
8
  • 1
    Following the error, this has nothing to do with your logic and is just a , that is floating around somewhere. I haven't spotted it yet though. Commented Jul 4, 2018 at 13:52
  • @loek Thanks, but is it the correct way of receiving "$item_name = $this->validateParameter('item_name', $this->param['item_name'], ARRAY, true);" an array format. Commented Jul 4, 2018 at 13:53
  • @loek the previous lines before it present no syntax error Commented Jul 4, 2018 at 13:53
  • It's actually very logical, but I didn't expect it at all. See my answer. Commented Jul 4, 2018 at 13:56
  • @loek thanks for the answer but could you give an example cause I dont seem to grab "This is because ARRAY is an apparently case-insensitive function for creating arrays" especially this part "The easiest way to fix this, is to just pick another keyword I guess?" cause array is the receiving format just like th string listed above. Commented Jul 4, 2018 at 13:59

1 Answer 1

3

This is because ARRAY is an apparently case-insensitive function for creating arrays. See it here: https://3v4l.org/keu3K

The easiest way to fix this, is to just pick another keyword I guess?

EDIT - For reference, all the reserved keywords and functions in php: https://secure.php.net/manual/en/reserved.keywords.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.