Skip to content

Commit

Permalink
update the amount to be always 2 digit after comma , money format fun…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
ali3bdalla committed Dec 12, 2019
1 parent 7ee2cad commit 00f35e6
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 152 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/Tafqeet.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

303 changes: 153 additions & 150 deletions src/core/Tafqeet.php
Original file line number Diff line number Diff line change
@@ -1,151 +1,154 @@
<?php
namespace AliAbdalla\Tafqeet\Core;
use AliAbdalla\Tafqeet\Helper\Calculators;
use AliAbdalla\Tafqeet\Helper\Handler;
use AliAbdalla\Tafqeet\Helper\Validation;
use AliAbdalla\Tafqeet\Helper\App;
class Tafqeet
{
use Calculators,Handler,Validation,App;

public $config = [
'connection_tool' => ' و',
'default_currency' => 'sar',
'starter'=>'فقط',
'end'=>'لاغير',
'currencies' => [
'sar' => [
'main1'=>'ريال',
'main2'=>'ريالاً',
'single'=>'هللة',
'multi'=>'هللات'
],

'sdg' => [
'main1'=>'قرش',
'main2'=>'قرشاً',
'single'=>'قرش',
'multi'=>'قروش'
],

'usd' => [
'main1'=>'دولار',
'main2'=>'دولاراً',
'single'=>'سنت',
'multi'=>'سنت'
],


],

];
/*
* parsed number
* */
private $parsed_number;

/*
* array of numbers after split process
* */
private $parsed_number_array = [];


/*
* all number array
* all array count
* */
private $all_numbers_len;
private $all_numbers_array;



/*
* before comma number array
* before comma array count
* */
private $before_comma_len;
private $before_comma_array;



/*
* after comma number array
* after comma array count
* */
private $after_comma_len;
private $after_comma_array;
public $after_comma_sum;




/*
* result before and after comma
*
* */
private $result_before_comma;
private $result_after_comma;






private $is_main1_currency = true;



public static function arablic($amount = 0,$currency = 'sar')
{

return (new self)->setAmount($amount)->initValidation()->prepare()->run()->result($currency);
}


public function run()
{
$this->result_before_comma = $this->runBeforeComma();
$this->result_after_comma = $this->runAfterComma();
return $this;
}

public function result($currency = 'sar')
{
$result = $this->config['starter'] . ' ';

if($this->is_main1_currency){
$result.= $this->result_before_comma . ' ' . $this->config['currencies'][$currency]['main1'];

}else
{
$result.= $this->result_before_comma . ' ' . $this->config['currencies'][$currency]['main2'];

}
if($this->after_comma_len>=1)
{
if(in_array($this->after_comma_sum,[
3,4,5,6,7,8,9,10
]))
{
$result.=$this->config['connection_tool']. $this->result_after_comma . ' ' .
$this->config['currencies'][$currency]['multi'];
}else
{
$result.=$this->config['connection_tool']. $this->result_after_comma . ' ' .
$this->config['currencies'][$currency]['single'];
}

}

$result.= ' ' . $this->config['end'];

return str_replace(' ',' ',$result);
}

public function prepare()
{
$this->split_parsed_number_to_two_number_depend_on_comma()->split_numbers_before_comma_to_array()->split_numbers_after_comma_to_array();
return $this;
}

}

namespace AliAbdalla\Tafqeet\Core;

use AliAbdalla\Tafqeet\Helper\Calculators;
use AliAbdalla\Tafqeet\Helper\Handler;
use AliAbdalla\Tafqeet\Helper\Validation;
use AliAbdalla\Tafqeet\Helper\App;

class Tafqeet
{
use Calculators,Handler,Validation,App;

public $config = [
'connection_tool' => ' و',
'default_currency' => 'sar',
'starter' => 'فقط',
'end' => 'لاغير',
'currencies' => [
'sar' => [
'main1' => 'ريال',
'main2' => 'ريالاً',
'single' => 'هللة',
'multi' => 'هللات'
],

'sdg' => [
'main1' => 'قرش',
'main2' => 'قرشاً',
'single' => 'قرش',
'multi' => 'قروش'
],

'usd' => [
'main1' => 'دولار',
'main2' => 'دولاراً',
'single' => 'سنت',
'multi' => 'سنت'
],


],

];
/*
* parsed number
* */
public $after_comma_sum;

/*
* array of numbers after split process
* */
private $parsed_number;

/*
* all number array
* all array count
* */
private $parsed_number_array = [];
private $all_numbers_len;

/*
* before comma number array
* before comma array count
* */
private $all_numbers_array;
private $before_comma_len;

/*
* after comma number array
* after comma array count
* */
private $before_comma_array;
private $after_comma_len;
private $after_comma_array;

/*
* result before and after comma
*
* */
private $result_before_comma;
private $result_after_comma;

private $is_main1_currency = true;

/**
* @param int $amount
* @param string $currency
*
* @return mixed
*/
public static function arablic($amount = 0,$currency = 'sar')
{

$amount = money_format("%i",$amount); // update the amount to be always 2 digit after comma

//
return (new self)->setAmount($amount)->initValidation()->prepare()->run()->result($currency);
}

/**
* @param string $currency
*
* @return mixed
*/
public function result($currency = 'sar')
{
$result = $this->config['starter'].' ';

if ($this->is_main1_currency){
$result .= $this->result_before_comma.' '.$this->config['currencies'][$currency]['main1'];

}else{
$result .= $this->result_before_comma.' '.$this->config['currencies'][$currency]['main2'];

}
if ($this->after_comma_len >= 1){
if (in_array($this->after_comma_sum,[
3,4,5,6,7,8,9,10
])){
$result .= $this->config['connection_tool'].$this->result_after_comma.' '.
$this->config['currencies'][$currency]['multi'];
}else{
$result .= $this->config['connection_tool'].$this->result_after_comma.' '.
$this->config['currencies'][$currency]['single'];
}

}

$result .= ' '.$this->config['end'];

return str_replace(' ',' ',$result);
}

/**
* @return $this
*/
public function run()
{
$this->result_before_comma = $this->runBeforeComma();
$this->result_after_comma = $this->runAfterComma();
return $this;
}

/**
* @return $this
*/
public function prepare()
{
$this->split_parsed_number_to_two_number_depend_on_comma()->split_numbers_before_comma_to_array()->split_numbers_after_comma_to_array();
return $this;
}

}
3 changes: 3 additions & 0 deletions src/helper/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function runBeforeComma()

public function runAfterComma()
{
//
// var_dump($this->after_comma_len);
// exit();
$class = $this->detectClass($this->after_comma_len);
$methodName = 'Class' . $class;
if(method_exists($this,$methodName))
Expand Down
4 changes: 3 additions & 1 deletion src/helper/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ trait Handler

public function setAmount($number = 0)
{

$this->parsed_number = $number . '';
return $this;
}

private function split_parsed_number_to_two_number_depend_on_comma()
{
$arr = explode('.',$this->parsed_number);

$this->parsed_number_array = $arr;
$this->all_numbers_len = count($this->parsed_number_array);
return $this;
Expand All @@ -36,6 +37,7 @@ private function split_numbers_after_comma_to_array()
if($this->all_numbers_len>=2)
{
$arr = str_split($this->parsed_number_array[1]);

if(count($arr)>=3)
{
$this->after_comma_array = [$arr[0],$arr[1]];
Expand Down
Loading

0 comments on commit 00f35e6

Please sign in to comment.