You are here

class ContextApi in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 vendor/smartling/api-sdk-php/src/Context/ContextApi.php \Smartling\Context\ContextApi
  2. 8.2 api-sdk-php/src/Context/ContextApi.php \Smartling\Context\ContextApi
  3. 8.2 vendor/smartling/api-sdk-php/src/Context/ContextApi.php \Smartling\Context\ContextApi

Class ContextApi

@package Smartling\Project

Hierarchy

Expanded class hierarchy of ContextApi

3 files declare their use of ContextApi
ContextApiFunctionalTest.php in vendor/smartling/api-sdk-php/tests/functional/ContextApiFunctionalTest.php
ContextApiTest.php in vendor/smartling/api-sdk-php/tests/unit/ContextApiTest.php
SmartlingApiFactory.php in src/Smartling/SmartlingApiFactory.php
SmartlingApiFactory.php.

File

vendor/smartling/api-sdk-php/src/Context/ContextApi.php, line 21

Namespace

Smartling\Context
View source
class ContextApi extends BaseApiAbstract implements Waitable {
  use Wait;
  const ENDPOINT_URL = 'https://api.smartling.com/context-api/v2/projects';

  /**
   * Makes async operation sync.
   *
   * @param array $data
   * @throws SmartlingApiException
   */
  public function wait(array $data) {
    if (!empty($data['matchId'])) {
      $start_time = time();
      do {
        $delta = time() - $start_time;
        if ($delta > $this
          ->getTimeOut()) {
          throw new SmartlingApiException(vsprintf('Async operation is not completed after %s seconds.', [
            $delta,
          ]));
        }
        sleep(1);
        $result = $this
          ->getMatchStatus($data['matchId']);
      } while ($result['status'] != 'COMPLETED');
    }
  }

  /**
   * Instantiates Context API object.
   *
   * @param AuthApiInterface $authProvider
   * @param string $projectId
   * @param LoggerInterface $logger
   *
   * @return ContextApi
   */
  public static function create(AuthApiInterface $authProvider, $projectId, $logger = null) {
    $client = self::initializeHttpClient(self::ENDPOINT_URL);
    $instance = new self($projectId, $client, $logger, self::ENDPOINT_URL);
    $instance
      ->setAuth($authProvider);
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  protected function processBodyOptions($requestData = []) {
    $opts = parent::processBodyOptions($requestData);
    $keys = [
      'content',
      'resource',
    ];
    if (!empty($opts['multipart'])) {
      foreach ($opts['multipart'] as &$data) {
        if (in_array($data['name'], $keys)) {
          $data['contents'] = $this
            ->readFile($data['contents']);
        }
        if ($data['name'] == 'matchParams') {
          $data['headers'] = [
            "Content-Type" => "application/json",
          ];
        }
      }
    }
    return $opts;
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultRequestData($parametersType, $parameters, $auth = true, $httpErrors = false) {
    $requestData = parent::getDefaultRequestData($parametersType, $parameters, $auth = true, $httpErrors = false);
    $requestData['headers']['X-SL-Context-Source'] = $this
      ->getXSLContextSourceHeader();
    return $requestData;
  }

  /**
   * Returns X-SL-Context-Source header.
   *
   * @return string
   */
  private function getXSLContextSourceHeader() {
    return vsprintf('group=connectors;name=%s;version=%s', [
      static::getCurrentClientId(),
      static::getCurrentClientVersion(),
    ]);
  }

  /**
   * Upload a new context.
   *
   * @param \Smartling\Context\Params\UploadContextParameters $params
   * @return array
   * @throws \Smartling\Exceptions\SmartlingApiException
   */
  public function uploadContext(UploadContextParameters $params) {
    $requestData = $this
      ->getDefaultRequestData('multipart', $params
      ->exportToArray());
    return $this
      ->sendRequest('contexts', $requestData, self::HTTP_METHOD_POST);
  }

  /**
   * Match context async.
   *
   * @param $contextUid
   * @param \Smartling\Context\Params\MatchContextParameters $params
   *
   * @return array
   * @throws \Smartling\Exceptions\SmartlingApiException
   */
  public function matchContext($contextUid, MatchContextParameters $params = null) {
    $endpoint = vsprintf('contexts/%s/match/async', $contextUid);
    $requestData = $this
      ->getDefaultRequestData('json', is_null($params) ? [] : $params
      ->exportToArray());
    return $this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_POST);
  }

  /**
   * Match context sync.
   *
   * @param $contextUid
   * @param \Smartling\Context\Params\MatchContextParameters $params
   *
   * @throws \Smartling\Exceptions\SmartlingApiException
   */
  public function matchContextSync($contextUid, MatchContextParameters $params = null) {
    $this
      ->wait($this
      ->matchContext($contextUid, $params));
  }

  /**
   * Upload and match async.
   *
   * @param \Smartling\Context\Params\UploadContextParameters $params
   * @return array
   * @throws \Smartling\Exceptions\SmartlingApiException
   */
  public function uploadAndMatchContext(UploadContextParameters $params) {
    $requestData = $this
      ->getDefaultRequestData('multipart', $params
      ->exportToArray());
    return $this
      ->sendRequest('contexts/upload-and-match-async', $requestData, self::HTTP_METHOD_POST);
  }

  /**
   * Upload and match sync.
   *
   * @param \Smartling\Context\Params\UploadContextParameters $params
   * @throws \Smartling\Exceptions\SmartlingApiException
   */
  public function uploadAndMatchContextSync(UploadContextParameters $params) {
    $this
      ->wait($this
      ->uploadAndMatchContext($params));
  }

  /**
   * Get context match status.
   *
   * @param $matchId
   * @return array
   * @throws SmartlingApiException
   */
  public function getMatchStatus($matchId) {
    $endpoint = vsprintf('/match/%s', $matchId);
    $requestData = $this
      ->getDefaultRequestData('query', []);
    return $this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_GET);
  }

