MyAllocator BuildToUs API PHP SDK for OTA's
  • Namespace
  • Class
  • Tree

Namespaces

  • MyAllocator
    • phpsdkota
      • src
        • Api
          • Inbound
        • Object

Classes

  • MaError
  • MaResponse
  1 <?php
  2 /**
  3  * MyAllocator BuildToUs PHP SDK Response Object
  4  *
  5  * @package     myallocator/myallocator-php-sdk-ota
  6  * @author      Nathan Helenihi <support@myallocator.com>
  7  * @copyright   Copyright (c) MyAllocator
  8  * @license     http://mit-license.org/
  9  * @link        https://github.com/MyAllocator/myallocator-php-sdk-ota
 10  */
 11 
 12 namespace MyAllocator\phpsdkota\src\Object;
 13 use MyAllocator\phpsdkota\src\Api\Inbound\MaInboundInterface;
 14 use MyAllocator\phpsdkota\src\Object\MaError;
 15 
 16 class MaResponse
 17 {
 18     /**
 19      * @var boolean The result: true = success, false = error
 20      */
 21     public $success = true;
 22 
 23     /**
 24      * @var boolean Data to be returned.
 25      */
 26     public $data = null;
 27 
 28     /**
 29      * @var array Array of errors. [{id:'', type:'', msg:''}, ...]
 30      */
 31     public $errors = array();
 32 
 33     /**
 34      * @var array Interface to OTA backend. Only used for logging here.
 35      */
 36     protected $logger = null;
 37 
 38     /**
 39      * Class contructor.
 40      *
 41      * @param \MyAllocator\phpsdkota\src\Api\Inbound\MaInboundInterface
 42      *  $interface The object implementing MaInboundInterface to invoke
 43      *  backend functionality.
 44      */
 45     public function __construct(MaInboundInterface $logger = null)
 46     {
 47         // Set inbound interface
 48         $this->logger = $logger;
 49     }
 50 
 51     /**
 52      * Set response to success and return an object response.
 53      *
 54      * @param mixed $data Data to return in successful response.
 55      *
 56      * @return \MyAllocator\phpsdkota\src\Object\MaResponse Response object.
 57      */
 58     public function success($data = null)
 59     {
 60         $this->success = true;
 61         $this->data = $data;
 62         return $this;
 63     }
 64 
 65     /**
 66      * Add an error to the response object.
 67      *
 68      * @param int $error The Error code.
 69      * @param string $msg Custom error message.
 70      *
 71      * @return \MyAllocator\phpsdkota\src\Object\MaResponse Response object.
 72      */
 73     public function error($id, $data = null)
 74     {
 75         $this->success = false;
 76         $error = new \MyAllocator\phpsdkota\src\Object\MaError($id, $data);
 77         $this->log("Error", json_encode($error->toArray()));
 78         $this->errors[] = $error;
 79         return $this;
 80     }
 81 
 82     /**
 83      * Return array response.
 84      *
 85      * @return array Response array.
 86      */
 87     public function response()
 88     {
 89         $rsp = $this->toArray();
 90         $this->log("Response", json_encode($rsp));
 91         return $rsp; 
 92     }
 93 
 94     /**
 95      * Convert response object to an array.
 96      *
 97      * @return array Response array.
 98      */
 99     public function toArray()
100     {
101         $response = array(
102             'success' => $this->success,
103             'errors' => array()
104         );
105 
106         // Add data to top level
107         if ($this->data) {
108             foreach($this->data as $k => $v) {
109                 $response[$k] = $v;
110             }
111         }
112 
113         foreach ($this->errors as $err) {
114             $response['errors'][] = $err->toArray();
115         }
116 
117         // If no errors, set errors null
118         if (empty($response['errors'])) {
119             $response['errors'] = null;
120         }
121 
122         return $response;
123     }
124 
125     /**
126      * Set interface for logging.
127      *
128      * @return array Response array.
129      */
130     public function setLogger(MaInboundInterface $logger)
131     {
132         $this->logger = $logger;
133     }
134 
135     private function log($str, $data = null)
136     {
137         if ($this->logger) {
138             $this->logger->log($str, $data);
139         }
140     }
141 }
142 
MyAllocator BuildToUs API PHP SDK for OTA's API documentation generated by ApiGen