-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
934 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "danielcmm/dutils", | ||
"type": "library", | ||
"description": "Simple Utility Classes", | ||
"keywords": ["utility","currency","date","environment"], | ||
"homepage": "https://github.com/danielcmm/DUtils", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Daniel Miranda", | ||
"email": "[email protected]", | ||
"homepage": "https://github.com/danielcmm/DUtils", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.6.0" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"DUtils": "src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace DUtils; | ||
|
||
class CurrencyUtils{ | ||
|
||
|
||
public static function prepareToView($value){ | ||
|
||
if ($value != 0 && empty($value)) | ||
return ""; | ||
|
||
return number_format(static::getCurrencyInUSFormat($value), 2, ',', '.'); | ||
} | ||
|
||
/** | ||
* Prepara um valor para ser persistido na base de dados | ||
* @param $value | ||
* @return int|string | ||
*/ | ||
public static function prepareToPersist($value){ | ||
|
||
if (empty($value)) | ||
return 0; | ||
|
||
$value = str_replace(["R$"," "],"",$value); | ||
$valueFormatado = static::getCurrencyInUSFormat($value); | ||
|
||
if (!is_numeric($valueFormatado)){ | ||
return 0; | ||
} | ||
|
||
return number_format($valueFormatado, 2, '.', ''); | ||
|
||
} | ||
|
||
public static function getCurrencyInUSFormat($value){ | ||
|
||
$value = "$value"; | ||
|
||
if (strlen($value) >=3 && ($value[strlen($value)-2] == "." || $value[strlen($value)-2] == ",")) | ||
$value .= "0"; | ||
|
||
$newValue = str_replace(",","",$value); | ||
$newValue = str_replace(".","",$newValue); | ||
|
||
if ($newValue == $value) //Significa que não tinha pontos ou virgulas, entao consideramos como um inteiro | ||
$newValue .= "00"; | ||
|
||
return substr($newValue,0,-2) . '.' . substr($newValue,strlen($newValue)-2); | ||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.