on 622fdea help sign in
155 lines 3.6 kB history raw
1<?php
2/**
3 * Created by PhpStorm.
4 * User: glali
5 * Date: 2017-04-27
6 * Time: 14:55
7 */
9namespace TANIOS\Airtable;
11class Request implements \ArrayAccess
14 /**
15 * @var Airtable Instance of Airtable
16 */
17 private $airtable;
18 /**
19 * @var resource Instance of CURL
20 */
21 private $curl;
22 /**
23 * @var string Content type
24 */
25 private $content_type;
26 /**
27 * @var array Request data
28 */
29 private $data = [];
30 /**
31 * @var bool Is it a POST request?
32 */
33 private $is_post = false;
35 /**
36 * @var array|boolean Relations to lazy load
37 */
38 private $relations;
40 /**
41 * Create a Request to AirTable API
42 * @param Airtable $airtable Instance of Airtable
43 * @param string $content_type Content type
44 * @param array $data Request data
45 * @param bool|string $is_post Is it a POST request?
46 */
47 public function __construct( $airtable, $content_type, $data = [], $is_post = false, $relations = false )
48 {
50 $this->airtable = $airtable;
51 $this->content_type = $content_type;
52 $this->data = $data;
53 $this->is_post = $is_post;
54 $this->relations = $relations;
56 }
58 private function init()
59 {
61 $headers = array(
62 'Content-Type: application/json',
63 sprintf('Authorization: Bearer %s', $this->airtable->getKey())
64 );
66 $request = $this->content_type;
68 if( ! $this->is_post )
69 {
70 if (!empty($this->data)){
71 $data = http_build_query($this->data);
72 $request .= "?" . $data;
73 }
74 }
76 $curl = curl_init($this->airtable->getApiUrl($request));
78 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
79 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
81 if( $this->is_post )
82 {
83 if( strtolower( $this->is_post ) == 'patch' )
84 {
85 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
86 }
87 else if( strtolower( $this->is_post ) == 'delete' )
88 {
89 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
90 }
91 curl_setopt($curl,CURLOPT_POST, count($this->data));
92 curl_setopt($curl,CURLOPT_POSTFIELDS, json_encode($this->data));
93 }
95 $this->curl = $curl;
97 }
99 /**
100 * @return Response Get response from API
101 */
102 public function getResponse()
103 {
105 $this->init();
107 $response_string = curl_exec( $this->curl );
109 $response = new Response( $this->airtable, $this, $response_string, $this->relations );
111 return $response;
113 }
115 public function __set( $key, $value )
116 {
117 if( ! is_array( $this->data ) )
118 {
119 $this->data = [];
120 }
122 $this->data[ $key ] = $value;
123 }
125 public function offsetExists($offset)
126 {
127 return is_array( $this->data) && isset( $this->data[ $offset ] );
128 }
130 public function offsetGet($offset)
131 {
132 return is_array( $this->data ) && isset( $this->data[ $offset ] )
133 ? $this->data[ $offset ]
134 : null;
135 }
137 public function offsetSet($offset, $value)
138 {
139 if( ! is_array( $this->data ) )
140 {
141 $this->data = [];
142 }
144 $this->data[ $offset ] = $value;
145 }
147 public function offsetUnset($offset)
148 {
149 if( is_array( $this->data ) && isset( $this->data[ $offset ] ) )
150 {
151 unset( $this->data[ $offset ] );
152 }
153 }