1. Go to this page and download the library: Download collab-corp/formatter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
collab-corp / formatter example snippets
use CollabCorp\Formatter\Support\ValueFormatter;
$formatter = new ValueFormatter(" uncle bob ", ['trim', 'ucwords']);
$formatter->apply()->get(); // returns "Uncle Bob"
function suffix_string($value, $suffix)
{
return $value.$suffix;
}
$formatter = new ValueFormatter(" Foo ", ['trim', 'suffix_string:Bar']);
$formatter->apply()->get(); // returns "FooBar"
// trim & replace all non numerics with "#"
$formatter = new ValueFormatter(" ABC123 ", [
'trim',
'preg_replace:/[^0-9]/,#,:value:'
]);
$formatter->apply()->get(); // returns "###123"
$formatter = new ValueFormatter(" uncle bob ", ['trim', 'ucwords']);
$formatter->allowedCallables(["trim"]); //only trim is allowed
// throws InvalidArgumentException:
// "Encountered non whitelisted function or non callable [ucwords]"
$formatter->apply()->get();
$formatter = new ValueFormatter("the value", [
function($value){
//do something to the value
return $value;
},
...
]);
use CollabCorp\Formatter\Support\Contracts\Formattable;
use Closure;
class FormatValue implements Formattable
{
/**
* Format the value.
*
* @param mixed $value
* @param Closure $exit
* @return mixed
*/
public function format($value, Closure $exit)
{
// quit formatting value(s)
if($someCondition){
$exit();
}
// or change the $value
$value = "Changed"
return $value;
}
}
$formatter = new ValueFormatter("The value", [
"trim"
new FormatValue
// or using class constant:
FormatValue::class // formatter will new up the class.
]);
$formatter = new ValueFormatter(null, [
"?", //tells the class not to process callables if value is blank
'to_carbon',
'.addDays:1',
'.format:m/d/Y'
]);
$formatter->apply()->get(); // returns original null value
use CollabCorp\Formatter\Support\Concerns\FormatsData;
class SomeClass
{
use FormatsData;
public function someFunction()
{
//returns ValueFormatter
$this->formatValue("...", [...])->apply()->get();
//returns DataFormatter
$this->formatData([...], [...])->apply()->get();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.