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

Commit 1a3693f

Browse files
Merge pull request #7 from TextControl/proofing
Proofing
2 parents 65e61bd + d6951cc commit 1a3693f

30 files changed

Lines changed: 610 additions & 142 deletions

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
## dev-master
66

7+
## 1.4.0 - 2017-10-02
8+
9+
* Implemented [end-points](https://www.textcontrol.com/blog/2017/08/23/):
10+
* `/v1/proofing/check`
11+
* `/v1/proofing/availabledictionaries`
12+
* `/v1/proofing/suggestions`
13+
* Updated `AccountSettings` property map with keys:
14+
* `proofing_transactions`
15+
* `max_proofing_transactions`
16+
* Refactored `Validator` unit tests.
17+
* Added script to download and build _available dictionaries_ resource file.
18+
719
## 1.3.4 - 2017-08-22
820

921
* Implemented the `mergeSettings` option `merge_html`.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* Available Dictionaries Resource File
5+
*
6+
* This script downloads all the available dictionaries from the Reporting Cloud Web API and writes them to the file:
7+
*
8+
* resource/available-dictionaries.php
9+
*
10+
* The package maintainer should execute this script, whenever new dictionaries are added to the backend.
11+
*
12+
*/
13+
14+
include_once 'bootstrap.php';
15+
16+
use TxTextControl\ReportingCloud\Console\Helper;
17+
use TxTextControl\ReportingCloud\Exception\RuntimeException;
18+
use TxTextControl\ReportingCloud\ReportingCloud;
19+
20+
$outputFilename = realpath(__DIR__ . '/../resource/available-dictionaries.php');
21+
22+
// ---------------------------------------------------------------------------------------------------------------------
23+
24+
$reportingCloud = new ReportingCloud([
25+
'username' => Helper::username(),
26+
'password' => Helper::password(),
27+
]);
28+
29+
$availableDictionaries = $reportingCloud->getAvailableDictionaries();
30+
31+
sort($availableDictionaries);
32+
33+
if (0 === count($availableDictionaries)) {
34+
throw new RuntimeException('Cannot download the available dictionaries from the Reporting Cloud Web API.');
35+
}
36+
37+
// ---------------------------------------------------------------------------------------------------------------------
38+
39+
$buffer = '<?php';
40+
$buffer .= PHP_EOL;
41+
$buffer .= PHP_EOL;
42+
$buffer .= 'return ';
43+
$buffer .= var_export($availableDictionaries, true);
44+
$buffer .= ';';
45+
$buffer .= PHP_EOL;
46+
47+
file_put_contents($outputFilename, $buffer);
48+
49+
// ---------------------------------------------------------------------------------------------------------------------
50+
51+
echo PHP_EOL;
52+
echo sprintf('The available dictionaries are %s.', implode(', ', $availableDictionaries));
53+
echo PHP_EOL;
54+
echo PHP_EOL;
55+
echo sprintf('Written resource file to %s', $outputFilename);
56+
echo PHP_EOL;
57+
echo PHP_EOL;
58+
59+
// ---------------------------------------------------------------------------------------------------------------------

demo/proofing.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
include_once 'bootstrap.php';
4+
5+
use TxTextControl\ReportingCloud\Console\Helper;
6+
use TxTextControl\ReportingCloud\ReportingCloud;
7+
8+
$reportingCloud = new ReportingCloud([
9+
'username' => Helper::username(),
10+
'password' => Helper::password(),
11+
]);
12+
13+
// ---------------------------------------------------------------------------------------------------------------------
14+
15+
var_dump($reportingCloud->getAvailableDictionaries());
16+
17+
// ---------------------------------------------------------------------------------------------------------------------
18+
19+
var_dump($reportingCloud->getProofingSuggestions('ssky', 'en_US.dic', 10));
20+
21+
// ---------------------------------------------------------------------------------------------------------------------
22+
23+
var_dump($reportingCloud->proofingCheck('Thiss is a testt about rockkets in the ssky', 'en_US.dic'));
24+
25+
// ---------------------------------------------------------------------------------------------------------------------
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
return array (
4+
0 => 'af_ZA.dic',
5+
1 => 'an_ES.dic',
6+
2 => 'ar.dic',
7+
3 => 'be_BY.dic',
8+
4 => 'bg_BG.dic',
9+
5 => 'br_FR.dic',
10+
6 => 'ca.dic',
11+
7 => 'cs_CZ.dic',
12+
8 => 'da_DK.dic',
13+
9 => 'de_AT_frami.dic',
14+
10 => 'de_CH_frami.dic',
15+
11 => 'de_DE_frami.dic',
16+
12 => 'el_GR.dic',
17+
13 => 'en_AU.dic',
18+
14 => 'en_CA.dic',
19+
15 => 'en_GB.dic',
20+
16 => 'en_US.dic',
21+
17 => 'en_US_Legal.txd',
22+
18 => 'en_US_OpenMedSpel.dic',
23+
19 => 'en_ZA.dic',
24+
20 => 'es_ES.dic',
25+
21 => 'et_EE.dic',
26+
22 => 'fa.dic',
27+
23 => 'fr.dic',
28+
24 => 'gd_GB.dic',
29+
25 => 'gl_ES.dic',
30+
26 => 'he_IL.dic',
31+
27 => 'hr_HR.dic',
32+
28 => 'hy_AM.dic',
33+
29 => 'it_IT.dic',
34+
30 => 'ku_TR.dic',
35+
31 => 'lt_LT.dic',
36+
32 => 'nb_NO.dic',
37+
33 => 'nl_NL.dic',
38+
34 => 'nn_NO.dic',
39+
35 => 'oc_FR.dic',
40+
36 => 'pl_PL.dic',
41+
37 => 'pt_BR.dic',
42+
38 => 'pt_PT.dic',
43+
39 => 'ro_RO.dic',
44+
40 => 'ru_RU.dic',
45+
41 => 'sh.dic',
46+
42 => 'sk_SK.dic',
47+
43 => 'sl_SI.dic',
48+
44 => 'sr.dic',
49+
45 => 'sv_SE.dic',
50+
46 => 'uk_UA.dic',
51+
47 => 'ur.dic',
52+
48 => 'vi_VN.dic',
53+
);

src/PropertyMap/AccountSettings.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ class AccountSettings extends AbstractPropertyMap
2727
public function __construct()
2828
{
2929
$this->setMap([
30-
'serialNumber' => 'serial_number',
31-
'createdDocuments' => 'created_documents',
32-
'uploadedTemplates' => 'uploaded_templates',
33-
'maxDocuments' => 'max_documents',
34-
'maxTemplates' => 'max_templates',
35-
'validUntil' => 'valid_until',
30+
'serialNumber' => 'serial_number',
31+
'createdDocuments' => 'created_documents',
32+
'uploadedTemplates' => 'uploaded_templates',
33+
'maxDocuments' => 'max_documents',
34+
'maxTemplates' => 'max_templates',
35+
'validUntil' => 'valid_until',
36+
'proofingTransactions' => 'proofing_transactions', // added in 1.4.0
37+
'maxProofingTransactions' => 'max_proofing_transactions', // added in 1.4.0
3638
]);
3739
}
3840
}

