Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from TextControl/proofing
Browse files Browse the repository at this point in the history
Proofing
  • Loading branch information
jonathanmaron authored Oct 2, 2017
2 parents 65e61bd + d6951cc commit 1a3693f
Show file tree
Hide file tree
Showing 30 changed files with 610 additions and 142 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

## dev-master

## 1.4.0 - 2017-10-02

* Implemented [end-points](https://www.textcontrol.com/blog/2017/08/23/):
* `/v1/proofing/check`
* `/v1/proofing/availabledictionaries`
* `/v1/proofing/suggestions`
* Updated `AccountSettings` property map with keys:
* `proofing_transactions`
* `max_proofing_transactions`
* Refactored `Validator` unit tests.
* Added script to download and build _available dictionaries_ resource file.

## 1.3.4 - 2017-08-22

* Implemented the `mergeSettings` option `merge_html`.
Expand Down
59 changes: 59 additions & 0 deletions bin/build-available-dictionaries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* Available Dictionaries Resource File
*
* This script downloads all the available dictionaries from the Reporting Cloud Web API and writes them to the file:
*
* resource/available-dictionaries.php
*
* The package maintainer should execute this script, whenever new dictionaries are added to the backend.
*
*/

include_once 'bootstrap.php';

use TxTextControl\ReportingCloud\Console\Helper;
use TxTextControl\ReportingCloud\Exception\RuntimeException;
use TxTextControl\ReportingCloud\ReportingCloud;

$outputFilename = realpath(__DIR__ . '/../resource/available-dictionaries.php');

// ---------------------------------------------------------------------------------------------------------------------

$reportingCloud = new ReportingCloud([
'username' => Helper::username(),
'password' => Helper::password(),
]);

$availableDictionaries = $reportingCloud->getAvailableDictionaries();

sort($availableDictionaries);

if (0 === count($availableDictionaries)) {
throw new RuntimeException('Cannot download the available dictionaries from the Reporting Cloud Web API.');
}

// ---------------------------------------------------------------------------------------------------------------------

$buffer = '<?php';
$buffer .= PHP_EOL;
$buffer .= PHP_EOL;
$buffer .= 'return ';
$buffer .= var_export($availableDictionaries, true);
$buffer .= ';';
$buffer .= PHP_EOL;

file_put_contents($outputFilename, $buffer);

// ---------------------------------------------------------------------------------------------------------------------

echo PHP_EOL;
echo sprintf('The available dictionaries are %s.', implode(', ', $availableDictionaries));
echo PHP_EOL;
echo PHP_EOL;
echo sprintf('Written resource file to %s', $outputFilename);
echo PHP_EOL;
echo PHP_EOL;

// ---------------------------------------------------------------------------------------------------------------------
25 changes: 25 additions & 0 deletions demo/proofing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

include_once 'bootstrap.php';

use TxTextControl\ReportingCloud\Console\Helper;
use TxTextControl\ReportingCloud\ReportingCloud;

$reportingCloud = new ReportingCloud([
'username' => Helper::username(),
'password' => Helper::password(),
]);

// ---------------------------------------------------------------------------------------------------------------------

var_dump($reportingCloud->getAvailableDictionaries());

// ---------------------------------------------------------------------------------------------------------------------

var_dump($reportingCloud->getProofingSuggestions('ssky', 'en_US.dic', 10));

// ---------------------------------------------------------------------------------------------------------------------

var_dump($reportingCloud->proofingCheck('Thiss is a testt about rockkets in the ssky', 'en_US.dic'));

// ---------------------------------------------------------------------------------------------------------------------
53 changes: 53 additions & 0 deletions resource/available-dictionaries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

return array (
0 => 'af_ZA.dic',
1 => 'an_ES.dic',
2 => 'ar.dic',
3 => 'be_BY.dic',
4 => 'bg_BG.dic',
5 => 'br_FR.dic',
6 => 'ca.dic',
7 => 'cs_CZ.dic',
8 => 'da_DK.dic',
9 => 'de_AT_frami.dic',
10 => 'de_CH_frami.dic',
11 => 'de_DE_frami.dic',
12 => 'el_GR.dic',
13 => 'en_AU.dic',
14 => 'en_CA.dic',
15 => 'en_GB.dic',
16 => 'en_US.dic',
17 => 'en_US_Legal.txd',
18 => 'en_US_OpenMedSpel.dic',
19 => 'en_ZA.dic',
20 => 'es_ES.dic',
21 => 'et_EE.dic',
22 => 'fa.dic',
23 => 'fr.dic',
24 => 'gd_GB.dic',
25 => 'gl_ES.dic',
26 => 'he_IL.dic',
27 => 'hr_HR.dic',
28 => 'hy_AM.dic',
29 => 'it_IT.dic',
30 => 'ku_TR.dic',
31 => 'lt_LT.dic',
32 => 'nb_NO.dic',
33 => 'nl_NL.dic',
34 => 'nn_NO.dic',
35 => 'oc_FR.dic',
36 => 'pl_PL.dic',
37 => 'pt_BR.dic',
38 => 'pt_PT.dic',
39 => 'ro_RO.dic',
40 => 'ru_RU.dic',
41 => 'sh.dic',
42 => 'sk_SK.dic',
43 => 'sl_SI.dic',
44 => 'sr.dic',
45 => 'sv_SE.dic',
46 => 'uk_UA.dic',
47 => 'ur.dic',
48 => 'vi_VN.dic',
);
14 changes: 8 additions & 6 deletions src/PropertyMap/AccountSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ class AccountSettings extends AbstractPropertyMap
public function __construct()
{
$this->setMap([
'serialNumber' => 'serial_number',
'createdDocuments' => 'created_documents',
'uploadedTemplates' => 'uploaded_templates',
'maxDocuments' => 'max_documents',
'maxTemplates' => 'max_templates',
'validUntil' => 'valid_until',
'serialNumber' => 'serial_number',
'createdDocuments' => 'created_documents',
'uploadedTemplates' => 'uploaded_templates',
'maxDocuments' => 'max_documents',
'maxTemplates' => 'max_templates',
'validUntil' => 'valid_until',
'proofingTransactions' => 'proofing_transactions', // added in 1.4.0
'maxProofingTransactions' => 'max_proofing_transactions', // added in 1.4.0
]);
}
}
37 changes: 37 additions & 0 deletions src/PropertyMap/IncorrectWord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* ReportingCloud PHP Wrapper
*
* PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
*
* @link http://www.reporting.cloud to learn more about ReportingCloud
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
* @copyright © 2017 Text Control GmbH
*/

namespace TxTextControl\ReportingCloud\PropertyMap;

/**
* IncorrectWord property map
*
* @package TxTextControl\ReportingCloud
* @author Jonathan Maron (@JonathanMaron)
*/
class IncorrectWord extends AbstractPropertyMap
{
/**
* Set the property map of IncorrectWord
*/
public function __construct()
{
$this->setMap([
'length' => 'length',
'start' => 'start',
'text' => 'text',
'isDuplicate' => 'is_duplicate',
'language' => 'language',
]);
}
}
122 changes: 104 additions & 18 deletions src/ReportingCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use TxTextControl\ReportingCloud\PropertyMap\AccountSettings as AccountSettingsPropertyMap;
use TxTextControl\ReportingCloud\PropertyMap\TemplateInfo as TemplateInfoPropertyMap;
use TxTextControl\ReportingCloud\PropertyMap\TemplateList as TemplateListPropertyMap;
use TxTextControl\ReportingCloud\PropertyMap\IncorrectWord as IncorrectWordMap;
use TxTextControl\ReportingCloud\Validator\StaticValidator;

/**
Expand All @@ -36,27 +37,32 @@ class ReportingCloud extends AbstractReportingCloud
*/

/**
* Return an array of merge blocks and merge fields in a template file in template storage.
* Check a corpus of text for spelling errors.
*
* @param string $templateName Template name
* Return an array of misspelled words, if spelling errors are found in the corpus of text.
*
* @throws InvalidArgumentException
* Return null, if no misspelled words are found in the corpus of text.
*
* @param string $text Corpus of text that should be spell checked
* @param string $language Language of specified text
*
* @return array|null
*/
public function getTemplateInfo($templateName)
public function proofingCheck($text, $language)
{
$ret = null;

$propertyMap = new TemplateInfoPropertyMap();
StaticValidator::execute($text, 'TypeString');
StaticValidator::execute($language, 'Language');

StaticValidator::execute($templateName, 'TemplateName');
$propertyMap = new IncorrectWordMap();

$query = [
'templateName' => $templateName,
'text' => $text,
'language' => $language,
];

$records = $this->get('/templates/info', $query);
$records = $this->get('/proofing/check', $query);

if (is_array($records) && count($records) > 0) {
$ret = $this->buildPropertyMapArray($records, $propertyMap);
Expand All @@ -66,25 +72,80 @@ public function getTemplateInfo($templateName)
}

/**
* Execute a GET request via REST client
* Return an array of available dictionaries on the Reporting Cloud service
*
* @param string $uri URI
* @param array $query Query
* @return array|null
*/
public function getAvailableDictionaries()
{
$ret = null;

$dictionaries = $this->get('/proofing/availabledictionaries');

if (is_array($dictionaries) && count($dictionaries) > 0) {
$ret = array_map('trim', $dictionaries);
}

return $ret;
}

/**
* Return an array of suggestions for a misspelled word.
*
* @return mixed|null
* @param string $word Word that should be spell checked
* @param string $language Language of specified text
* @param int $max Maximum number of suggestions to return
*
* @return array|null
*/
protected function get($uri, $query = [])
public function getProofingSuggestions($word, $language, $max = 10)
{
$ret = null;

$options = [
RequestOptions::QUERY => $query,
StaticValidator::execute($word, 'TypeString');
StaticValidator::execute($language, 'Language');
StaticValidator::execute($max, 'TypeInteger');

$query = [
'word' => $word,
'language' => $language,
'max' => $max,
];

$response = $this->request('GET', $this->uri($uri), $options);
$records = $this->get('/proofing/suggestions', $query);

if ($response instanceof Response && 200 === $response->getStatusCode()) {
$ret = json_decode($response->getBody(), true);
if (is_array($records) && count($records) > 0) {
$ret = array_map('trim', $records);
}

return $ret;
}

/**
* Return an array of merge blocks and merge fields in a template file in template storage.
*
* @param string $templateName Template name
*
* @throws InvalidArgumentException
*
* @return array|null
*/
public function getTemplateInfo($templateName)
{
$ret = null;

$propertyMap = new TemplateInfoPropertyMap();

StaticValidator::execute($templateName, 'TemplateName');

$query = [
'templateName' => $templateName,
];

$records = $this->get('/templates/info', $query);

if (is_array($records) && count($records) > 0) {
$ret = $this->buildPropertyMapArray($records, $propertyMap);
}

return $ret;
Expand Down Expand Up @@ -277,6 +338,31 @@ public function downloadTemplate($templateName)
return $ret;
}

/**
* Execute a GET request via REST client
*
* @param string $uri URI
* @param array $query Query
*
* @return mixed|null
*/
protected function get($uri, $query = [])
{
$ret = null;

$options = [
RequestOptions::QUERY => $query,
];

$response = $this->request('GET', $this->uri($uri), $options);

if ($response instanceof Response && 200 === $response->getStatusCode()) {
$ret = json_decode($response->getBody(), true);
}

return $ret;
}


/**
* POST methods
Expand Down
Loading

0 comments on commit 1a3693f

Please sign in to comment.