Skip to content

Commit

Permalink
Bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleigh committed Dec 8, 2015
1 parent 4799fb5 commit fa7da5d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 23 deletions.
43 changes: 34 additions & 9 deletions src/Helpers/PluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Limelight\Helpers;

use Limelight\Mecab\Node;
use Limelight\Config\Config;
use Limelight\Plugins\Plugin;

Expand All @@ -11,31 +12,55 @@ trait PluginHelper
* Run all registered plugins.
*
* @param string $text
* @param Node $node
* @param array $tokens
* @param array $words
* @param Node/null $node
* @param array/null $tokens
* @param array/null $words
* @param array $pluginWhiteList
*
* @return array
*/
protected function runPlugins($text, $node, $tokens, $words)
protected function runPlugins($text, $node, $tokens, $words, $pluginWhiteList = [])
{
$pluginResults = [];

$config = Config::getInstance();

$plugins = $config->getPlugins();
$allPlugins = $config->getPlugins();

foreach ($plugins as $plugin => $namespace) {
$this->validatePlugin($namespace);
foreach ($allPlugins as $plugin => $namespace) {
if ($this->isWhiteListed($plugin, $pluginWhiteList)) {
$this->validatePlugin($namespace);

$pluginClass = new $namespace($text, $node, $tokens, $words);
$pluginClass = new $namespace($text, $node, $tokens, $words);

$pluginResults[$plugin] = $this->firePlugin($pluginClass);
$pluginResults[$plugin] = $this->firePlugin($pluginClass);
}
}

return $pluginResults;
}

/**
* Whitelist is empty or plugin is in white list.
*
* @param string $plugin
* @param array $pluginWhiteList
*
* @return bool
*/
private function isWhiteListed($plugin, array $pluginWhiteList)
{
if (empty($pluginWhiteList)) {
return true;
}

array_map(function ($value) {
return ucfirst($value);
}, $pluginWhiteList);

return in_array($plugin, $pluginWhiteList);
}

/**
* Validate plugin class exists.
*
Expand Down
5 changes: 3 additions & 2 deletions src/Limelight.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ public function parse($text, $runPlugins = true)
* Run given text through plugins without mecab parsing. Kanji input will fail.
*
* @param string $text
* @param array $pluginWhiteList [Plugins to run]
*
* @return Limelight\Classes\LimelightResults/ InvalidInputException
*/
public function noParse($text)
public function noParse($text, $pluginWhiteList = ['Furigana', 'Romanji'])
{
$noParser = new NoParser();

return $noParser->handle($text);
return $noParser->handle($text, $pluginWhiteList);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Parse/NoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ class NoParser
* Handle the no-parse for given text.
*
* @param string $text
* @param array $pluginWhiteList
*
* @return LimelightResults
*/
public function handle($text)
public function handle($text, array $pluginWhiteList)
{
if ($this->hasKanji($text)) {
throw new InvalidInputException('Text must not contain kanji.');
Expand All @@ -38,7 +39,7 @@ public function handle($text)

$words = [new LimelightWord($token, $properties, $converter)];

$pluginResults = $this->runPlugins($text, null, $token, $words);
$pluginResults = $this->runPlugins($text, null, $token, $words, $pluginWhiteList);

return new LimelightResults($text, $words, $pluginResults);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Library/Romanji/Lib/Hepburn.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@
''=>'xa', ''=>'xi', ''=>'xu', ''=>'xe', ''=>'xo',
''=>'xka', ''=>'xke', ''=>'xwa',
''=>'.', ''=>',', ''=>'?', ''=>'!',
'' =>'"', ''=>'"'
'' =>'"', ''=>'"', ' ' => ' '
];
2 changes: 1 addition & 1 deletion src/Plugins/Library/Romanji/Lib/HepburnTraditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
''=>'xa', ''=>'xi', ''=>'xu', ''=>'xe', ''=>'xo',
''=>'xka', ''=>'xke', ''=>'xwa',
''=>'.', ''=>',', ''=>'?', ''=>'!',
'' =>'"', ''=>'"'
'' =>'"', ''=>'"', ' ' => ' '
];
2 changes: 1 addition & 1 deletion src/Plugins/Library/Romanji/Lib/KunreiShiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
''=>'xa', ''=>'xi', ''=>'xu', ''=>'xe', ''=>'xo',
''=>'xka', ''=>'xke', ''=>'xwa',
''=>'.', ''=>',', ''=>'?', ''=>'!',
'' =>'"', ''=>'"'
'' =>'"', ''=>'"', ' ' => ' '
];
2 changes: 1 addition & 1 deletion src/Plugins/Library/Romanji/Lib/NihonShiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
''=>'xa', ''=>'xi', ''=>'xu', ''=>'xe', ''=>'xo',
''=>'xka', ''=>'xke', ''=>'xwa',
''=>'.', ''=>',', ''=>'?', ''=>'!',
'' =>'"', ''=>'"'
'' =>'"', ''=>'"', ' ' => ' '
];
26 changes: 20 additions & 6 deletions tests/Parse/NoParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public static function setUpBeforeClass()
}

/**
* It parses kana text.
*
* @test
*/
public function it_parses_kana_text()
Expand All @@ -33,8 +31,6 @@ public function it_parses_kana_text()
}

/**
* It gets romanji for kana text.
*
* @test
*/
public function it_gets_romanji_for_kana_text()
Expand All @@ -45,8 +41,26 @@ public function it_gets_romanji_for_kana_text()
}

/**
* It throws exception for kanji input.
*
* @test
*/
public function it_doesnt_run_plugins_not_in_given_whitelist()
{
$results = self::$limelight->noParse('ねんがっぴ', ['Furigana']);

$this->assertEquals('', $results->plugin('romanji'));
}

/**
* @test
*/
public function it_capitalizes_items_in_whitelist()
{
$results = self::$limelight->noParse('ねんがっぴ', ['furigana']);

$this->assertEquals('', $results->plugin('romanji'));
}

/**
* @test
* @expectedException Limelight\Exceptions\InvalidInputException
* @expectedExceptionMessage Text must not contain kanji.
Expand Down

0 comments on commit fa7da5d

Please sign in to comment.