-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
370 additions
and
6 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.01 | ||
1.02 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* PHP Speller | ||
* | ||
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @author Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
namespace Mekras\Speller\Source; | ||
|
||
/** | ||
* File as text source | ||
* | ||
* @since 1.02 | ||
*/ | ||
class FileSource implements Source | ||
{ | ||
/** | ||
* File name | ||
* | ||
* @var string | ||
* @since 1.02 | ||
*/ | ||
protected $filename; | ||
|
||
/** | ||
* Create new source | ||
* | ||
* @param string $filename | ||
* | ||
* @since 1.02 | ||
*/ | ||
public function __construct($filename) | ||
{ | ||
$this->filename = $filename; | ||
} | ||
|
||
/** | ||
* Return text as one string | ||
* | ||
* @return string | ||
* | ||
* @since 1.02 | ||
*/ | ||
public function getAsString() | ||
{ | ||
return file_get_contents($this->filename); | ||
} | ||
|
||
/** | ||
* Return file name with text to check | ||
* | ||
* This can be used for backends with file checking support | ||
* | ||
* @return string | ||
* | ||
* @since 1.02 | ||
*/ | ||
public function getFilename() | ||
{ | ||
return (string) $this->filename; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/** | ||
* PHP Speller | ||
* | ||
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @author Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
namespace Mekras\Speller\Source\Filter; | ||
|
||
/** | ||
* Filter interface | ||
* | ||
* Filters are used to filter out text, which does not require checking | ||
* | ||
* @since 1.02 | ||
*/ | ||
interface Filter | ||
{ | ||
/** | ||
* Filter string | ||
* | ||
* @param string $string string to be filtered | ||
* | ||
* @return string filtered string | ||
* | ||
* @since 1.02 | ||
*/ | ||
public function filter($string); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* PHP Speller | ||
* | ||
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @author Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
namespace Mekras\Speller\Source\Filter; | ||
|
||
/** | ||
* Filter which strips all input text | ||
* | ||
* All characters except new lines (\n), tabs (\t) and spaces will be replaces with spaces. | ||
* | ||
* @since 1.02 | ||
*/ | ||
class StripAllFilter implements Filter | ||
{ | ||
/** | ||
* Filter string | ||
* | ||
* @param string $string string to be filtered | ||
* | ||
* @return string filtered string | ||
* | ||
* @since 1.02 | ||
*/ | ||
public function filter($string) | ||
{ | ||
return preg_replace('/\S/', ' ', $string); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/** | ||
* PHP Speller | ||
* | ||
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @author Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
namespace Mekras\Speller\Source; | ||
|
||
use Mekras\Speller\Source\Filter\Filter; | ||
use Mekras\Speller\Source\Filter\StripAllFilter; | ||
|
||
/** | ||
* XLIFF translations as text source | ||
* | ||
* @link http://docs.oasis-open.org/xliff/xliff-core/v2.0/xliff-core-v2.0.html | ||
* @since 1.02 | ||
*/ | ||
class XliffSource extends FileSource | ||
{ | ||
/** | ||
* Text filters | ||
* | ||
* @var Filter[]|null[] | ||
*/ | ||
private $filters = [ | ||
'#<!--.*?-->#ums' => null, // Comments | ||
'#<[^>]+>#ums' => null // Top level tags | ||
]; | ||
|
||
/** | ||
* Add custom pattern to be filtered | ||
* | ||
* Matched text will be filtered with a given filter or with | ||
* {@link Mekras\Speller\Source\Filter\StripAllFilter} if $filter is null. | ||
* | ||
* @param string $pattern PCRE pattern. It is recommended to use "ums" PCRE modifiers. | ||
* @param Filter $filter filter to be applied | ||
* | ||
* @since 1.02 | ||
*/ | ||
public function addFilter($pattern, Filter $filter = null) | ||
{ | ||
$this->filters[(string) $pattern] = $filter; | ||
} | ||
|
||
/** | ||
* Return text as one string | ||
* | ||
* @return string | ||
* | ||
* @since 1.02 | ||
*/ | ||
public function getAsString() | ||
{ | ||
$text = parent::getAsString(); | ||
|
||
$stripAll = new StripAllFilter(); | ||
|
||
/* Removing CDATA tags */ | ||
$text = preg_replace_callback( | ||
'#<!\[CDATA\[(.*?)\]\]>#ums', | ||
function ($match) use ($stripAll) { | ||
$string = $match[1]; | ||
$string = preg_replace_callback( | ||
'#(<[^>]+>)([^<]*)(</[^>]+>)#', | ||
function ($match) use ($stripAll) { | ||
return $stripAll->filter($match[1]) . $match[2] | ||
. $stripAll->filter($match[3]); | ||
}, | ||
$string | ||
); | ||
return ' ' . $string . ' '; | ||
}, | ||
$text | ||
); | ||
|
||
/* Processing bottom level tags */ | ||
$text = preg_replace_callback( | ||
'#(<(\w+)(\s[^>]*?)?>)([^<>]*)(</\w+\s*>)#ums', | ||
function ($match) use ($stripAll) { | ||
if (strtolower($match[2]) == 'target') { | ||
$replace = $stripAll->filter($match[1]) . $match[4] | ||
. $stripAll->filter($match[5]); | ||
} else { | ||
$replace = $stripAll->filter($match[0]); | ||
} | ||
return $replace; | ||
}, | ||
$text | ||
); | ||
|
||
/* Other replacements */ | ||
foreach ($this->filters as $pattern => $filter) { | ||
if (null === $filter) { | ||
$filter = $stripAll; | ||
} | ||
$text = preg_replace_callback( | ||
$pattern, | ||
function ($match) use ($filter) { | ||
return $filter->filter($match[0]); | ||
}, | ||
$text | ||
); | ||
} | ||
|
||
return $text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* PHP Speller | ||
* | ||
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @author Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
namespace Mekras\Speller\Tests\Source; | ||
|
||
use Mekras\Speller\Source\FileSource; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
|
||
/** | ||
* Tests for Mekras\Speller\Source\FileSource | ||
* | ||
* @covers Mekras\Speller\Source\FileSource | ||
*/ | ||
class FileSourceTest extends TestCase | ||
{ | ||
/** | ||
* Test basic functions | ||
*/ | ||
public function testBasics() | ||
{ | ||
$source = new FileSource(__DIR__ . '/fixtures/test.txt'); | ||
static::assertEquals('Tiger, tiger, burning bright', $source->getAsString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* PHP Speller | ||
* | ||
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @author Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
namespace Mekras\Speller\Tests\Source\Filter; | ||
|
||
use Mekras\Speller\Source\Filter\StripAllFilter; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
|
||
/** | ||
* Tests for Mekras\Speller\Source\Filter\StripAllFilter | ||
* | ||
* @covers Mekras\Speller\Source\Filter\StripAllFilter | ||
*/ | ||
class StripAllFilterTest extends TestCase | ||
{ | ||
/** | ||
* Test basic functional | ||
*/ | ||
public function testBasics() | ||
{ | ||
$filter = new StripAllFilter(); | ||
static::assertEquals(" \n\t ", $filter->filter("foo\n\tbar")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* PHP Speller | ||
* | ||
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @author Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
namespace Mekras\Speller\Tests\Source; | ||
|
||
use Mekras\Speller\Source\XliffSource; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
|
||
/** | ||
* Tests for Mekras\Speller\Source\XliffSource | ||
* | ||
* @covers Mekras\Speller\Source\XliffSource | ||
*/ | ||
class XliffSourceTest extends TestCase | ||
{ | ||
/** | ||
* Test basic functional | ||
*/ | ||
public function testBasics() | ||
{ | ||
$source = new XliffSource(__DIR__ . '/fixtures/test.xliff'); | ||
$source->addFilter('#\{\{[^}]+\}\}#ums'); | ||
$lines = explode("\n", $source->getAsString()); | ||
static::assertCount(36, $lines); | ||
static::assertEquals('Foo', substr($lines[7], 17, 3)); | ||
static::assertEquals('Bar', substr($lines[12], 20, 3)); | ||
static::assertEquals('Bar', substr($lines[13], 20, 3)); | ||
static::assertNotContains('var', $lines[14]); | ||
static::assertEquals('Baz', substr($lines[27], 42, 3)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Tiger, tiger, burning bright |
Oops, something went wrong.