Skip to content

Commit

Permalink
commit inicial
Browse files Browse the repository at this point in the history
  • Loading branch information
masakik committed May 28, 2019
1 parent 7b86e08 commit 92a935c
Show file tree
Hide file tree
Showing 16 changed files with 13,101 additions and 0 deletions.
18 changes: 18 additions & 0 deletions composer.json
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": {}
}
32 changes: 32 additions & 0 deletions readme.md
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;

```




65 changes: 65 additions & 0 deletions src/Cutter.php
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];
}
}
}
Loading

0 comments on commit 92a935c

Please sign in to comment.