Beispiel #1
0
 public function createOrUpdate($name)
 {
     $city = self::find($name);
     if ($city) {
         return $city;
     } else {
         $res = RestLayer::post('v1/cities', array("name" => $name), array('Authorization' => "Bearer " . self::token()))[2];
         return json_decode($res, true)['id'];
     }
 }
Beispiel #2
0
 public static function get()
 {
     $res = RestLayer::get('v1/token', array(), array('Authentication' => "Key " . Settings::$appSecret));
     if ($res[0] == 200) {
         $body = json_decode($res[2], true);
         $expiration = date("U", strtotime($body['expired_at']));
         self::$access_token = $body['token'];
         self::$expiration = $expiration;
         self::$retailer_id = $body['retailer_id'];
     }
     return self::$access_token;
 }
Beispiel #3
0
 public function find($name)
 {
     $res = RestLayer::get('v1/brands/search', array("q" => $name), array('Authorization' => 'Bearer ' . self::token()));
     if ($res[0] == 200) {
         return self::from_json($res[1]);
     } else {
         if ($res[0] == 404) {
             return null;
         } else {
             throw new \Exception("Unexpected HTTP status code {$res[0]}, {$res[1]}");
         }
     }
 }
Beispiel #4
0
 public function save()
 {
     $method = "post";
     $endpoint = "v1/offers";
     $fields = [];
     foreach (array("title", "description", "price", "original_price", "discount", "start_date", "end_date", "image") as $field) {
         if (!empty($this->{$field})) {
             $fields[$field] = $this->{$field};
         }
     }
     $fields["store_ids"] = $this->store_ids;
     $res = RestLayer::request($method, $endpoint, $fields, array("Authorization" => "Bearer " . self::token(), "Content-Type" => "application/json"));
     if ($res[0] == 200 or $res[0] == 201) {
     } elseif ($res[0] == 400) {
         throw new \Exception("Bad Request! Error: {$res[1]["errors"]}", 1);
     } else {
         throw new \Exception("Unexpected HTTP status code {$res[0]}, {$res[1]}", 1);
     }
 }
Beispiel #5
0
 public function save()
 {
     if ($this->id != null) {
         $method = "put";
         $endpoint = "v1/leaflets/{$this->id}";
         $expected_status = 200;
     } else {
         $method = "post";
         $endpoint = "v1/leaflets";
         $expected_status = 201;
     }
     $fields = [];
     foreach (array('name', 'url') as $field) {
         if ($this->{$field} == null) {
             throw new \Exception("Missing required {$field} field!", 1);
         } else {
             $fields[$field] = $this->{$field};
         }
     }
     //$fields['name'] = ($this->name == null) ? null : $this->name;
     //$fields['url'] = ($this->url == null) ? null : $this->url;
     $fields['start_date'] = $this->start_date == null ? null : $this->start_date;
     $fields['end_date'] = $this->end_date == null ? null : $this->end_date;
     $fields['pdf_data'] = $this->pdf_data == null ? null : $this->pdf_data;
     $fields['image_urls'] = empty($this->image_urls) ? array() : json_encode(array_values($this->image_urls));
     $fields['store_ids'] = empty($this->store_ids) || empty(array_values($this->store_ids)) ? array() : json_encode(array_values($this->store_ids));
     $res = RestLayer::request($method, $endpoint, $fields, array('Authorization' => "Bearer " . self::token()));
     if ($res[0] != $expected_status) {
         throw new \Exception("Unexpected HTTP status code {$res[0]}, {$res[1]}");
     } else {
         if ($method == "post") {
             $this->id = json_decode($res[2], true)['id'];
         }
     }
     return $this->id;
 }
Beispiel #6
0
 public function save()
 {
     if ($this->id != null) {
         $method = "put";
         $url = "v1/stores/{$this->id}";
         $expected_status = 200;
     } else {
         $method = "post";
         $url = "v1/stores";
         $expected_status = 201;
     }
     $fields = array();
     foreach (array('name', 'address', 'zipcode', 'origin') as $field) {
         if ($this->{$field} == null) {
             throw new \Exception("Missing required {$field} field", 1);
         } else {
             $fields[$field] = $this->{$field};
         }
     }
     if ($this->city == null and $this->city_id == null) {
         throw new \Exception("city or city_id must be set");
     }
     $fields["city_id"] = $this->city_id == null ? null : $this->city_id;
     $fields["city"] = $this->city == null ? null : $this->city;
     $fields['phone'] = $this->phone == null ? null : $this->phone;
     $fields['opening_hours'] = empty($this->opening_hours) ? null : json_encode($this->opening_hours);
     $fields['opening_hours_text'] = $this->opening_hours_text == null ? null : $this->opening_hours_text;
     $fields['latitude'] = $this->latitude == null ? null : $this->latitude;
     $fields['longitude'] = $this->longitude == null ? null : $this->longitude;
     $res = RestLayer::request($method, $url, $fields, array('Authorization' => "Bearer " . self::token()));
     if ($res[0] != $expected_status) {
         if (is_array($res[1])) {
             throw new \Exception("Unexpected HTTP status code {$res[0]}", 1);
         } else {
             throw new \Exception("Unexpected HTTP status code {$res[0]}, {$res[1]}", 1);
         }
     } else {
         if ($method == "post") {
             $this->id = json_decode($res[2], true)['id'];
         }
     }
     return $this->id;
 }