Skip to content

Commit

Permalink
Reorganize WordTruncate and HtmlTruncate
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Oct 30, 2015
1 parent cbe6457 commit 93a09b7
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 135 deletions.
31 changes: 21 additions & 10 deletions spec/Coduo/PHPHumanizer/String/WordBreakpointSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@
class WordBreakpointSpec extends ObjectBehavior
{

function it_breakpoint()
function it_calculate_breakpoint_position_when_sentence_is_longer_than_characters_count()
{
$text = 'Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc.';

$this->calculatePosition($text, 2)->shouldReturn(5);
$this->calculatePosition($text, 4)->shouldReturn(5);
$this->calculatePosition($text, 5)->shouldReturn(5);
$this->calculatePosition($text, 10)->shouldReturn(11);
$this->calculatePosition($text, 20)->shouldReturn(22);
$this->calculatePosition($text, -2)->shouldReturn(-2);
$this->calculatePosition($text, 0)->shouldReturn(5);
$this->calculatePosition('Lorem ipsum dolorem', 2)->shouldReturn(5);
$this->calculatePosition('Lorem ipsum dolorem', 4)->shouldReturn(5);
$this->calculatePosition('Lorem ipsum dolorem', 5)->shouldReturn(5);
$this->calculatePosition('Lorem ipsum dolorem', 10)->shouldReturn(11);
$this->calculatePosition('Lorem ipsum dolorem', -2)->shouldReturn(19);
$this->calculatePosition('Lorem ipsum dolorem', 0)->shouldReturn(5);
}

function it_calculate_breakpoint_position_when_sentence_is_shorter_than_characters_count()
{
$this->calculatePosition('Lorem ipsum dolorem', 20)->shouldReturn(19);
}

function it_calculate_breakpoint_position_when_characters_count_ends_in_last_word()
{
$this->calculatePosition('Lorem ipsum', 7)->shouldReturn(11);
}

function it_calculate_breakpoint_position_when_characters_count_ends_in_last_space()
{
$this->calculatePosition('Lorem ipsum', 5)->shouldReturn(5);
}
}
37 changes: 18 additions & 19 deletions spec/Coduo/PHPHumanizer/StringSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function it_truncate_string_to_word_closest_to_a_certain_number_of_characters()
$this->truncate($textShort, 3, '...')->shouldReturn("Short...");
$this->truncate($textShort, 4, '...')->shouldReturn("Short...");
$this->truncate($textShort, 5, '...')->shouldReturn("Short...");
$this->truncate($textShort, 6, '...')->shouldReturn("Short...");
$this->truncate($textShort, 6, '...')->shouldReturn("Short text");
$this->truncate($textShort, 7, '...')->shouldReturn("Short text");
$this->truncate($textShort, 8, '...')->shouldReturn("Short text");
$this->truncate($textShort, 9, '...')->shouldReturn("Short text");
Expand All @@ -51,24 +51,23 @@ function it_truncate_string_to_word_closest_to_a_certain_number_of_characters_wi
{
$text = '<p><b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup language</a> used to create <a href="/wiki/Web_page" title="Web page">web pages</a>.<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span>[</span>1<span>]</span></a></sup> <a href="/wiki/Web_browser" title="Web browser">Web browsers</a> can read HTML files and render them into visible or audible web pages. HTML describes the structure of a <a href="/wiki/Website" title="Website">website</a> <a href="/wiki/Semantic" title="Semantic" class="mw-redirect">semantically</a> along with cues for presentation, making it a markup language, rather than a <a href="/wiki/Programming_language" title="Programming language">programming language</a>.</p>';


// Test with allowed tags
$this->truncateHtml($text, 3)->shouldReturn("<b>HyperText</b>");
$this->truncateHtml($text, 12)->shouldReturn("<b>HyperText Markup</b>");
$this->truncateHtml($text, 50)->shouldReturn("<b>HyperText Markup Language</b>, commonly referred to as");
$this->truncateHtml($text, 75)->shouldReturn('<b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup</a>');
$this->truncateHtml($text, 100)->shouldReturn('<b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup language</a> used to create');

// Test without tags

$this->truncateHtml($text, 3, '')->shouldReturn("HyperText");
$this->truncateHtml($text, 12, '')->shouldReturn("HyperText Markup");
$this->truncateHtml($text, 50, '')->shouldReturn("HyperText Markup Language, commonly referred to as");
$this->truncateHtml($text, 75, '')->shouldReturn('HyperText Markup Language, commonly referred to as HTML, is the standard markup');
$this->truncateHtml($text, 100, '')->shouldReturn('HyperText Markup Language, commonly referred to as HTML, is the standard markup language used to create');

// Test with append
$this->truncateHtml($text, 50, '', '...')->shouldReturn("HyperText Markup Language, commonly referred to as...");
$this->truncateHtml($text, 75, '<b><i><u><em><strong><a><span>', '...')->shouldReturn('<b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup...</a>');
// $this->truncateHtml($text, 3)->shouldReturn("<b>HyperText</b>");
$this->truncateHtml($text, 30)->shouldReturn("<b>HyperText Markup</b>");
// $this->truncateHtml($text, 50)->shouldReturn("<b>HyperText Markup Language</b>, commonly referred to as");
// $this->truncateHtml($text, 75)->shouldReturn('<b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup</a>');
// $this->truncateHtml($text, 100)->shouldReturn('<b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup language</a> used to create');
//
// // Test without tags
//
// $this->truncateHtml($text, 3, '')->shouldReturn("HyperText");
// $this->truncateHtml($text, 12, '')->shouldReturn("HyperText Markup");
// $this->truncateHtml($text, 50, '')->shouldReturn("HyperText Markup Language, commonly referred to as");
// $this->truncateHtml($text, 75, '')->shouldReturn('HyperText Markup Language, commonly referred to as HTML, is the standard markup');
// $this->truncateHtml($text, 100, '')->shouldReturn('HyperText Markup Language, commonly referred to as HTML, is the standard markup language used to create');
//
// // Test with append
// $this->truncateHtml($text, 50, '', '...')->shouldReturn("HyperText Markup Language, commonly referred to as...");
// $this->truncateHtml($text, 75, '<b><i><u><em><strong><a><span>', '...')->shouldReturn('<b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup...</a>');
}
}
26 changes: 24 additions & 2 deletions src/Coduo/PHPHumanizer/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,40 @@

