PHP code example of enimiste / l5-math

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

    

enimiste / l5-math example snippets


 $calculator = calculator();
 $calculator->add(.. , ...);
 ....

    Enimiste\L5Math\Providers\L5MathServiceProvider::class;

    'Calculator' => 'Enimiste\L5Math\Facades\CalculatorFacade::class;

  
     public function set[Yourattribute]Attribute($value){
          $this->attribute['price'] = as_float_number($value);//__toString() function of VONumber is used to passe data to db
     }
     
     public function get[Yourattribute]Attribute($value){
          return as_float_number($value);
     }
  


interface Calculator {

	/**
	 * Multiplication
	 *
	 * @param Number $l
	 * @param Number $r
	 *
	 * @return Number
	 */
	public function mult( Number $l, Number $r );

	/**
	 * Calculate the TTC price from HT and TVA
	 *
	 * @param FloatNumber $ht
	 * @param FloatNumber $tva between 0 and 1
	 *
	 * @return FloatNumber
	 */
	public function ttc( FloatNumber $ht, FloatNumber $tva );

	/**
	 * Add two Numbers
	 *
	 * @param Number $l
	 * @param Number $r
	 *
	 * @return Number
	 */
	public function add( Number $l, Number $r );

	/**
	 * @param IntegerNumber $quantite
	 * @param FloatNumber   $prixUnitaireHt
	 * @param FloatNumber   $tva
	 *
	 * @return PriceResultDto
	 */
	public function price( IntegerNumber $quantite, FloatNumber $prixUnitaireHt, FloatNumber $tva );

	/**
	 * Build TVA as value betwenn 0 and 1 from a value from 0 to 100
	 *
	 * @param FloatNumber $tva
	 *
	 * @return FloatNumber
	 */
	public function tva( FloatNumber $tva );

	/**
	 * Sub two Numbers
	 * $l - $r
	 *
	 * @param Number $l
	 * @param Number $r
	 *
	 * @return Number
	 */
	public function sub( Number $l, Number $r );
}



   //products table
   $table->increments('id');
   $table->string('title')->unique();
   $table->decimal('price', 8, 2);//000000.00
   $table->decimal('tva', 5, 2)->default(20.0);//between 0.00 and 100.00
   
   //orders table
   $table->increments('id');
   $table->unsignedInteger('amount');//count of product ordered
   $table->decimal('total_ht', 8, 2);// amount * price
   $table->decimal('total_ttc', 8, 2);
   $table->unsignedInteger('product_id');



class Product extends Model {

   protected $fillable = ['title', 'price', 'tva'];
   
   public function setPriceAttribute($value){
        $this->attribute['price'] = as_float_number($value);
   }
   
   public function getPriceAttribute($value){
        return as_float_number($value);
   }
   
   public function setTvaAttribute($value){
       $this->attribute['tva'] = as_float_number($value);
   }
   
   public function getTvaAttribute($value){
       return as_float_number($value);
   }

}


class Order extends Model {

   protected $fillable = ['amount', 'total_ht', 'total_ttc', 'product_id'];
   
   public function setTotalHtAttribute($value){
        $this->attribute['total_ht'] = as_float_number($value);
   }
   
   public function getTotalHtAttribute($value){
        return as_float_number($value);
   }
   
   public function setTotalTtcAttribute($value){
        $this->attribute['total_ttc'] = as_float_number($value);
   }
      
   public function getTotalTtcAttribute($value){
       return as_float_number($value);
   }

}


 $p = Product::create(
        [
            'price' =>  100.76,
            'tva'   =>  20.0
        ]
 );
 
 $calc = calculator();
 
 $o1 = Order::create(
        [
            'product_id'    =>  $p->id,
            'amount'        =>  33,
            'total_ht'      =>  $ht = $calc->mult($p->price, new Enimiste\Math\VO\IntegerNumber(33)),
            'total_ttc'     =>  $calc->ttc($ht, $calc->tva($p->tva))
        ]
 );

//NB : $calc->tva convert a tva as % to float value between 0 and 1
//another way

 $price = $calc->price(new Enimiste\Math\VO\IntegerNumber(78), $p->price, $calc->tva($p->tva));
 $o2 = Order::create(
        [
            'product_id'    =>  $p->id,
            'amount'        =>  $price->quantite,
            'total_ht'      =>  $price->ht,
            'total_ttc'     =>  $price->ttc
        ]
 );


 $products = Product::all();
 
 foreach($products as $p){
    echo 'Price : $' . $p->price->__toString() . PHP_EOL;
    echo 'Tva : ' . $p->tva->__toString() . '%' . PHP_EOL;
 }



 $orders = Order::all();
 
 foreach($orders as $o){
    echo 'Amount : $' . $o->amount . PHP_EOL;
    echo 'Total Ht : $' . $o->total_ht->__toString() . PHP_EOL;
    echo 'Total Ttc : ' . $o->total_ttc->__toString() . '%' . PHP_EOL;
    echo PHP_EOL;
 }