PHP code example of axm / fluent

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

    

axm / fluent example snippets


$result = __(new MyClass)
    ->method1()
    ->method2()
    ->method3()
    ->get();

$result = __(User::class)
    ->if(fn ($user) => $user->isAdmin())
    ->setProperty('type', 'admin')
    ->return('You are an admin.')

    ->elseif(fn ($user) => $user->isUser())
    ->setProperty('type', 'user')
    ->return('You are a user.')

    ->else()
    ->setProperty('type', 'unknown')
    ->return('You are not logged in.')
    ->get();

$result = __(new MyClass)  //new instance
    ->method1()
    ->method2()
    ->new('SomeClass')    //resets the previous intent with a new `FluentInterface` class to continue chaining.
    ->method1()
    ->method2()
    ->method3()
    ->new('OtherClass')    //resets the previous intent with a new `FluentInterface` class to continue chaining.
    ->method1()
    ->method2()
    ->method3()

    ->get('method1');   //gets the value in this case of the return of method 1 of the last instance

$result = __(new MyClass)
    ->boot()
    ->getErrors()
    ->throwIf(fn ($user) => $fi->get('getError') > 0, , 'An error has occurred while initializing.')

$result = __(new MyClass)
    ->increment(10)
    ->duplicate()
    ->dd();

$result = __(new MyClass)
    ->increment(10)
    ->duplicate()
    ->dump('duplicate');       //will return the value of the duplicate method call.

$result = __(new MyClass)
    ->increment(10)
    ->duplicate()
    ->echo();

$result = __(new MyClass)
    ->addCustomMethod('call', function ($obj) {
    // Define your own logic here
    })
    ->addCustomMethod('getName', function ($obj) {
    // Define your own logic here
    })
    ->call()
    ->getName()
    ->all();

$collection = [
    ['name' => 'John Doe',  'age' => 30],
    ['name' => 'Jane Doe',  'age' => 25],
    ['name' => 'John Smith','age' => 40],
];

$result = __($collection)
    ->filter(function ($item) {
        return $item['age'] > 25;
    })
    ->sort(function ($a, $b) {
        return $a['name'] <=> $b['name'];
    });

$result->all();

class MiClase
{
    public $value = 0;

    public function increment($cantidad)
    {
        return $this->value += $cantidad;
    }

    public function duplicate()
    {
        return $this->value *= 2;
    }

    public function getValue()
    {
        return $this->value;
    }
}

$res = __(MiClase::class)
    ->increment(5)
    ->duplicate()
    ->increment(5)

    ->if(fn ($fi) => $fi->value > 20)
    ->increment(5)

    ->elseif(fn ($fi) => $fi->value < 15)
    ->increment(10)

    ->else()
    ->increment(10)

    ->getValue()->dd('getValue');

__($request->input())   //input array

    ->if(fn ($item) => $item['name'] === '')
    ->throwIf(true, 'The name field is )     // Create a new instance of the class 'Auth'.
    ->if(fn ($user) => $user->hasPermission('admin'))
    ->return(['Admin Dashboard', 'User Management','Role Management',])

    ->elseif(fn ($user) => $user->hasPermission('user'))
    ->return(['My Profile','My Orders','My Account',])

    ->else()
    ->return(['Login','Register',])
    ->get('return');

// Create a custom report using FluentInterface.
__(ReportBuilder::class)
    ->setHeader('Informe de Ventas')
    ->setSubtitle('Resultados mensuales')
    ->setChart('Ventas por mes', 'bar')
    ->addData('Enero', 1000)
    ->addData('Febrero', 1200)
    ->addData('Marzo', 800)
    ->setFooter('© 2023 Mi Empresa')
    ->setFormat('PDF')
    ->generateReport();

 __(FormBuilder::class)
    ->setTitle('Contact Form')
    ->addField('Name', 'text')
    ->addField('Email', 'email')
    ->addField('Message', 'textarea')
    ->addButton('Submit', 'submit')
    ->setAction('/submit-form')
    ->setMethod('POST')
    ->generateForm();

__(EmailBuilder::class)
    ->setRecipient('[email protected]')
    ->setSubject('Welcome!')
    ->setBody('Hi, [name]. Thank you for joining our website.')
    ->addAttachment('invoice.pdf')
    ->setSender('[email protected]')
    ->send();

$query = __(QueryBuilder::class)
    ->select('name', 'email')
    ->from('users')
    ->where('age', '>', 25)
    ->andWhere('city', '=', 'New York')
    ->orderBy('name', 'ASC')
    ->limit(10)
    ->execute();

$chart = __(ChartBuilder::class)
    ->setType('line')
    ->setTitle('Monthly Sales')
    ->addData('January', [100, 150, 200, 120])
    ->addData('February', [120, 160, 180, 140])
    ->setXAxisLabels(['Week 1', 'Week 2', 'Week 3', 'Week 4'])
    ->setYAxisLabel('Sales (in thousands)')
    ->render();