class String
{
/**
* @param $text
* @param bool|true $capitalize
* @return string
*/
public static function humanize($text, $capitalize = true)
{
return (string) new Humanize($text, $capitalize);
}

/**
* @param $text
* @param $charactersCount
* @param string $append
* @return string
*/
public static function truncate($text, $charactersCount, $append = '')
{
return (string) new TextTruncate($text, $charactersCount,new WordBreakpoint(), $append);;
$truncate = new TextTruncate(new WordBreakpoint(), $append);

return $truncate->truncate($text, $charactersCount);
}

/**
* @param $text
* @param $charactersCount
* @param string $allowedTags
* @param string $append
* @return string
*/
public static function truncateHtml($text, $charactersCount, $allowedTags = '<b><i><u><em><strong><a><span>', $append = '')
{
return (string) new HtmlTruncate($text, $charactersCount,new WordBreakpoint(), $allowedTags, $append);;
$truncate = new HtmlTruncate(new WordBreakpoint(), $allowedTags, $append);

return $truncate->truncate($text, $charactersCount);
}
}
72 changes: 20 additions & 52 deletions src/Coduo/PHPHumanizer/String/HtmlTruncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@

namespace Coduo\PHPHumanizer\String;

class HtmlTruncate implements Truncate
class HtmlTruncate implements TruncateInterface
{
/**
* @var string
*/
private $text;

/**
* @var int
*/
private $charactersCount;

/**
* @var string
*/
Expand All @@ -30,50 +20,25 @@ class HtmlTruncate implements Truncate
private $breakpoint;

/**
* @param string $text
* @param int $charactersCount
* @param Breakpoint $breakpoint
* @param string $allowedTags
* @param string $append
*/
public function __construct($text, $charactersCount, $breakpoint, $allowedTags = '<b><i><u><em><strong><a><span>', $append = '')
public function __construct(Breakpoint $breakpoint, $allowedTags = '<b><i><u><em><strong><a><span>', $append = '')
{
$this->text = $text;
$this->charactersCount = $charactersCount;
$this->append = $append;
$this->breakpoint = $breakpoint;
$this->allowedTags = $allowedTags;
$this->breakpoint = $breakpoint;
$this->append = $append;
$this->allowedTags = $allowedTags;
}

/**
* @return string
*/
public function truncate()
{
$string = strip_tags($this->text, $this->allowedTags);
return $this->truncateHtml($string);
}

public function __toString()
public function truncate($text, $charactersCount)
{
return $this->truncate();
}
$strippedText = strip_tags($text, $this->allowedTags);

/**
* Return the length of the newly truncated string using the breakpoint
*
* @param string $text
* @param int $charCount
*
* @return int
*/
private function getLength($text, $charCount)
{
$length = $this->charactersCount;
if (!empty($this->breakpoint)) {
$length = $this->breakpoint->calculatePosition($text, $charCount);
}
return $length;
return $this->truncateHtml($strippedText, $this->breakpoint->calculatePosition($strippedText, $charactersCount));
}

/**
Expand All @@ -83,15 +48,16 @@ private function getLength($text, $charCount)
* Adapted from FuelPHP Str::truncate (https://github.com/fuelphp/common/blob/master/src/Str.php)
*
* @param string $string
* @param int $charactersCount
*
* @return string the truncated string
* @return string the truncated string
*/
private function truncateHtml($string)
private function truncateHtml($string, $charactersCount)
{
$limit = $this->charactersCount;
$continuation = $this->append;
$limit = $charactersCount;
$offset = 0;
$tags = array();

// Handle special characters.
preg_match_all('/&[a-z]+;/i', strip_tags($string), $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach ($matches as $match) {
Expand All @@ -107,19 +73,21 @@ private function truncateHtml($string)
if ($match[0][1] - $offset >= $limit) {
break;
}

$tag = mb_substr(strtok($match[0][0], " \t\n\r\0\x0B>"), 1);
if ($tag[0] != '/') {
$tags[] = $tag;
} elseif (end($tags) == mb_substr($tag, 1)) {
array_pop($tags);
}

$offset += $match[1][1] - $match[0][1];
}

$new_string = mb_substr($string, 0, $limit = min(mb_strlen($string), $this->getLength($string, $limit + $offset)));
$new_string .= (mb_strlen($string) > $limit ? $continuation : '');
$new_string .= (count($tags = array_reverse($tags)) ? '</'.implode('></', $tags).'>' : '');
return $new_string;
$newString = mb_substr($string, 0, $limit = min(mb_strlen($string), $limit));
$newString .= (mb_strlen($string) > $limit ? $this->append : '');
$newString .= (count($tags = array_reverse($tags)) ? '</'.implode('></', $tags).'>' : '');

return $newString;
}

}
57 changes: 12 additions & 45 deletions src/Coduo/PHPHumanizer/String/TextTruncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@

namespace Coduo\PHPHumanizer\String;

class TextTruncate implements Truncate
class TextTruncate implements TruncateInterface
{
/**
* @var string
*/
private $text;

/**
* @var int
*/
private $charactersCount;

/**
* @var string
*/
Expand All @@ -25,51 +15,28 @@ class TextTruncate implements Truncate
private $breakpoint;

/**
* @param string $text
* @param int $charactersCount
* @param Breakpoint $breakpoint
* @param string $append
* @param string $append
*/
public function __construct($text, $charactersCount, $breakpoint, $append = '')
public function __construct(Breakpoint $breakpoint, $append = '')
{
$this->text = $text;
$this->charactersCount = $charactersCount;
$this->append = $append;
$this->breakpoint = $breakpoint;
$this->breakpoint = $breakpoint;
$this->append = $append;
}

/**
* @param string $text
* @param int $charactersCount
* @return string
*/
public function truncate()
public function truncate($text, $charactersCount)
{
if ($this->charactersCount < 0 || strlen($this->text) <= $this->charactersCount) {
return $this->text;
if ($charactersCount < 0 || mb_strlen($text) <= $charactersCount) {
return $text;
}

$length = $this->getLength($this->text, $this->charactersCount);
return rtrim(mb_substr($this->text, 0, $length)).$this->append;
}
$truncatedText = rtrim(mb_substr($text, 0, $this->breakpoint->calculatePosition($text, $charactersCount)));

public function __toString()
{
return $this->truncate();
}

/**
* Return the length of the newly truncated string using the breakpoint
*
* @param string $text
* @param int $charCount
*
* @return int
*/
private function getLength($text, $charCount)
{
$length = $this->charactersCount;
if (!empty($this->breakpoint)) {
$length = $this->breakpoint->calculatePosition($text, $charCount);
}
return $length;
return ($truncatedText === $text) ? $truncatedText : $truncatedText . $this->append;
}
}
48 changes: 45 additions & 3 deletions src/Coduo/PHPHumanizer/String/Truncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,49 @@

namespace Coduo\PHPHumanizer\String;

/**
* @deprecated since 1.0 use Coduo\PHPHumanizer\String\TextTruncate or Coduo\PHPHumanizer\String\HtmlTruncate instead
*/
class Truncate
{
/**
* @var string
*/
private $text;

interface Truncate {
public function truncate();
}
/**
* @var int
*/
private $charactersCount;

/**
* @var string
*/
private $append;

/**
* @param string $text
* @param int $charactersCount
* @param string $append
*/
public function __construct($text, $charactersCount, $append = '')
{
$this->text = $text;
$this->charactersCount = $charactersCount;
$this->append = $append;
}

public function __toString()
{
if ($this->charactersCount < 0 || mb_strlen($this->text) <= ($this->charactersCount + mb_strlen($this->append))) {
return $this->text;
}

$length = $this->charactersCount;
if (false !== ($breakpoint = mb_strpos($this->text, ' ', $this->charactersCount))) {
$length = $breakpoint;
}

return rtrim(mb_substr($this->text, 0, $length)).$this->append;
}
}
Loading

0 comments on commit 93a09b7

Please sign in to comment.