Skip to content

Commit

Permalink
refacto truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Smeeckaert committed Oct 30, 2015
1 parent 798d77d commit cc7a56a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 27 deletions.
57 changes: 48 additions & 9 deletions src/Coduo/PHPHumanizer/String/HtmlTruncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Coduo\PHPHumanizer\String;

class HtmlTruncate extends TextTruncate
class HtmlTruncate implements Truncate
{
/**
* @var string
Expand All @@ -24,6 +24,11 @@ class HtmlTruncate extends TextTruncate
*/
private $allowedTags;

/**
* @var Breakpoint
*/
private $breakpoint;

/**
* @param string $text
* @param int $charactersCount
Expand All @@ -38,6 +43,48 @@ public function __construct($text, $charactersCount, $allowedTags = '<b><i><u><e
$this->allowedTags = $allowedTags;
}

/**
* @param Breakpoint $breakpoint
*
* @return TextTruncate
*/
public function setBreakpoint($breakpoint)
{
$this->breakpoint = $breakpoint;
return $this;
}

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

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;
}

/**
* Truncates a string to the given length. It will optionally preserve
* HTML tags if $is_html is set to true.
Expand Down Expand Up @@ -84,12 +131,4 @@ private function truncateHtml($string)
return $new_string;
}

/**
* @return string
*/
public function truncate()
{
$string = strip_tags($this->text, $this->allowedTags);
return $this->truncateHtml($string);
}
}
38 changes: 20 additions & 18 deletions src/Coduo/PHPHumanizer/String/TextTruncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,15 @@ public function __construct($text, $charactersCount, $append = '')

/**
* @param Breakpoint $breakpoint
*
* @return TextTruncate
*/
public function setBreakpoint($breakpoint) {
public function setBreakpoint($breakpoint)
{
$this->breakpoint = $breakpoint;
return $this;
}

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

/**
* @return string
*/
Expand All @@ -79,4 +64,21 @@ 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;
}
}

0 comments on commit cc7a56a

Please sign in to comment.