Skip to main content
Share Your Experience: Take the 2024 Developer Survey
1 of 5

PHP Class with static function and variables

I am experimenting with Five9 API and setup a php class to deal with the addRecordstoList method.

The main issue this class was meant to solve over the traditional function found in there documentation is that the function must map the index of the array to the correct field. Such as column 1 is populated by value of index 0 of the array.

This causes an issue when the lead array sent as a parameter has keys in different order or includes optional fields. It throws the indexes off and information on the records list is out of order when sent over.

I would like some input on the class and how to make it faster and allow to sendtoFive9 function to be looped without overriding the array.

    //how the columns are mapped in the example provided by five9
    $listUpdateSettings = array ( "fieldsMapping" => array (
     array ( "columnNumber" => '1', "fieldName" =>
    "number1", "key" => true ),
     array ( "columnNumber" => '2', "fieldName" => "first_
    name", "key" => false ),
     array ( "columnNumber" => '3', "fieldName" => "last_
    name", "key" => false) );
    
    class five9{
    
      //the fields mapped to five9
      //initialize and empty array so there are no errors with array_push
      private static $listUpdateSettings = array("fieldsMapping" => array());
    
      //maps field to five9 columns
      protected function mapField($index, $field, $key = false){
        //map the field to five9. Index must start at 1 not 0
        //mapping fields this way allows flexibility in which order the parameter has its keys
        array_push( self::$listUpdateSettings["fieldsMapping"], array( "columnNumber" => $index + 1, "fieldName" => $field, "key" => $key ));
      }
    
      //returns data array after being scrubbed
      protected function checkArray( array $lead){
    
        //data sent to five9
        $data = array();
    
        //counter
        $i = 0;
    
        //five9 requires the $field names outlined below
        foreach($lead as $field => $value){
          //make sure first_name is 3 characters or more
          if( ($field == 'first_name' && strlen($field) > 3)){
            //add field to array
            $data[$i] = $value;
            //map the field in five9
            self::mapField($i, $field);
          }
          //make sure last_name is 3 characters or more
          if($field == 'last_name' && strlen($field) > 3 ){
            //add field to array
            $data[$i] = $value;
            //map the field in five9
            self::mapField($i, $field);
          }
          //if the field is a phone number
          if( $field == 'number1' ){
            //add field to array
            $data[$i] = preg_replace("/[^0-9]/", "", $value);
            //map the field in five9
            //this was they key for my instance
            self::mapField($i, $field, true);
          }
          //if the field is a phone number
          if( $field == 'number2' ){
            //add field to array
            $data[$i] = preg_replace("/[^0-9]/", "", $value);
            //setup column mapping in five9
            self::mapField($i, $field);
          }
          //make sure the state is only two characters
          if($field == 'state' && strlen($value) <= 2){
            //add field to array
            $data[$i] = $value;
            //setup column mapping in five9
            self::mapField($i, $field);
          }
          //make sure the state is only two characters
          if($field == 'zip' && strlen($value) <= 5){
            //add field to array
            $data[$i] = $value;
            //setup column mapping in five9
            self::mapField($i, $field);
          }
          //make sure memberid is an int
          if($field == 'member_id' && is_numeric($value)){
            //add field to array
            $data[$i] = $value;
            //setup column mapping in five9
            self::mapField($i, $field);
          }
          //increase the counter
          $i++;
        }
        //return the data array that is constructed
        return $data;
    
      }
    
      static function sendToFive9(array $lead ){
    
        //the conctructed array
        $data = self::checkArray($lead);
    
        //if the fields sent are all correct both arrays are the same size
        if(sizeof($lead) === sizeof($data) ){
    
            // Import the WSDL and authenticate the user.-----------------------------
            $wsdl_five9 = "https://api.five9.com/wsadmin/v2/AdminWebService?wsdl&user=$username";
    
            //try to authenticate with five9
            try{
                $soap_options = array( 'login'    => '$username',
                                       'password' => '$password',
                                       'trace' => true );
    
                $client_five9 = new SoapClient( $wsdl_five9 , $soap_options );
            }//if errors occur add the message to the response array
            catch (Exception $e){
                $error_message = $e->getMessage();
                $response['error'] = $error_message;
            }
    
    
            //settings required by five9
            self::$listUpdateSettings["skipHeaderLine"] = false;
            self::$listUpdateSettings["cleanListBeforeUpdate"] = false;
            self::$listUpdateSettings["crmAddMode"] = 'ADD_NEW';
            self::$listUpdateSettings["crmUpdateMode"] = 'UPDATE_SOLE_MATCHES';
            self::$listUpdateSettings["listAddMode"] = 'ADD_IF_SOLE_CRM_MATCH';
    
            //the default list for all new leads
            $list = "test";
    
            //prepare the query used to add the record to five9
            $query = array ( 'listName' => "$list",
                             'listUpdateSettings' => self::$listUpdateSettings,
                             'record' => $data );
    
            //get the result from running the query
            //this will return an object
            $result = $client_five9->AddRecordToList($query);
    
            //get the array of variables within the results object
            $variables = get_object_vars($result);
    
            //get the array of varaibles within the return array of objects
            $resp = get_object_vars($variables['return']);
    
            //if there was an error adding the record
            if($resp['failureMessage'] != ""){
              $response['errors'] = $resp['failureMessage'];
            }
           //if it was successful either adding or updating
            if($resp['crmRecordsUpdated'] == 1 || $resp['crmRecordsInserted'] == 1){
              $response['success'] = true;
              //reset the settings array so this class can be looped without passing the lists back and forth
              self::$listUpdateSettings = array("fieldsMapping" => array());
            }
    
        }//end if
        else{
            //return the differences in the arrays usually caused due to improper names
            $reponse["errors"] = array_diff($lead, $data);
    
        }
    
        return $response;
    
      }//end function
}