Skip to content

Commit

Permalink
- Glossary API update [issue #97]
Browse files Browse the repository at this point in the history
  • Loading branch information
bhdnb committed Jan 13, 2025
1 parent d2e4f80 commit 329b349
Show file tree
Hide file tree
Showing 39 changed files with 1,422 additions and 331 deletions.
7 changes: 3 additions & 4 deletions src/CrowdinApiClient/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getHeader($header)
return $this->headers[strtolower($header)];
}

public function getHeaders()
public function getHeaders(): array
{
return $this->headers;
}
Expand All @@ -77,7 +77,7 @@ public function setHeaders(array $headers)
return $this;
}

public function hasHeader($header)
public function hasHeader($header): bool
{
return isset($this->headers[strtolower($header)]);
}
Expand All @@ -102,7 +102,6 @@ public function removeHeader($header)
protected function _update(string $path, ModelInterface $model)
{
$dataModel = $model->getProperties();

$_data = [];

foreach ($model->getData() as $key => $val) {
Expand Down Expand Up @@ -182,7 +181,7 @@ protected function _delete(string $path, array $params = [])
* @param array $params
* @return mixed
*/
protected function _get(string $path, string $modelName, $params = [])
protected function _get(string $path, string $modelName, array $params = [])
{
$options = [
'params' => $params,
Expand Down
93 changes: 91 additions & 2 deletions src/CrowdinApiClient/Api/GlossaryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace CrowdinApiClient\Api;

use CrowdinApiClient\Http\ResponseDecorator\ResponseModelListDecorator;
use CrowdinApiClient\Model\DownloadFile;
use CrowdinApiClient\Model\Glossary;
use CrowdinApiClient\Model\GlossaryConcept;
use CrowdinApiClient\Model\GlossaryConcordance;
use CrowdinApiClient\Model\GlossaryExport;
use CrowdinApiClient\Model\GlossaryImport;
use CrowdinApiClient\Model\Term;
Expand Down Expand Up @@ -99,8 +102,11 @@ public function update(Glossary $glossary): ?Glossary
* @param array $exportFields
* @return GlossaryExport|null
*/
public function export(int $glossaryId, $format = 'tbx', $exportFields = ['term', 'description', 'partOfSpeech']): ?GlossaryExport
{
public function export(
int $glossaryId,
string $format = 'tbx',
array $exportFields = ['term', 'description', 'partOfSpeech']
): ?GlossaryExport {
$path = sprintf('glossaries/%d/exports', $glossaryId);
$params = ['format' => $format, 'exportFields' => $exportFields];

Expand Down Expand Up @@ -141,6 +147,7 @@ public function download(int $glossaryId, string $exportId): ?DownloadFile
* Import Glossary
* @link https://developer.crowdin.com/api/v2/#operation/api.glossaries.imports.post API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.glossaries.imports.post API Documentation
*
* @param int $glossaryId
* @param array $data
* integer $data[storageId] required<br>
Expand Down Expand Up @@ -169,6 +176,32 @@ public function getImport(int $glossaryId, string $importId): ?GlossaryImport
return $this->_get($path, GlossaryImport::class);
}

/**
* Concordance search in Glossaries
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.glossaries.concordance.post API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.glossaries.concordance.post API Documentation Enterprise
*
* @param int $projectId
* @param array $data
* string $data[sourceLanguageId]<br>
* string $data[targetLanguageId]<br>
* string[] $data[expressions]<br>
* string $data[expression]<br>
* @return ModelCollection|null
*/
public function concordance(int $projectId, array $data): ?ModelCollection
{
return $this->client->apiRequest(
'post',
sprintf('projects/%d/glossaries/concordance', $projectId),
new ResponseModelListDecorator(GlossaryConcordance::class),
[
'body' => json_encode($data),
'headers' => $this->getHeaders(),
]
);
}

/**
* List Terms
* @link https://developer.crowdin.com/api/v2/#operation/api.glossaries.terms.getMany API Documentation
Expand Down Expand Up @@ -257,6 +290,7 @@ public function deleteTerm(int $glossaryId, int $termId)
* Edit Term
* @link https://developer.crowdin.com/api/v2/#operation/api.glossaries.terms.patch API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.glossaries.terms.patch API Documentation Enterprise
*
* @param Term $term
* @return Term|null
*/
Expand All @@ -265,4 +299,59 @@ public function updateTerm(Term $term): ?Term
$path = sprintf('glossaries/%d/terms/%d', $term->getGlossaryId(), $term->getId());
return $this->_update($path, $term);
}

/**
* List Concepts
* @link https://developer.crowdin.com/api/v2/#operation/api.glossaries.terms.getMany API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.glossaries.terms.getMany API Documentation Enterprise
*/
public function listConcepts(int $glossaryId, array $params = []): ?ModelCollection
{
$path = sprintf('glossaries/%d/concepts', $glossaryId);
return $this->_list($path, GlossaryConcept::class, $params);
}

/**
* Get Concept
* @link https://developer.crowdin.com/api/v2/#operation/api.glossaries.concepts.get API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.glossaries.concepts.get API Documentation Enterprise
*/
public function getConcept(int $glossaryId, int $conceptId): ?GlossaryConcept
{
$path = sprintf('glossaries/%d/concepts/%d', $glossaryId, $conceptId);
return $this->_get($path, GlossaryConcept::class);
}

/**
* Update Concept
* @link https://developer.crowdin.com/api/v2/#operation/api.glossaries.concepts.put API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.glossaries.concepts.put API Documentation Enterprise
*
* @param array $data
* string $data[subject]<br>
* string $data[definition]<br>
* bool $data[translatable]<br>
* string $data[note]<br>
* string $data[url]<br>
* string $data[figure]<br>
* array[] $data[languageDetails]<br>
*/
public function updateConcept(int $glossaryId, int $conceptId, array $data): GlossaryConcept
{
$path = sprintf('glossaries/%d/concepts/%d', $glossaryId, $conceptId);
return $this->_put($path, GlossaryConcept::class, $data);
}

/**
* Delete Concept
* @link https://developer.crowdin.com/api/v2/#operation/api.glossaries.concepts.delete API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.glossaries.concepts.delete API Documentation Enterprise
*
* @return mixed
*/
public function deleteConcept(int $glossaryId, int $conceptId)
{
$path = sprintf('glossaries/%d/concepts/%d', $glossaryId, $conceptId);
return $this->_delete($path);
}
}
29 changes: 5 additions & 24 deletions src/CrowdinApiClient/Model/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,29 @@
class BaseModel implements ModelInterface
{
/**
* Model Data
*
* @var array
*/
protected $data;

/**
* Create a new Model instance
*
* @param array $data
*/
public function __construct(array $data = [])
{
$this->data = $data;
}

/**
* Get the Model data
*
* @return array
*/
public function getData()
public function getData(): array
{
return $this->data;
}

/**
* Get Data Property
*
* @param string $property
*
* @return mixed
* @return null|mixed
*/
public function getDataProperty($property)
public function getDataProperty(string $property)
{
return isset($this->data[$property]) ? $this->data[$property] : null;
return $this->data[$property] ?? null;
}

/**
* @return array
*/
public function getProperties()
public function getProperties(): array
{
return get_object_vars($this);
}
Expand Down
66 changes: 39 additions & 27 deletions src/CrowdinApiClient/Model/Glossary.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,35 @@ class Glossary extends BaseModel
protected $terms;

/**
* @var array
* @var string
*/
protected $languageId;

/**
* @var string[]
*/
protected $languageIds;

/**
* @var array
* @var int[]
*/
protected $defaultProjectIds;

/**
* @var int[]
*/
protected $projectIds;

/**
* @var string
*/
protected $createdAt;
protected $webUrl;

/**
* @param array $data
* @var string
*/
protected $createdAt;

public function __construct(array $data = [])
{
parent::__construct($data);
Expand All @@ -59,78 +71,78 @@ public function __construct(array $data = [])
$this->groupId = (integer)$this->getDataProperty('groupId');
$this->userId = (integer)$this->getDataProperty('userId');
$this->terms = (integer)$this->getDataProperty('terms');
$this->languageId = (string)$this->getDataProperty('languageId');
$this->languageIds = (array)$this->getDataProperty('languageIds');
$this->defaultProjectIds = (array)$this->getDataProperty('defaultProjectIds');
$this->projectIds = (array)$this->getDataProperty('projectIds');
$this->webUrl = (string)$this->getDataProperty('webUrl');
$this->createdAt = (string)$this->getDataProperty('createdAt');
}

/**
* @return int
*/
public function getId(): int
{
return $this->id;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}

/**
* @return int
*/
public function getGroupId(): int
{
return $this->groupId;
}

/**
* @return int
*/
public function getUserId(): int
{
return $this->userId;
}

/**
* @return int
*/
public function getTerms(): int
{
return $this->terms;
}

public function getLanguageId(): string
{
return $this->languageId;
}

/**
* @return array
* @return string[]
*/
public function getLanguageIds(): array
{
return $this->languageIds;
}

/**
* @return array
* @return int[]
*/
public function getProjectIds(): array
public function getDefaultProjectIds(): array
{
return $this->projectIds;
return $this->defaultProjectIds;
}

/**
* @return string
* @return int[]
*/
public function getProjectIds(): array
{
return $this->projectIds;
}

public function getWebUrl(): string
{
return $this->webUrl;
}

public function getCreatedAt(): string
{
return $this->createdAt;
Expand Down
Loading

0 comments on commit 329b349

Please sign in to comment.