Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added truncate operation #4

Merged
merged 3 commits into from
Apr 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}