2019-06-21 23:20:55 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A very compact and simple Memsource API library
|
2019-06-25 13:47:05 +00:00
|
|
|
*
|
2019-06-21 23:20:55 +00:00
|
|
|
* @author Bruno Fontes <developer@brunofontes.net>
|
|
|
|
* @link https://github.com/brunofontes
|
|
|
|
*/
|
2019-06-25 13:47:05 +00:00
|
|
|
|
2019-06-21 23:20:55 +00:00
|
|
|
namespace BrunoFontes\Memsource;
|
|
|
|
|
2019-06-22 00:12:00 +00:00
|
|
|
class BilingualFile extends \BrunoFontes\Memsource\BaseApi
|
2019-06-21 23:20:55 +00:00
|
|
|
{
|
2019-06-24 22:47:45 +00:00
|
|
|
private $_url = '/api2/v1/bilingualFiles';
|
2019-06-25 13:47:05 +00:00
|
|
|
|
2019-06-21 23:20:55 +00:00
|
|
|
/**
|
|
|
|
* Download one or more bilingual files
|
2019-06-25 13:47:05 +00:00
|
|
|
*
|
|
|
|
* As Memsource limits downloading files into 100 jobs per time, this script
|
|
|
|
* will prevent that by making all the necessary fetchs and saving each on
|
2019-06-21 23:20:55 +00:00
|
|
|
* a different file.
|
2019-06-25 13:47:05 +00:00
|
|
|
*
|
2019-06-21 23:20:55 +00:00
|
|
|
* An array with all the files used will be returned.
|
|
|
|
*
|
|
|
|
* @param string $projectUid The project uid which contain the jobs
|
|
|
|
* @param array $jobUids A simple array of Job uids: ['job_uid1', 'job_uid2']
|
2019-06-25 13:47:05 +00:00
|
|
|
* @param string $filename File that will be created on server to store the
|
2019-06-21 23:20:55 +00:00
|
|
|
* downloaded jobs
|
2019-06-25 13:47:05 +00:00
|
|
|
*
|
2019-06-21 23:20:55 +00:00
|
|
|
* @return array A list of the downloaded files
|
|
|
|
*/
|
2019-06-25 13:47:05 +00:00
|
|
|
public function download(string $projectUid, array $jobUids, string $filename): array
|
2019-06-21 23:20:55 +00:00
|
|
|
{
|
|
|
|
$url = "/api2/v1/projects/{$projectUid}/jobs/bilingualFile";
|
|
|
|
|
|
|
|
$groupedJobUids = array_chunk($jobUids, 100);
|
|
|
|
for ($i = 0; $i < count($groupedJobUids); $i++) {
|
|
|
|
$apiReadyArray = $this->_convertUidArrayToApiRequest($groupedJobUids[$i]);
|
2019-06-25 13:47:05 +00:00
|
|
|
$filenames[$i] = count($groupedJobUids) > 1 ? "{$i}_{$filename}" : $filename;
|
2019-06-22 00:12:00 +00:00
|
|
|
$filecontent = $this->fetchApi->fetch('jsonPost', $url, $apiReadyArray);
|
2019-06-21 23:20:55 +00:00
|
|
|
$this->_saveIntoFile($filenames[$i], $filecontent);
|
|
|
|
}
|
|
|
|
return $filenames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-25 13:47:05 +00:00
|
|
|
* Convert a simple Array of uids provided by the user into the array
|
2019-06-21 23:20:55 +00:00
|
|
|
* format required by Memsource API
|
|
|
|
*
|
|
|
|
* @param array $uids A simple array of UIDs
|
2019-06-25 13:47:05 +00:00
|
|
|
*
|
2019-06-21 23:20:55 +00:00
|
|
|
* @return array The API ready array
|
|
|
|
*/
|
|
|
|
private function _convertUidArrayToApiRequest(array $uids): array
|
|
|
|
{
|
|
|
|
foreach ($uids as $jobUid) {
|
|
|
|
$convertedArray[] = ['uid' => $jobUid];
|
|
|
|
}
|
|
|
|
return ['jobs' => $convertedArray];
|
|
|
|
}
|
|
|
|
|
2019-06-24 22:47:45 +00:00
|
|
|
private function _saveIntoFile(string $filename, string $filecontent): void
|
2019-06-21 23:20:55 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$f = fopen($filename, 'w+');
|
|
|
|
fwrite($f, $filecontent);
|
|
|
|
fclose($f);
|
|
|
|
} catch (\Exception $e) {
|
2019-06-24 22:47:45 +00:00
|
|
|
throw new \Exception("File could not be saved: {$e->error}", 1);
|
2019-06-21 23:20:55 +00:00
|
|
|
}
|
2019-06-24 22:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload a bilingual file to Memsource
|
|
|
|
*
|
|
|
|
* @param string $filename The filename to be uploaded
|
|
|
|
* @param array $params Any API (HTTP GET) parameters as ['key' => 'value'] format
|
2019-06-25 13:47:05 +00:00
|
|
|
*
|
2019-06-24 22:47:45 +00:00
|
|
|
* @return string The http request responde from API in json format
|
|
|
|
*/
|
2019-06-25 13:47:05 +00:00
|
|
|
public function upload(string $filename, array $params): string
|
2019-06-24 22:47:45 +00:00
|
|
|
{
|
|
|
|
$urlParams = http_build_query($params);
|
|
|
|
$fileContent = file_get_contents($filename);
|
|
|
|
if ($fileContent === false) {
|
|
|
|
throw new \Exception('File for upload inexistent or invalid', 1);
|
|
|
|
}
|
|
|
|
return $this->fetchApi->fetch('put', $this->_url . "?{$urlParams}", [$fileContent]);
|
|
|
|
}
|
2019-06-21 23:20:55 +00:00
|
|
|
}
|