  /**
   * Get missing resources.
   *
   * @param MissingResourcesParameters|null $params
   * @return array
   * @throws \Smartling\Exceptions\SmartlingApiException
   */
  public function getMissingResources(MissingResourcesParameters $params = null) {
    $requestData = $this
      ->getDefaultRequestData('query', is_null($params) ? [] : $params
      ->exportToArray());
    return $this
      ->sendRequest('missing-resources', $requestData, self::HTTP_METHOD_GET);
  }

  /**
   * Get all missing resources.
   *
   * @return array
   * Contains next keys:
   *  - "items": array of missing resources
   *  - "all": boolean which indicates whether function has read all the
   *           available items or not.
   *
   * @throws SmartlingApiException
   */
  public function getAllMissingResources() {
    $missingResources = [];
    $offset = FALSE;
    $all = TRUE;
    $start_time = time();
    while (!is_null($offset)) {
      $delta = time() - $start_time;
      if ($delta > $this
        ->getTimeOut()) {
        $all = FALSE;
        break;
      }
      if (!$offset) {
        $params = NULL;
      }
      else {
        $params = new MissingResourcesParameters();
        $params
          ->setOffset($offset);
      }
      $response = $this
        ->getMissingResources($params);
      $offset = !empty($response['offset']) ? $response['offset'] : NULL;
      $missingResources = array_merge($missingResources, $response['items']);
    }
    return [
      'items' => $missingResources,
      'all' => $all,
    ];
  }

  /**
   * Upload resource.
   *
   * @param $resourceId
   * @param \Smartling\Context\Params\UploadResourceParameters $params
   * @return bool
   * @throws SmartlingApiException
   */
  public function uploadResource($resourceId, UploadResourceParameters $params) {
    $endpoint = vsprintf('resources/%s', $resourceId);
    $requestData = $this
      ->getDefaultRequestData('multipart', $params
      ->exportToArray());
    return $this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_PUT);
  }

  /**
   * Render context.
   *
   * @param $contextUid
   * @return array
   * @throws \Smartling\Exceptions\SmartlingApiException
   */
  public function renderContext($contextUid) {
    $endpoint = vsprintf('contexts/%s/render', $contextUid);
    $requestData = $this
      ->getDefaultRequestData('form_params', []);
    return $this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_POST);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseApiAbstract::$auth private property
