auditseo
Новичок
Добрый день! Прошу посмотреть мой незатейлевый код и сказать нормально ли так писать в ООП стиле или это не в какие рамки не лезет? Интересна конструктивная критика.
PHP:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// var_dump($_POST);
// echo "\n";
$objProcessingRequest = new ProcessingRequest();
//$objProcessingRequest->makeRequest();
$textJson = $objProcessingRequest->getTextJson();
// echo "JSON\n\n";
echo $textJson;
}
class ProcessingRequest
{
private $requestClient = '';
private $requestClientEncode = '';
private $finalRequest = '';
private $appId = 'Q2XYU3-EUU22U5UVX';
private $responseXml = '';
private $img3D;
private $imgContour;
private $imgExpression;
private $responseJson = '';
//формируем запрос на вольфрам
public function __construct()
{
// сначала получим запрашиваемую ф-цию от клиента
$this->acceptClientRequest();
//сформируем GET запрос для вольфрама
$this->createGetRequest();
//посылаем запрос в вольфрам (CURL)
$this->sendRequest();
//распарсим XML
$this->parseXml();
//сформируем JSON для отправки клиенту
$this->createJson();
}
// получить у объекта текст XML
public function getTextXml()
{
return $this->responseXml;
}
// получить JSON
public function getTextJson(){
return $this->responseJson;
}
// принимает от клиента запрос - которые переправим на вольфрам
private function acceptClientRequest()
{
// взяли данные из запроса
$strFunc = $_POST['strFunc'];
$betweenX_1 = $_POST['betweenX_1'];
$betweenX_2 = $_POST['betweenX_2'];
$betweenY_1 = $_POST['betweenY_1'];
$betweenY_2 = $_POST['betweenY_2'];
// формируем строку запроса на вольфрам
//'plot x^2+(y-3)^2-2 over x=-10..10, y=-10..10';
$this->requestClient = 'plot ' . $strFunc . ' over' . ' x=' . $betweenX_1 . '..' . $betweenX_2 .
', ' . 'y=' . $betweenY_1 . '..' . $betweenY_2;
//debug
// echo 'request: ' . $this->requestClient . "\n";
}
// склеим запрос, который пошлем вольфраму
private function createGetRequest()
{
$this->requestClientEncode = rawurlencode($this->requestClient);
$this->finalRequest = 'http://api.wolframalpha.com/v2/query?appid=' . $this->appId . '&input=' . $this->requestClientEncode . '&format=image';
//debug
// echo $this->finalRequest . "\n";
}
//отправляет запрос и получает XML
private function sendRequest()
{
$curl = curl_init();
if ($curl === false) {
// echo "Curl error: " . curl_error($curl);
} else {
curl_setopt($curl, CURLOPT_URL, $this->finalRequest);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
$this->responseXml = curl_exec($curl);
curl_close($curl);
}
// проверяем была ли ошибка
if (curl_errno($curl) != 0){
exit;
}
//debug
// echo '$resposeXml = ' . $this->responseXml . "\n";
}
// распарсим XML на ассоативный массив
private function parseXml()
{
$objXml = new SimpleXMLElement($this->responseXml);
// картинка 3D
foreach ($objXml->pod as $tagPod) {
if($tagPod['title'] == 'Input interpretation'){
$tagImgExpression = $tagPod->subpod->img;
$this->imgExpression['src'] = (string) $tagImgExpression['src'];
$this->imgExpression['width'] = (string) $tagImgExpression['width'];
$this->imgExpression['height'] = (string) $tagImgExpression['height'];
} elseif ($tagPod['title'] == '3D plot') {
$tagImg3D = $tagPod->subpod->img;
$this->img3D['src'] = (string) $tagImg3D['src'];
//// echo "CHECK!!!\n\n";
//// var_dump(( (string) $tagImg3D['src']));
$this->img3D['width'] = (string) $tagImg3D['width'];
$this->img3D['height'] = (string) $tagImg3D['height'];
} elseif ($tagPod['title'] == 'Contour plot') {
$tagImgContour = $tagPod->subpod->img;
$this->imgContour['src'] = (string) $tagImgContour['src'];
$this->imgContour['width'] = (string) $tagImgContour['width'];
$this->imgContour['height'] = (string) $tagImgContour['height'];
}
}
}
// формируем JSON, который отправим клиенту
private function createJson(){
$jsonImg3D = json_encode($this->img3D);
$jsonImgContour = json_encode($this->imgContour);
$jsonImgExpression = json_encode($this->imgExpression);
// var_dump($jsonImg3D);
// var_dump($jsonImgContour);
$this->responseJson = "{\"imgExpression\": $jsonImgExpression,\"img3D\": $jsonImg3D,\"imgContour\": $jsonImgContour}" ;
// var_dump($this->responseJson);
}
}