Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Agaletskiy committed Jul 21, 2019
0 parents commit e674f50
Show file tree
Hide file tree
Showing 27 changed files with 1,395 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
/.build/
composer.lock
.phpunit.result.cache
57 changes: 57 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
return Config::create()
->setUsingCache(true)
->setFinder(
Finder::create()
->exclude([
'vendor',
])
->in(__DIR__)
)
->setRules([
'@PSR2' => true,
'@Symfony' => true,
'class_definition' => [
'multiLineExtendsEachSingleLine' => true,
],
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
'align_double_arrow' => false,
'align_equals' => false,
],
'concat_space' => [
'spacing' => 'one',
],
'general_phpdoc_annotation_remove' => [
'author',
'package',
],
'no_multiline_whitespace_before_semicolons' => true,
'no_null_property_initialization' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_class_elements' => [
'order' => [
'use_trait',
'constant_public',
'constant_protected',
'constant_private',
'property_public',
'property_protected',
'property_private',
'construct'
]
],
'ordered_imports' => [
'sortAlgorithm' => 'alpha'
],
'phpdoc_order' => true,
'phpdoc_types_order' => [
'null_adjustment' => 'always_last'
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_annotation_without_dot' => true,
'yoda_style' => false
]);
16 changes: 16 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
checks:
php:
code_rating: true
duplication: true

build:
tests:
override:
- command: vendor/bin/phpunit --coverage-clover=.build/clover.xml
coverage:
file: .build/clover.xml
format: clover

filter:
excluded_paths:
- tests/*
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
language: php

php:
- 7.1
- 7.2
- 7.3
- 7.4snapshot
# php8 is not supported yet
#- nightly

env:
matrix:
- DEPENDENCIES="high"
- DEPENDENCIES="low"
global:
- DEFAULT_COMPOSER_FLAGS="--prefer-dist --no-interaction --no-ansi --no-progress --no-suggest"

matrix:
fast_finish: true
allow_failures:
- php: 7.4snapshot

install:
- if [[ "$DEPENDENCIES" = 'high' ]]; then travis_retry composer update $DEFAULT_COMPOSER_FLAGS; fi
- if [[ "$DEPENDENCIES" = 'low' ]]; then travis_retry composer update $DEFAULT_COMPOSER_FLAGS --prefer-lowest; fi

cache:
directories:
- $HOME/.composer/cache
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Lamoda

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Lamoda GS1 Barcode parser and validator
=======================================

[![Build Status](https://travis-ci.org/lamoda/gs1-barcode-parser.svg?branch=master)](https://travis-ci.org/lamoda/gs1-barcode-parser)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/lamoda/gs1-barcode-parser/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/lamoda/gs1-barcode-parser/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/lamoda/gs1-barcode-parser/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/lamoda/gs1-barcode-parser/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/lamoda/gs1-barcode-parser/badges/build.png?b=master)](https://scrutinizer-ci.com/g/lamoda/gs1-barcode-parser/build-status/master)


## Installation

### Composer

```sh
composer require lamoda/gs1-barcode-parser
```

## Description

This library provides parsing of GS1 Barcodes according to
[GS1 General specification](https://www.gs1.org/sites/default/files/docs/barcodes/GS1_General_Specifications.pdf)
and [GS1 DataMatrix Guideline](https://www.gs1.org/docs/barcodes/GS1_DataMatrix_Guideline.pdf).

Library also provides general purpose validator for barcode's content.

## Usage

### Parser
```php
<?php

$config = new \Lamoda\GS1Parser\Parser\ParserConfig();
$parser = new \Lamoda\GS1Parser\Parser\Parser($config);

$value = ']d201034531200000111719112510ABCD1234';

$barcode = $parser->parse($value);

// $barcode is an object of Barcode class
```

### Validator
```php
<?php

$parserConfig = new \Lamoda\GS1Parser\Parser\ParserConfig();
$parser = new \Lamoda\GS1Parser\Parser\Parser($parserConfig);

$validatorConfig = new \Lamoda\GS1Parser\Validator\ValidatorConfig();
$validator = new \Lamoda\GS1Parser\Validator\Validator($parser, $validatorConfig);

$value = ']d201034531200000111719112510ABCD1234';

$resolution = $validator->validate($value);

if ($resolution->isValid()) {
// ...
} else {
var_dump($resolution->getErrors());
}

```
33 changes: 33 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "lamoda/gs1-barcode-parser",
"description": "GS1 Barcode parser and validator compatible with official GS1 documentation",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Lamoda developers",
"homepage": "https://tech.lamoda.ru/"
}
],
"minimum-stability": "stable",
"require": {
"php": "^7.1"
},
"autoload": {
"psr-4": {
"Lamoda\\GS1Parser\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Lamoda\\GS1Parser\\Tests\\": "tests/"
}
},
"config": {
"sort-packages": true
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.15",
"phpunit/phpunit": "^7.5"
}
}
27 changes: 27 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
backupGlobals="false"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="SHELL_VERBOSITY" value="-1" />
</php>
<testsuites>
<testsuite name="Lamoda GS1 Parser Unit Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<directory>src/Exception</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
86 changes: 86 additions & 0 deletions src/Barcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Lamoda\GS1Parser;

final class Barcode
{
public const TYPE_GS1_DATAMATRIX = 'gs1_datamatrix';
public const TYPE_GS1_QRCODE = 'gs1_qrcode';
public const TYPE_EAN = 'ean';
public const TYPE_GS1_128 = 'gs1_128';
public const TYPE_UNKNOWN = 'unknown';

/**
* @var string
*/
private $content;
/**
* @var string
*/
private $type;
/**
* @var array
*/
private $ais;
/**
* @var array
*/
private $buffer;
/**
* @var string | null
*/
private $fnc1Prefix;

public function __construct(string $content, string $type, array $ais, array $buffer, string $fnc1Prefix)
{
$this->content = $content;
$this->type = $type;
$this->ais = $ais;
$this->buffer = $buffer;
$this->fnc1Prefix = $fnc1Prefix;
}


public function type(): string
{
return $this->type;
}

public function ais(): array
{
return $this->ais;
}

public function buffer(): array
{
return $this->buffer;
}

public function hasAI(string $code): bool
{
return array_key_exists($code, $this->ais);
}

public function ai(string $code): string
{
return $this->ais[$code] ?? '';
}

public function raw(): string
{
return $this->content;
}

public function fnc1Prefix(): string
{
return $this->fnc1Prefix;
}

public function normalized(): string
{
$prefixLength = strlen($this->fnc1Prefix);
return substr($this->content, $prefixLength);
}
}
16 changes: 16 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Lamoda\GS1Parser;

final class Constants
{
public const GROUP_SEPARATOR_SYMBOL = "\u{001d}";
public const FNC1_SYMBOL = "\u{00e8}";

public const FNC1_GS1_DATAMATRIX_SEQUENCE = ']d2';
public const FNC1_GS1_QRCODE_SEQUENCE = ']Q3';
public const FNC1_GS1_EAN_SEQUENCE = ']e0';
public const FNC1_GS1_128_SEQUENCE = ']C1';
}
12 changes: 12 additions & 0 deletions src/Exception/GS1ParserExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Lamoda\GS1Parser\Exception;

use Throwable;

interface GS1ParserExceptionInterface extends Throwable
{

}
Loading

0 comments on commit e674f50

Please sign in to comment.