src/PropertyMap/IncorrectWord.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* ReportingCloud PHP Wrapper
5+
*
6+
* PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7+
*
8+
* @link http://www.reporting.cloud to learn more about ReportingCloud
9+
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10+
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11+
* @copyright © 2017 Text Control GmbH
12+
*/
13+
14+
namespace TxTextControl\ReportingCloud\PropertyMap;
15+
16+
/**
17+
* IncorrectWord property map
18+
*
19+
* @package TxTextControl\ReportingCloud
20+
* @author Jonathan Maron (@JonathanMaron)
21+
*/
22+
class IncorrectWord extends AbstractPropertyMap
23+
{
24+
/**
25+
* Set the property map of IncorrectWord
26+
*/
27+
public function __construct()
28+
{
29+
$this->setMap([
30+
'length' => 'length',
31+
'start' => 'start',
32+
'text' => 'text',
33+
'isDuplicate' => 'is_duplicate',
34+
'language' => 'language',
35+
]);
36+
}
37+
}

src/ReportingCloud.php

Lines changed: 104 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use TxTextControl\ReportingCloud\PropertyMap\AccountSettings as AccountSettingsPropertyMap;
2121
use TxTextControl\ReportingCloud\PropertyMap\TemplateInfo as TemplateInfoPropertyMap;
2222
use TxTextControl\ReportingCloud\PropertyMap\TemplateList as TemplateListPropertyMap;
23+
use TxTextControl\ReportingCloud\PropertyMap\IncorrectWord as IncorrectWordMap;
2324
use TxTextControl\ReportingCloud\Validator\StaticValidator;
2425

2526
/**
@@ -36,27 +37,32 @@ class ReportingCloud extends AbstractReportingCloud
3637
*/
3738

