Skip to content

Commit

Permalink
Adds feature returning array for single word
Browse files Browse the repository at this point in the history
  • Loading branch information
heiglandreas committed Oct 14, 2012
1 parent 8573ceb commit f8e84f3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
29 changes: 29 additions & 0 deletions docs/examples.rst
Expand Up @@ -64,6 +64,35 @@ Invoke the ``Hyphenator`` manually
// prints We have some re-al-ly long words in ger-man like sau-er-stoff-feld-fla-sche.
// Thanks to lsmith for the idea!

Get the hyphenation of a single word as array
=============================================

::
<?php
use \Org\Heigl\Hyphenator as h;
$o = new h\Options();
$o->setHyphen('-')
->setDefaultLocale('de_DE')
->setRightMin(2)
->setLeftMin(2)
->setWordMin(5)
->setFilters('NonStandard')
->setTokenizers('Whitespace','Punctuation');
$h = new h\Hyphenator();
$h->setOptions($o);
var_Dump($h->hyphenate('Donaudampfschifffahrt'));
// array(4) {
// [0] =>
// string(22) "Do-naudampfschifffahrt"
// [1] =>
// string(22) "Donau-dampfschifffahrt"
// [2] =>
// string(22) "Donaudampf-schifffahrt"
// [3] =>
// string(22) "Donaudampfschiff-fahrt"
// }

.. warning::

Performance-Hint: Due to the sometimes rather large hyphenation-pattern-files it
Expand Down
8 changes: 6 additions & 2 deletions src/Org/Heigl/Hyphenator/Hyphenator.php
Expand Up @@ -362,8 +362,12 @@ public function hyphenate ( $string )
$tokens = $this->_tokenizers->tokenize($string);
$tokens = $this->getHyphenationPattern($tokens);
$tokens = $this->filter($tokens);
$return = $this->getFilters()->concatenate($tokens);

if (1 === sizeof($tokens) && 1 === $this->getFilters()->count()) {
$tokens->rewind();
$return = $tokens->current()->getHyphenatedContent();
} else {
$return = $this->getFilters()->concatenate($tokens);
}
return $return;
}

Expand Down
77 changes: 77 additions & 0 deletions tests/Org/Heigl/HyphenatorFeatureTest.php
@@ -0,0 +1,77 @@
<?php
/**
* Copyright (c) 2008-2011 Andreas Heigl<andreas@heigl.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @category Org_Heigl
* @package Org_Heigl_Hyphenator
* @author Andreas Heigl <andreas@heigl.org>
* @copyright 2008-2011 Andreas Heigl<andreas@heigl.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT-License
* @version 2.0.1
* @since 02.11.2011
*/

namespace Org\Heigl\HyphenatorTest;

use \Org\Heigl\Hyphenator as h;

/**
* This class tests the functionality of the class Org_Heigl_Hyphenator
*
* @category Org_Heigl
* @package Org_Heigl_Hyphenator
* @author Andreas Heigl <andreas@heigl.org>
* @copyright 2008 Andreas Heigl<andreas@heigl.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT-License
* @version 2.0.1
* @since 20.04.2009
*/
class HyphenatorFeatureTest extends \PHPUnit_Framework_TestCase
{

/**
* @dataProvider hyphenationOfSingleWordWithArrayOutputProvider
*/
public function testHyphenationOfSingleWordWithArrayOutput($word, $language, $expected)
{
$o = new h\Options();
$o->setHyphen('-')
->setDefaultLocale($language)
->setRightMin(2)
->setLeftMin(2)
->setWordMin(5)
->setFilters('NonStandard')
->setTokenizers('Whitespace','Punctuation');

$h = new h\Hyphenator();
$h->setOptions($o);
var_Dump($h->hyphenate($word));
$this->assertEquals($expected, $h->hyphenate($word));

}

public function hyphenationOfSingleWordWithArrayOutputProvider()
{
return array(
array('donaudampfschifffahrt', 'de_DE', array('do-naudampfschifffahrt', 'donau-dampfschifffahrt', 'donaudampf-schifffahrt', 'donaudampfschiff-fahrt')),
);
}
}

0 comments on commit f8e84f3

Please sign in to comment.