-
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
16 changed files
with
13,101 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,18 @@ | ||
{ | ||
"name": "uspdev/cutter", | ||
"description": "Retorna o código cutter da string fornecida", | ||
"type": "library", | ||
"license": "GPL-V3", | ||
"authors": [ | ||
{ | ||
"name": "Masaki Kawabata Neto", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Uspdev\\": "src" | ||
} | ||
}, | ||
"require": {} | ||
} |
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,32 @@ | ||
# Cutter | ||
|
||
Biblioteca que retorna o código cutter de uma string. | ||
|
||
Para saber o que é e para que serve veja https://pt.wikipedia.org/wiki/Tabela_de_Cutter. | ||
|
||
Esta biblioteca foi inspirada pelo projeto https://github.com/bcunhasa/gerador-cutter que está escrito em python. Especificamente aproveitei boa parte do método recursivo para encontrar o código. | ||
|
||
A tabela utilizada foi retirada do endereço http://203.241.185.12/asd/board/Author/upfile/abcd.htm, visitado em 28/5/2019. Parece ser a mesma tabela publicada em http://conteudo.icmc.usp.br/Portal/Sistemas/Biblioteca/cutter/ e em http://biblioteca.eesc.usp.br/index.php?option=com_content&view=article&id=206&Itemid=375. | ||
|
||
## Utilização | ||
|
||
Adicione esta biblioteca ao seu projeto | ||
|
||
composer require uspdev/cutter | ||
|
||
Exemplo de teste | ||
|
||
```php | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use Uspdev\Cutter; | ||
|
||
echo Cutter::find('Kawabata, Neto').PHP_EOL; | ||
|
||
``` | ||
|
||
|
||
|
||
|
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,65 @@ | ||
<?php | ||
namespace Uspdev; | ||
|
||
class Cutter | ||
{ | ||
public static function find($search) | ||
{ | ||
$search = trim($search); | ||
$search = strtolower(Cutter::removeAccents($search)); | ||
|
||
// esta lista foi compilada a partir do endereço | ||
// http://203.241.185.12/asd/board/Author/upfile/abcd.htm visitado em 28/5/2019 | ||
$list = Cutter::load(__DIR__ . '/cutter.csv'); | ||
|
||
return Cutter::recursiveSearch($search, $list, 0); | ||
} | ||
|
||
protected static function removeAccents($string) | ||
{ | ||
return preg_replace('/[`^~\'"]/', null, iconv('UTF-8', 'ASCII//TRANSLIT', $string)); | ||
} | ||
|
||
public static function load($file) | ||
{ | ||
// $file está no formato 000;xxxx | ||
$csv = file_get_contents($file); | ||
|
||
//vamos remover as linhas que começam com # | ||
$csv = preg_replace('/#.*.\n/', '', $csv); | ||
|
||
$arr = array_map(function ($v) {return str_getcsv($v, ";");}, explode("\n", $csv)); // vamos converter para array | ||
return $arr; | ||
} | ||
|
||
// método interativo de busca do código cutter | ||
protected static function recursiveSearch($search, $list, $i) | ||
{ | ||
$new_list = []; | ||
|
||
foreach ($list as $tuple) { | ||
|
||
$tuple[0] = (int) $tuple[0]; | ||
$tuple[1] = strtolower(trim($tuple[1])); | ||
|
||
if ($i >= strlen($search)) { | ||
return $list[0][0]; | ||
} | ||
|
||
if ($i > strlen($tuple[1])) { | ||
break; | ||
} | ||
|
||
// em alguns momentos $tuple[1][$i] (um caracter) pode não existir, pr isso testamos antes | ||
if (!empty($tuple[1][$i]) && $search[$i] == $tuple[1][$i]) { | ||
array_push($new_list, $tuple); | ||
} | ||
} | ||
|
||
if (!empty($new_list)) { | ||
return Cutter::recursiveSearch($search, $new_list, $i + 1); | ||
} else { | ||
return $list[0][0]; | ||
} | ||
} | ||
} |
Oops, something went wrong.