Last active
December 27, 2018 13:14
-
-
Save samhk222/96d33da6396f66ab000ae6641394a80a to your computer and use it in GitHub Desktop.
Create a composer package
This file contains hidden or 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
{ | |
"name": "samhk222/json-string-parser", | |
"description": "Parseia as informações de uma string, retornando em forma de array, os jsons que foram encontrados nela", | |
"type": "library", | |
"authors": [ | |
{ | |
"name": "Samuel Aiala Ferreira", | |
"email": "[email protected]" | |
} | |
], | |
"minimum-stability": "dev", | |
"require": { | |
"php": ">=5.3.0" | |
}, | |
"autoload": { | |
"psr-4": { | |
"samhk222\\": "src/" | |
} | |
} | |
} |
This file contains hidden or 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
composer init |
This file contains hidden or 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
echo "# Primeira lib enviada ao packagist" >> README.md | |
echo "/vendor" >> .gitignore | |
git init | |
git add . | |
git commit -m "Primeiro commit" | |
git remote add origin https://github.com/samhk222/json-string-parser.git | |
# O comando abaixo é opcional, mas ele salva suas credenciais do git para nao ter que digitar toda hora | |
git config credentials.helper store | |
git push -u origin master | |
# O Packagist ira nos solicitar que criemos uma tag, para nao precisarmos instalar do branch de dev, entao crie ela de uma vez | |
git tag -a 0.0.2 -m "Criando a primeira versão do nosso aplicativo" | |
git push origin --tags |
This file contains hidden or 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
<?PHP | |
require('vendor/autoload.php'); # Todas as paginas que desejar usar a lib, inclua essa linha | |
use samhk222\Parser; # Importe nosso parser | |
$parser = new Parser(); | |
$string_com_json = 'Olá, seja bem vindo ao nosso site, as opções de menu são {"menu": {"header": "SVG Viewer", "items": [{"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View SVG"}, {"id": "ViewSource", "label": "View Source"}, {"id": "SaveAs", "label": "Save As"}, null, {"id": "Help"}, {"id": "About", "label": "About Adobe CVG Viewer..."} ] }} por favor escolha uma para continuarmos, vamos inserir outro json, para termos {"segundo":"parametro"} e porque não um terceiro json {"chave":"valor"} depois dessa string toda, mas esse ultimo aqui tem erro, porque {"chave":sem aspas}não tem aspas'; | |
$string_sem_json = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; | |
echo "\n<pre>"; | |
$parser->parseString($string_com_json); | |
echo sprintf("Tem algum json ? %s\n", $parser->hasJSON ? "Sim" : "Não"); | |
echo sprintf("Tem erro em algum json ? %s\n", $parser->hasFaltyJSON ? "Sim" : "Não"); | |
if ($parser->hasJSON) { | |
echo sprintf("JSONs válidos encontrados (Total: %s) \n%s\n", count($parser->matches), print_r($parser->matches,true)); | |
} | |
echo "\n</pre>"; |
This file contains hidden or 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
<?PHP | |
# Lembra quando fizemos o autoload dentro do composer.json, para os namespaces samhk222 dentro da pasta src ? | |
# precisamos declará-lo agora, senão não sera feito o autload dessa classe | |
namespace samhk222; | |
class Parser | |
{ | |
public $hasJSON = false; #Indica se a string tem algum json | |
public $hasFaltyJSON = false; # Indica se tem json, mas com erro | |
public $matches = []; # Retorna os matches do json caso encontrados | |
public function parseString($text) | |
{ | |
$pattern = '/\{(?:[^{}]|(?R))*\}/x '; | |
preg_match_all($pattern, $text, $matches); | |
if (count($matches[0])>0){ | |
$this->hasJSON = true; | |
foreach ($matches[0] as $key => $value) { | |
$decoded = json_decode($value); | |
if (json_last_error() !== JSON_ERROR_NONE) { | |
$this->hasFaltyJSON = true; | |
} else { | |
$this->matches[] = $decoded; | |
} | |
} | |
} | |
return ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment