-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58f3d56
commit 65564e1
Showing
4 changed files
with
83 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |