Skip to content

Commit 92a935c

Browse files
committed
commit inicial
1 parent 7b86e08 commit 92a935c

16 files changed

Lines changed: 13101 additions & 0 deletions

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "uspdev/cutter",
3+
"description": "Retorna o código cutter da string fornecida",
4+
"type": "library",
5+
"license": "GPL-V3",
6+
"authors": [
7+
{
8+
"name": "Masaki Kawabata Neto",
9+
"email": "kawabata@usp.br"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"Uspdev\\": "src"
15+
}
16+
},
17+
"require": {}
18+
}

readme.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Cutter
2+
3+
Biblioteca que retorna o código cutter de uma string.
4+
5+
Para saber o que é e para que serve veja https://pt.wikipedia.org/wiki/Tabela_de_Cutter.
6+
7+
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.
8+
9+
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.
10+
11+
## Utilização
12+
13+
Adicione esta biblioteca ao seu projeto
14+
15+
composer require uspdev/cutter
16+
17+
Exemplo de teste
18+
19+
```php
20+
<?php
21+
22+
require 'vendor/autoload.php';
23+
24+
use Uspdev\Cutter;
25+
26+
echo Cutter::find('Kawabata, Neto').PHP_EOL;
27+
28+
```
29+
30+
31+
32+

src/Cutter.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
namespace Uspdev;
3+
4+
class Cutter
5+
{
6+
public static function find($search)
7+
{
8+
$search = trim($search);
9+
$search = strtolower(Cutter::removeAccents($search));
10+
11+
// esta lista foi compilada a partir do endereço
12+
// http://203.241.185.12/asd/board/Author/upfile/abcd.htm visitado em 28/5/2019
13+
$list = Cutter::load(__DIR__ . '/cutter.csv');
14+
15+
return Cutter::recursiveSearch($search, $list, 0);
16+
}
17+
18+
protected static function removeAccents($string)
19+
{
20+
return preg_replace('/[`^~\'"]/', null, iconv('UTF-8', 'ASCII//TRANSLIT', $string));
21+
}
22+
23+
public static function load($file)
24+
{
25+
// $file está no formato 000;xxxx
26+
$csv = file_get_contents($file);
27+
28+
//vamos remover as linhas que começam com #
29+
$csv = preg_replace('/#.*.\n/', '', $csv);
30+
31+
$arr = array_map(function ($v) {return str_getcsv($v, ";");}, explode("\n", $csv)); // vamos converter para array
32+
return $arr;
33+
}
34+
35+
// método interativo de busca do código cutter
36+
protected static function recursiveSearch($search, $list, $i)
37+
{
38+
$new_list = [];
39+
40+
foreach ($list as $tuple) {
41+
42+
$tuple[0] = (int) $tuple[0];
43+
$tuple[1] = strtolower(trim($tuple[1]));
44+
45+
if ($i >= strlen($search)) {
46+
return $list[0][0];
47+
}
48+
49+
if ($i > strlen($tuple[1])) {
50+
break;
51+
}
52+
53+
// em alguns momentos $tuple[1][$i] (um caracter) pode não existir, pr isso testamos antes
54+
if (!empty($tuple[1][$i]) && $search[$i] == $tuple[1][$i]) {
55+
array_push($new_list, $tuple);
56+
}
57+
}
58+
59+
if (!empty($new_list)) {
60+
return Cutter::recursiveSearch($search, $new_list, $i + 1);
61+
} else {
62+
return $list[0][0];
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)