BaseApiAbstract::$baseUrl private property Smartling API base url.
BaseApiAbstract::$currentClientId private static property
BaseApiAbstract::$currentClientUserAgentExtension private static property
BaseApiAbstract::$currentClientVersion private static property
BaseApiAbstract::$httpClient private property Http Client abstraction.
BaseApiAbstract::$logger private property Logger.
BaseApiAbstract::$projectId private property Project Id in Smartling dashboard
BaseApiAbstract::checkAuthenticationError private function
BaseApiAbstract::CLIENT_LIB_ID_SDK constant
BaseApiAbstract::CLIENT_LIB_ID_VERSION constant
BaseApiAbstract::CLIENT_USER_AGENT_EXTENSION constant
BaseApiAbstract::getAuth protected function
BaseApiAbstract::getBaseUrl protected function
BaseApiAbstract::getCurrentClientId public static function
BaseApiAbstract::getCurrentClientUserAgentExtension public static function
BaseApiAbstract::getCurrentClientVersion public static function
BaseApiAbstract::getHttpClient protected function
BaseApiAbstract::getLogger protected function
BaseApiAbstract::getProjectId protected function
BaseApiAbstract::HTTP_METHOD_DELETE constant
BaseApiAbstract::HTTP_METHOD_GET constant
BaseApiAbstract::HTTP_METHOD_POST constant
BaseApiAbstract::HTTP_METHOD_PUT constant
BaseApiAbstract::initializeHttpClient protected static function
BaseApiAbstract::normalizeUri private function
BaseApiAbstract::PATTERN_DATE_TIME_ISO_8601 constant PHP equivalent to 'YYYY-MM-DDThh:mm:ssZ'
BaseApiAbstract::processError private function
BaseApiAbstract::processErrors private function
BaseApiAbstract::readFile protected function OOP wrapper for fopen() function.
BaseApiAbstract::sendRequest protected function
BaseApiAbstract::setAuth protected function
BaseApiAbstract::setBaseUrl protected function
BaseApiAbstract::setCurrentClientId public static function
BaseApiAbstract::setCurrentClientUserAgentExtension public static function
BaseApiAbstract::setCurrentClientVersion public static function
BaseApiAbstract::setHttpClient protected function
BaseApiAbstract::setLogger protected function
BaseApiAbstract::setProjectId protected function
BaseApiAbstract::__construct public function BaseApiAbstract constructor. 3
ContextApi::create public static function Instantiates Context API object.
ContextApi::ENDPOINT_URL constant
ContextApi::getAllMissingResources public function Get all missing resources.
ContextApi::getDefaultRequestData protected function Overrides BaseApiAbstract::getDefaultRequestData
ContextApi::getMatchStatus public function Get context match status.
ContextApi::getMissingResources public function Get missing resources.
ContextApi::getXSLContextSourceHeader private function Returns X-SL-Context-Source header.
ContextApi::matchContext public function Match context async.
ContextApi::matchContextSync public function Match context sync.
ContextApi::processBodyOptions protected function Overrides BaseApiAbstract::processBodyOptions
ContextApi::renderContext public function Render context.
ContextApi::uploadAndMatchContext public function Upload and match async.
ContextApi::uploadAndMatchContextSync public function Upload and match sync.
ContextApi::uploadContext public function Upload a new context.
ContextApi::uploadResource public function Upload resource.
ContextApi::wait public function Makes async operation sync. Overrides Waitable::wait
Wait::$timeOut private property Timeout for sync requests in seconds.
Wait::getTimeOut public function
Wait::setTimeOut public function