Version 1
[yaffs-website] / vendor / jcalderonzumba / gastonjs / src / NetworkTraffic / Request.php
1 <?php
2
3 namespace Zumba\GastonJS\NetworkTraffic;
4
5 /**
6  * Class Request
7  * @package Zumba\GastonJS\NetworkTraffic
8  */
9 class Request {
10   /** @var array */
11   protected $data;
12   /** @var array */
13   protected $responseParts;
14
15
16   /**
17    * @param array $data
18    * @param array $responseParts
19    */
20   public function __construct($data, $responseParts = null) {
21     $this->data = $data;
22     $this->responseParts = $this->createResponseParts($responseParts);
23   }
24
25   /**
26    * Creates an array of Response objects from a given response array
27    * @param $responseParts
28    * @return array
29    */
30   protected function createResponseParts($responseParts) {
31     if ($responseParts === null) {
32       return array();
33     }
34     $responses = array();
35     foreach ($responseParts as $responsePart) {
36       $responses[] = new Response($responsePart);
37     }
38     return $responses;
39   }
40
41   /**
42    * @return array
43    */
44   public function getResponseParts() {
45     return $this->responseParts;
46   }
47
48   /**
49    * @param array $responseParts
50    */
51   public function setResponseParts($responseParts) {
52     $this->responseParts = $responseParts;
53   }
54
55   /**
56    * Returns the url where the request is going to be made
57    * @return string
58    */
59   public function getUrl() {
60     //TODO: add isset maybe?
61     return $this->data['url'];
62   }
63
64   /**
65    * Returns the request method
66    * @return string
67    */
68   public function getMethod() {
69     return $this->data['method'];
70   }
71
72   /**
73    * Gets the request headers
74    * @return array
75    */
76   public function getHeaders() {
77     //TODO: Check if the data is actually an array, else make it array and see implications
78     return $this->data['headers'];
79   }
80
81   /**
82    * Returns if exists the request time
83    * @return \DateTime
84    */
85   public function getTime() {
86     if (isset($this->data['time'])) {
87       $requestTime = new \DateTime();
88       //TODO: fix the microseconds to miliseconds
89       $requestTime->createFromFormat("Y-m-dTH:i:s.uZ", $this->data["time"]);
90       return $requestTime;
91     }
92     return null;
93   }
94
95 }