From 7664bf67a432f6e6b8feed00889d16b0ad6622fb Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Fri, 21 Jun 2019 20:20:55 -0300 Subject: [PATCH] Including the BilingualFile class This class is responsible for downloading bilingual files --- src/BilingualFile.php | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/BilingualFile.php diff --git a/src/BilingualFile.php b/src/BilingualFile.php new file mode 100644 index 0000000..d5e5f8f --- /dev/null +++ b/src/BilingualFile.php @@ -0,0 +1,70 @@ + + * @link https://github.com/brunofontes + */ +namespace BrunoFontes\Memsource; + +class BilingualFile extends \BrunoFontes\Memsource +{ + private $_url = '/bilingualFiles'; + + /** + * Download one or more bilingual files + * + * 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 + * a different file. + * + * 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'] + * @param string $filename File that will be created on server to store the + * downloaded jobs + * + * @return array A list of the downloaded files + */ + public function downloadBilingualFile(string $projectUid, array $jobUids, string $filename): array + { + $url = "/api2/v1/projects/{$projectUid}/jobs/bilingualFile"; + + $groupedJobUids = array_chunk($jobUids, 100); + for ($i = 0; $i < count($groupedJobUids); $i++) { + $apiReadyArray = $this->_convertUidArrayToApiRequest($groupedJobUids[$i]); + $filenames[$i] = count($groupedJobUids) > 1?"{$i}_{$filename}":$filename; + $filecontent = $this->fetch('jsonPost', $url, $apiReadyArray); + $this->_saveIntoFile($filenames[$i], $filecontent); + } + return $filenames; + } + + /** + * Convert a simple Array of uids provided by the user into the array + * format required by Memsource API + * + * @param array $uids A simple array of UIDs + * + * @return array The API ready array + */ + private function _convertUidArrayToApiRequest(array $uids): array + { + foreach ($uids as $jobUid) { + $convertedArray[] = ['uid' => $jobUid]; + } + return ['jobs' => $convertedArray]; + } + + private function _saveIntoFile(string $filename, string $filecontent) + { + try { + $f = fopen($filename, 'w+'); + fwrite($f, $filecontent); + fclose($f); + } catch (\Exception $e) { + throw new Exception("File could not be saved: " . $e->error, 1); + } + } +}