Skip to content

Commit

Permalink
Feat: update format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Goldbach07 committed Oct 25, 2021
1 parent bfe124a commit 3c2fcfa
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 31 deletions.
22 changes: 22 additions & 0 deletions Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

class Constants
{
/**
* 🇧🇷 Configurações de máscaras
* 🇺🇸 Mask Settings
* 🇮🇹 Impostazioni delle maschere
*/
const CEP = '#####-###';
const CPF = '###.###.###-##';

/**
* 🇧🇷 Tamanho exato de cada tipo de informação
* 🇺🇸 Exact size of each type of information
* 🇮🇹 Dimensione esatta di ogni tipo di informazione
*/
const LENGTH = [
'CEP' => 8,
'CPF' => 11
];
}
61 changes: 32 additions & 29 deletions Mask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,56 @@

namespace GoldbachAlgorithms;

use GoldbachAlgorithms\Mask\Constants;

class Mask
{
const CEP = '#####-###';

public function transform(
string $mask,
string $value
){
) {
$value = $this->clear($value);
$value = $this->rewrite($mask, $value);
$value = $this->doIt($mask, $value);

return $value;
}

$maskared = '';
public function doIt(
string $mask,
string $value
): string {
$masked = '';

$k = 0;

for($i = 0; $i <= strlen($mask)-1; $i++) {
if($mask[$i] == '#') {
if(isset($value[$k])){
$maskared .= $value[$k++];
}
}
else
{
if(isset($mask[$i]))
{
$maskared .= $mask[$i];
for ($i = 0; $i <= strlen($mask)-1; $i++) {
if ($mask[$i] == '#') {
if (isset($value[$k])) {
$masked .= $value[$k++];
}
} else {
if (isset($mask[$i])) {
$masked .= $mask[$i];
}
}
}

return $maskared;


return $masked;
}

public function rewrite(
$mask,
$value
)
{
if($mask == self::CEP){
$value = str_replace('-','',$value);
return str_pad($value, 8, "0", STR_PAD_LEFT);
}
string $mask,
string $value
):string {
return str_pad($value, self::LENGTH[$mask], "0", STR_PAD_LEFT);
}

public function clear(
string $unmask
){
return $unmask;
):string {
$unmasked = preg_replace('/[\@\.\;\-\," "]+/', '', $unmask);

return $unmasked;
}
}
}
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
# Mask
Mascarador de valores PHP
# GoldMask

Goldbach Algorithms Mask (fondly nicknamed GoldMask) is a PHP library developed for Symfony for apply a mask to strings.

## Installation

Use the composer to install:

```bash
composer require goldbach-algorithms/aig
```

## Usage

```php

$mask = new Mask();

# returns '11025-140'
$mask->transform(Mask::CEP, '11025140');

```

## License
[MIT](https://choosealicense.com/licenses/mit/)

0 comments on commit 3c2fcfa

Please sign in to comment.