freebox_api_php/freebox/utils/rest/Rest.php

122 lines
3.2 KiB
PHP
Raw Normal View History

2016-02-02 20:34:57 +01:00
<?php
namespace alphayax\freebox\utils\rest;
2016-02-03 20:08:23 +01:00
use alphayax;
2016-02-02 20:34:57 +01:00
/**
* Class Rest
* @package alphayax\utils
* @author <alphayax@gmail.com>
*/
class Rest extends alphayax\utils\Rest {
2016-02-02 20:34:57 +01:00
2016-06-24 16:34:06 +02:00
/** @var bool */
protected $isResponseToCheck = true;
2016-02-02 20:34:57 +01:00
/**
* @param null $curlPostData
2016-06-24 16:34:06 +02:00
* @throws \alphayax\freebox\Exception\FreeboxApiException
2016-02-02 20:34:57 +01:00
*/
2016-06-24 16:34:06 +02:00
public function GET( $curlPostData = null){
parent::GET( $curlPostData);
2016-06-24 16:34:06 +02:00
$this->checkResponse();
2016-02-02 20:34:57 +01:00
}
/**
* @param $curlPostData
2016-02-02 20:34:57 +01:00
*/
public function POST( $curlPostData = null){
parent::POST( $curlPostData);
2016-02-03 20:08:23 +01:00
$this->checkResponse();
2016-02-02 20:34:57 +01:00
}
/**
* @param $curlPostData
2016-02-02 20:34:57 +01:00
*/
public function PUT( $curlPostData = null){
parent::PUT( $curlPostData);
2016-02-03 20:08:23 +01:00
$this->checkResponse();
}
/**
* @param $curlPostData
2016-02-03 20:08:23 +01:00
*/
public function DELETE( $curlPostData = null){
parent::DELETE( $curlPostData);
2016-02-03 20:08:23 +01:00
$this->checkResponse();
2016-02-02 20:34:57 +01:00
}
2016-02-03 20:08:23 +01:00
/**
* @throws \Exception
*/
protected function checkResponse(){
$request = explode( "\r\n", $this->curlGetInfo['request_header'])[0];
$url = $this->curlGetInfo['url'];
// echo ">> $request ($url)" . PHP_EOL;
2016-06-24 16:34:06 +02:00
/// Don't need to go further if the response have not to be checked (binary content, non standard response, ...)
if( ! $this->isResponseToCheck){
return;
}
if( false === $this->getSuccess()){
$response = $this->getCurlResponse();
$Exception = new alphayax\freebox\Exception\FreeboxApiException( $response['msg'] . ' ('. $response['error_code'] . ')');
$Exception->setApiMessage( $response['msg']);
$Exception->setApiErrorCode( $response['error_code']);
$Exception->setApiRequest( $request);
$Exception->setApiHost( $url);
throw $Exception;
2016-02-03 20:08:23 +01:00
}
}
2016-05-31 22:09:45 +02:00
/**
* @param string $className
* @return array
*/
public function getResultAsArray( $className = ''){
$Model_xs = @$this->getCurlResponse()['result'] ?: [];
/// Cast elements
2016-06-24 21:41:17 +02:00
if( ! empty( $className) && ! empty( $Model_xs) && is_subclass_of( $className, alphayax\freebox\utils\Model::class)) {
2016-05-31 22:09:45 +02:00
array_walk( $Model_xs, function( &$item, $key, $className){
$item = new $className( $item);
}, $className);
}
return $Model_xs;
}
/**
* @param string $className
2016-06-24 21:41:17 +02:00
* @return array|\alphayax\freebox\utils\Model
2016-05-31 22:09:45 +02:00
*/
public function getResult( $className = ''){
$Model = @$this->getCurlResponse()['result'];
/// Cast element
2016-06-24 21:41:17 +02:00
if( ! empty( $className) && ! empty( $Model) && is_subclass_of( $className, alphayax\freebox\utils\Model::class) && is_array( $Model)){
2016-05-31 22:09:45 +02:00
return new $className( $Model);
}
return $Model;
}
/**
* @return bool
*/
public function getSuccess(){
return boolval( $this->getCurlResponse()['success']);
}
2016-06-24 16:34:06 +02:00
/**
* @param boolean $isResponseToCheck
*/
public function setIsResponseToCheck( $isResponseToCheck = true){
$this->isResponseToCheck = $isResponseToCheck;
}
2016-02-02 20:34:57 +01:00
}