Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.82 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Response;
  4.  
  5.  
  6. class ApiResponse
  7. {
  8.     private $response = [];
  9.  
  10.  
  11.     public function addProperty(string $name, string $value)
  12.     :self
  13.     {
  14.         $this->response[$name] = $value;
  15.         return $this;
  16.     }
  17.    
  18.     public function fromJson(string $json)
  19.     :self
  20.     {
  21.         //maybe allow to call this function when response is not touched, in other case throw exception?
  22.         //or remove this and do it in constructor
  23.         $this->response = json_decode($json);
  24.         return $this;
  25.     }
  26.  
  27.     public function fromArray(array $array)
  28.     : self
  29.     {
  30.         //same comment apply as for fromJson function
  31.         $this->response = $array;
  32.         return $this;
  33.     }
  34.  
  35.     public function toJson()
  36.     :string
  37.     {
  38.         return json_encode($this->response);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement