Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Apr 22, 2014
1 parent 58f3d56 commit 65564e1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 20 deletions.
18 changes: 3 additions & 15 deletions src/PHPHumanizer/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,17 @@

namespace PHPHumanizer;

use PHPHumanizer\Number\Ordinal;

class Number
{

public static function ordinalize($number)
{
return $number . self::oridinal($number);
}

public static function oridinal($number)
{
$absNumber = abs((integer)$number);

if (in_array(($absNumber % 100), array(11,12,13))) {
return 'th';
}

switch ($absNumber % 10) {
case 1: return 'st';
case 2: return 'nd';
case 3: return 'rd';
default: return 'th';
}
return (string) new Ordinal($number);
}


}
35 changes: 35 additions & 0 deletions src/PHPHumanizer/Number/Ordinal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace PHPHumanizer\Number;

class Ordinal
{
/**
* @var int|double
*/
private $number;

/**
* @param int|double $number
*/
public function __construct($number)
{
$this->number = $number;
}

public function __toString()
{
$absNumber = abs((integer) $this->number);

if (in_array(($absNumber % 100), array(11,12,13))) {
return 'th';
}

switch ($absNumber % 10) {
case 1: return 'st';
case 2: return 'nd';
case 3: return 'rd';
default: return 'th';
}
}
}
8 changes: 3 additions & 5 deletions src/PHPHumanizer/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

namespace PHPHumanizer;

use PHPHumanizer\String\Humanize;

class String
{
protected static $forbiddenWords = array('id');

public static function humanize($text, $capitalize = true)
{
$humanized = trim(strtolower(preg_replace(array('/([A-Z])/', '/[_\s]+/'), array('_$1', ' '), $text)));
$humanized = trim(str_replace(self::$forbiddenWords, "", $humanized));
return $capitalize ? ucfirst($humanized) : $humanized;
return (string) new Humanize($text, $capitalize);
}
}
42 changes: 42 additions & 0 deletions src/PHPHumanizer/String/Humanize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace PHPHumanizer\String;

class Humanize
{
/**
* @var array
*/
private $forbiddenWords = array('id');

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

/**
* @var boolean
*/
private $capitalize;

/**
* @param $text
* @param bool $capitalize
*/
public function __construct($text, $capitalize = true)
{
$this->text = $text;
$this->capitalize = $capitalize;
}

/**
* @internal param bool $capitalize
* @return string
*/
public function __toString()
{
$humanized = trim(strtolower(preg_replace(array('/([A-Z])/', '/[_\s]+/'), array('_$1', ' '), $this->text)));
$humanized = trim(str_replace($this->forbiddenWords, "", $humanized));
return $this->capitalize ? ucfirst($humanized) : $humanized;
}
}

0 comments on commit 65564e1

Please sign in to comment.