Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcmm committed Jun 29, 2016
1 parent 3a7648b commit 0bc5985
Show file tree
Hide file tree
Showing 8 changed files with 934 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
24 changes: 24 additions & 0 deletions composer.json
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"
}
}
}
55 changes: 55 additions & 0 deletions src/DUtils/CurrencyUtils.php
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);

}


}
Loading

0 comments on commit 0bc5985

Please sign in to comment.