Last active
March 9, 2017 10:27
-
-
Save anthonybudd/884eb003fa5596c1854e2539635fe407 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
Class City extends WP_Model{ | |
public $postType = 'city'; | |
public $attributes = [ | |
'population', | |
]; | |
public $filter = [ | |
'population' | |
]; | |
public function _filterPopulation($value){ | |
$suffixes = ['', 'K', 'M', 'G', 'T']; | |
$suffixIndex = 0; | |
while(abs($value) >= 1000 && $suffixIndex < sizeof($suffixes)){ | |
$suffixIndex++; | |
$value /= 1000; | |
} | |
return ( $value > 0 | |
? floor($value * 1000) / 1000 | |
: ceil($value * 1000) / 1000 | |
) . $suffixes[$suffixIndex]; | |
} | |
} | |
$london = City::insert([ | |
'title' => 'London', | |
'population' => '8674000' | |
]); | |
echo $london->population; // 8.674M | |
$reykjavik = City::insert([ | |
'title' => 'reykjavik', | |
'population' => '119200' | |
]); | |
echo $reykjavik->population; // 119.2K |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment