a light-weight PHP package of useful common manipulators/formatters.
composer require hallindavid/manny
if using Laravel, the Manny alias should be autodiscovered and usable easily like this.
use Manny;
Manny::phone("8008008000"); // Returns: 800-800-8000
for other frameworks, you will likely need to do
require_once "vendor/autoload.php"
use Manny;
Manny::phone("8008008000"); // Returns: 800-800-8000
Manny::phone - a Canada/US phone formatter - rebuilt better than before from hallindavid/phonehelper
Definition
/**
* @param string $number
* @param array $options
*
*
* Default Options
*
* $default_options = [
* 'showCountryCode' => false,
* 'showAreaCode' => true,
* 'showExchange' => true,
* 'showLine' => true,
* 'showExtension' => false,
* 'prefix' => false,
* 'country_area_delimiter' => false,
* 'area_exchange_delimiter' => '-',
* 'exchange_line_delimiter' => '-',
* 'line_extension_delimiter'=> ' ext. ',
* ];
* @return string
*/
function phone($number, $options)
Example
Manny::phone("8008008000");
//outputs 800-800-8000
Extending Manny::phone
It's pretty easy to extend the phone class - here is an example
class Brack10 extends Manny\Phone
{
public function __construct($text)
{
parent::__construct($text);
$this->showCountryCode = false;
$this->showAreaCode = true;
$this->showExchange = true;
$this->showLine = true;
$this->showExtension = false;
$this->prefix = false;
$this->country_area_delimiter = '(';
$this->area_exchange_delimiter = ') ';
$this->exchange_line_delimiter = '-';
$this->line_extension_delimiter = ' ext. ';
}
}
$phone = new Brack10("123456789123456");
$phone->format();
//Returns: (234) 567-8912
A mask function for formatting fixed-length data. (great for real-time-masking with livewire/livewire)
Definition
/**
* @param string $target
* @param string $pattern
* @return string
*/
function mask($target, $pattern)
Pattern creation
A
should be a placeholder for an alphabetical character
1
should be a placeholder for a numeric character
all other characters are treated as formatting characters
Example
//US Social Security Number
Manny::mask("987654321", "111-11-1111"); //returns "987-65-4321"
//US Zip-code
Manny::mask("The whitehouse zip code is: 20500", "11111"); //returns "20500"
//Canada Postal Code
Manny::mask("K1M1M4", "A1A 1A1"); //
//outputs 987-65-4321
use yoink to pull specific key-values from an associative array, and (optionally) pass in defaults.
Definition
/**
* @param array $target - should be key-val associative array
* @param array $elements - should be flat array with desired key names from target array
* @param array $defaults (optional) - key-val associative array which will be appended to extracted key-value pairs before returning
* @return array
*/
function yoink($target, $elements, $defaults = null)
Example
$array = ['id' => '17', 'name'=> 'John Doe'];
$elements = ['name', 'role'];
$default_values = ['role'=> 'member'];
Manny::yoink($array, $elements, $default_values); //Returns: ['name'=>'John Doe','role'=>'member'] ;
a preg_replace abstraction easy-to-remember parameters to reduce frequent googling
Definition
/**
* @param string $text - the subject of our stripping
* @param array|null $options - an array with the return types you'd like
*
* keys can include the following types:
* alpha - keep the alphabetical characters (case-insensitive)
* num - keep the digits (0-9)
* comma - keep commas
* colon - keep the : character
* dot - keep periods
* dash - keep dashes/hyphens
* space - keep spaces
* underscore - keep underscores
* pipe - keep pipe characters
* bracket - keep square brackets []
* parenthesis - keep parenthesis ()
* curly - keep curley braces (useful for handlebar syntax ex. {{ thing }}
*
* @return string
*/
function stripper($text, $options = null)
Example
$string = 'With only 5-10 hours of development, Dave built Manny, saving him atleast 10 seconds per day!';
$config = ['num', 'alpha', 'space'];
Manny::stripper($string,$config);
//Returns: 'With only 510 hours of development Dave built Manny saving him atleast 10 seconds per day';
$alt_config = ['num'];
Manny::stripper($string,$alt_config);
//Returns: '51010';
an alias for the Manny::stripper
with the same functionality (since you are really "keeping" all the characters you define in the options, it makes for better code readability)
Example
$string = 'I only want to "keep" the alpha, num, and spaces for this string!';
$options = ['alpha', 'num', 'space']
Manny::keep()
//Returns: 'I only want to keep the alpha num and spaces for this string'
a preg_replace abstraction easy-to-remember parameters to reduce frequent googling
Definition
/**
* @param string $text - the subject of our crumbling
* @param array $crumbs - an array of positive integers
* @param bool $appendExtra - keys can include the following types
*
* @return array
*/
function crumble($string, $crumbs, $appendExtra = false)
Example
Manny::crumble("18008008000888", [1,3,3,4])
//Output: ["1","800","800","8000"];
//with append extra
Manny::crumble("18008008000888", [1,3,3,4],true)
//Output: ["1","800","800","8000", "888"];
This is a quick-use tool for generating percents. It cleans up bad data before processing, and has an opinionated workflow (eg. 0/0 = 100%)
Definition
/**
* @param int|float|string $num - the numerator
* @param int|float|string $denom - the denominator
* @param int $precision - keys can include the following types
*
* @return float
*/
function percent($num, $denom, $precision = 0)
Example
Manny::percent(1,8);
//Output: 12.5;
There are a tonne of tests for the packaged formats - to run them, pull the package then
composer install
composer test
To say thanks, you can share the project on social media or
Please report all issues in the GitHub Issue tracker
Shoot me an email, or DM me on twitter and I am happy to allow other contributors.