3839
/**
39-
* Return an array of merge blocks and merge fields in a template file in template storage.
40+
* Check a corpus of text for spelling errors.
4041
*
41-
* @param string $templateName Template name
42+
* Return an array of misspelled words, if spelling errors are found in the corpus of text.
4243
*
43-
* @throws InvalidArgumentException
44+
* Return null, if no misspelled words are found in the corpus of text.
45+
*
46+
* @param string $text Corpus of text that should be spell checked
47+
* @param string $language Language of specified text
4448
*
4549
* @return array|null
4650
*/
47-
public function getTemplateInfo($templateName)
51+
public function proofingCheck($text, $language)
4852
{
4953
$ret = null;
5054

51-
$propertyMap = new TemplateInfoPropertyMap();
55+
StaticValidator::execute($text, 'TypeString');
56+
StaticValidator::execute($language, 'Language');
5257

53-
StaticValidator::execute($templateName, 'TemplateName');
58+
$propertyMap = new IncorrectWordMap();
5459

5560
$query = [
56-
'templateName' => $templateName,
61+
'text' => $text,
62+
'language' => $language,
5763
];
5864

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

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

6874
/**
69-
* Execute a GET request via REST client
75+
* Return an array of available dictionaries on the Reporting Cloud service
7076
*
71-
* @param string $uri URI
72-
* @param array $query Query
77+
* @return array|null
78+
*/
79+
public function getAvailableDictionaries()
80+
{
81+
$ret = null;
82+
83+
$dictionaries = $this->get('/proofing/availabledictionaries');
84+
85+
if (is_array($dictionaries) && count($dictionaries) > 0) {
86+
$ret = array_map('trim', $dictionaries);
87+
}
88+
89+
return $ret;
90+
}
91+
92+
/**
93+
* Return an array of suggestions for a misspelled word.
7394
*
74-
* @return mixed|null
95+
* @param string $word Word that should be spell checked
96+
* @param string $language Language of specified text
97+
* @param int $max Maximum number of suggestions to return
98+
*
99+
* @return array|null
75100
*/
76-
protected function get($uri, $query = [])
101+
public function getProofingSuggestions($word, $language, $max = 10)
77102
{
78103
$ret = null;
79104

80-
$options = [
81-
RequestOptions::QUERY => $query,
105+
StaticValidator::execute($word, 'TypeString');
106+
StaticValidator::execute($language, 'Language');
107+
StaticValidator::execute($max, 'TypeInteger');
108+
109+
$query = [
110+
'word' => $word,
111+
'language' => $language,
112+
'max' => $max,
82113
];
83114

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

86-
if ($response instanceof Response && 200 === $response->getStatusCode()) {
87-
$ret = json_decode($response->getBody(), true);
117+
if (is_array($records) && count($records) > 0) {
118+
$ret = array_map('trim', $records);
119+
}
120+
121+
return $ret;
122+
}
123+
124+
/**
125+
* Return an array of merge blocks and merge fields in a template file in template storage.
126+
*
127+
* @param string $templateName Template name
128+
*
129+
* @throws InvalidArgumentException
130+
*
131+
* @return array|null
132+
*/
133+
public function getTemplateInfo($templateName)
134+
{
135+
$ret = null;
136+
137+
$propertyMap = new TemplateInfoPropertyMap();
138+
139+
StaticValidator::execute($templateName, 'TemplateName');
140+
141+
$query = [
142+
'templateName' => $templateName,
143+
];
144+
145+
$records = $this->get('/templates/info', $query);
146+
147+
if (is_array($records) && count($records) > 0) {
148+
$ret = $this->buildPropertyMapArray($records, $propertyMap);
88149
}
89150

90151
return $ret;
@@ -277,6 +338,31 @@ public function downloadTemplate($templateName)
277338
return $ret;
278339
}
279340

341+
/**
342+
* Execute a GET request via REST client
343+
*
344+
* @param string $uri URI
345+
* @param array $query Query
346+
*
347+
* @return mixed|null
348+
*/
349+
protected function get($uri, $query = [])
350+
{
351+
$ret = null;
352+
353+
$options = [
354+
RequestOptions::QUERY => $query,
355+
];
356+
357+
$response = $this->request('GET', $this->uri($uri), $options);
358+
359+
if ($response instanceof Response && 200 === $response->getStatusCode()) {
360+
$ret = json_decode($response->getBody(), true);
361+
}
362+
363+
return $ret;
364+
}
365+
280366

281367
/**
282368
* POST methods

0 commit comments

Comments
 (0)