/** * Process an error payload from the Graph API and return the appropriate * exception subclass. * @param ResponseInterface $response * @return RequestException */ public static function create(ResponseInterface $response) { $error_data = static::getErrorData($response->getContent()); if (in_array($error_data['error_subcode'], array(458, 459, 460, 463, 464, 467)) || in_array($error_data['code'], array(100, 102, 190)) || $error_data['type'] === 'OAuthException') { return new AuthorizationException($response); } elseif (in_array($error_data['code'], array(1, 2))) { return new ServerException($response); } elseif (in_array($error_data['code'], array(4, 17, 341))) { return new ThrottleException($response); } elseif ($error_data['code'] == 506) { return new ClientException($response); } elseif ($error_data['code'] == 10 || $error_data['code'] >= 200 && $error_data['code'] <= 299) { return new PermissionException($response); } else { return new self($response); } }
/** * @param ResponseInterface $response * @return array * @throws \InvalidArgumentException */ protected function assureResponseData(ResponseInterface $response) { $content = $response->getContent(); if (!isset($content['data']) || !is_array($content['data'])) { throw new \InvalidArgumentException("Malformed response data"); } return $content['data']; }
/** * @param ResponseInterface $response * @return array * @throws \InvalidArgumentException */ protected function assureResponseData(ResponseInterface $response) { $content = $response->getContent(); // First, check if the content contains data if (isset($content['data']) && is_array($content['data'])) { $data = $content['data']; // If data is an object wrap the object into an array if ($this->isJsonObject($data)) { $data = array($data); } return $data; } // Second, check if the content contains special entries if (isset($content['targetingsentencelines'])) { return $content['targetingsentencelines']; } if (isset($content['adaccounts'])) { return $content['adaccounts']; } if (isset($content['users'])) { return $content['users']; } // Third, check if the content is an array of objects indexed by id $is_id_indexed_array = true; $objects = array(); if (is_array($content) && count($content) >= 1) { foreach ($content as $key => $value) { if ($key === '__fb_trace_id__') { continue; } if ($value !== null && $this->isJsonObject($value) && isset($value['id']) && $value['id'] !== null && $value['id'] === $key) { $objects[] = $value; } else { $is_id_indexed_array = false; break; } } } else { $is_id_indexed_array = false; } if ($is_id_indexed_array) { return $objects; } throw new \InvalidArgumentException("Malformed response data"); }