Skip to content

Commit

Permalink
Merge pull request #4 from norzechowicz/truncate
Browse files Browse the repository at this point in the history
Added truncate operation
  • Loading branch information
defrag committed Apr 26, 2014
2 parents 7117755 + b973520 commit 6181797
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ echo String::humanize('user_id'); // "User"
echo String::humanize('field_name', false); // "field name"
```

**Truncate**

Truncate string to word closest to a certain length

```php
use Coduo\PHPHumanizer\String;

$text = 'Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc.';

echo String::truncate($text, 8); // "Lorem ipsum"
echo String::truncate($text, 8, '...'); // "Lorem ipsum..."
echo String::truncate($text, 2); // "Lorem"
echo String::truncate($text, strlen($text)); // "Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc."

```

## Number

**Ordinalize**
Expand Down
12 changes: 12 additions & 0 deletions spec/Coduo/PHPHumanizer/StringSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,16 @@ function it_humanize_string_excluding_forbidden_words()
{
$this->humanize('news_id')->shouldReturn('News');
}

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

$this->truncate($text, 2)->shouldReturn("Lorem");
$this->truncate($text, 10, '...')->shouldReturn("Lorem ipsum...");
$this->truncate($text, 30)->shouldReturn("Lorem ipsum dolorem si amet, lorem");
$this->truncate($text, 0)->shouldReturn("Lorem");
$this->truncate($text, 0, '...')->shouldReturn("Lorem...");
$this->truncate($text, -2)->shouldReturn($text);
}
}
6 changes: 6 additions & 0 deletions src/Coduo/PHPHumanizer/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
namespace Coduo\PHPHumanizer;

use Coduo\PHPHumanizer\String\Humanize;
use Coduo\PHPHumanizer\String\Truncate;

class String
{
public static function humanize($text, $capitalize = true)
{
return (string) new Humanize($text, $capitalize);
}

public static function truncate($text, $charactersCount, $append = '')
{
return (string) new Truncate($text, $charactersCount, $append);
}
}
47 changes: 47 additions & 0 deletions src/Coduo/PHPHumanizer/String/Truncate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Coduo\PHPHumanizer\String;

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

/**
* @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 || strlen($this->text) <= $this->charactersCount) {
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;
}
}

0 comments on commit 6181797

Please sign in to comment.