File "Endpoint.php"

Full path: /home/kosmetik/public_html/api/fan_courier/sursa/Endpoint.php
File size: 1.96 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

/**
 * @file
 * Contains \FanCourier\Endpoint\Endpoint.
 */
 
namespace FanCourier\Endpoint;

require_once __DIR__ . '/endpointInterface.php';
require_once __DIR__ . '/../Plugin/Curl.php';
require_once __DIR__ . '/../Plugin/Exeption/fcApiExeption.php';
require_once __DIR__ . '/paramError.php';
use FanCourier\Endpoint\endpointInterface;
use FanCourier\Plugin\Curl;
use FanCourier\Plugin\Exeption\fcApiExeption;

/**
 * Endpoint controller class.
 *
 * @author [email protected]
 */
abstract class Endpoint implements endpointInterface {

  use paramError;

  /**
   * Result of CURL request.
   *
   * @var array
   */
  public $result = NULL;

  /**
   * If result is a csv file keep or not the header.
   *
   * @var bool
   */
  public $result_with_header = TRUE;

  /**
   * New controller class.
   *
   * @return \FanCourier\Endpoint\class
   */
  public static function newEndpoint() {
    $class = get_called_class();
    return new $class();
  }

  /**
   * Set params for CURL call.
   *
   * @param array $post_params
   */
  public function setParams(array $post_params) {
    $this->post_params = $post_params;
  }

  /**
   * Make CURL call.
   *
   * @throws fcApiExeption
   */
  public function curlCall() {

    $this->checkErrors();

    $curl = new Curl($this->url);
    $rp = $curl->curlRequest($this->post_params);

    if ($rp['info']['http_code'] == 200) {
      $this->result = $rp['response'];
    }
    else {
      throw new fcApiExeption($rp['response'], $rp['info']['http_code']);
    }
  }

  /**
   * Return curl call result.
   *
   * @return type
   */
  public function getResult() {
    $this->curlCall();
    return $this->result;
  }

  /**
   * Set no header value. 
   */
  public function noHeader() {
    $this->result_with_header = FALSE;
  }

  /**
   * Convert csv/text to array.
   */
  public function csvToArray() {
    $this->result = str_getcsv($this->result, "\n");
    if (!$this->result_with_header) {
      unset($this->result[0]);
    }
  }

}