PHP code example of t-labs-co / formatter

1. Go to this page and download the library: Download t-labs-co/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/ */

    

t-labs-co / formatter example snippets


use TLabsCo\Formatter\Formatter;

$formatterRules = [
    'title' => 'trim|replace:Local Composer Dependencies,[Local Composer Dependencies]|replace:[Local Composer Dependencies],[Composer Dependencies]|limit:150',
    'publish_date' => 'date_format:Y-m-d',
];

$data = [
    'title' => '  How to resolve missing Local Composer Dependencies on CentOS 8?  ',
    'publish_date' => '2024/05/02 13:00',
];

$formatted = Formatter::make($data, $formatterRules)->format()->formatted();

/*
Output:
$formatted = [
    'title' => 'How to resolve missing [Composer Dependencies] on CentOS 8?',
    'publish_date' => '2024-05-02'
];
*/

use TLabsCo\Formatter\Facades\Formatter;

$result = Formatter::make($data, $rules)->format()->formatted();

use TLabsCo\Formatter\Formatter;

$result = Formatter::singleFormat('  hello world  ', 'trim|upper');
// 'HELLO WORLD'

use TLabsCo\Formatter\Rules\FormatterRule;

class Max100FormatRule extends FormatterRule
{
    public function format(string $attribute, mixed $value): mixed
    {
        if (strlen((string) $value) > 100) {
            return substr((string) $value, 0, 100);
        }

        return $value;
    }
}

// Using with custom rule
$formatterRules = [
    'title' => 'trim|limit:150',
    'short_description' => ['trim', new Max100FormatRule()],
];