Skip to content

Commit

Permalink
Merge pull request #4 from prevostc/master
Browse files Browse the repository at this point in the history
Breaking change: php 7.2 compatibility
  • Loading branch information
krichprollsch authored Feb 19, 2018
2 parents de35e34 + a2f9801 commit d48c7b2
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/vendor/
composer.lock
composer.lock
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- hhvm

matrix:
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
"name": "ptachoire/php-dmtx",
"description": "Datamatrix r/w based on libdmtx <http://www.libdmtx.org/>",
"require": {
"symfony/process": "~2.5",
"symfony/options-resolver": "~2.5"
"symfony/process": "~3.4||~4.0",
"symfony/options-resolver": "~3.4||~4.0"
},
"require-dev": {
"phpunit/phpunit": "~5.7"
},
"autoload": {
"psr-0": { "Dmtx": "src/" }
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ echo $reader->decodeFile('/tmp/image.png');

```sh
composer install
phpunit
./vendor/bin/phpunit
```

## Credits
Expand Down
50 changes: 21 additions & 29 deletions src/Dmtx/AbstractDmtx.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

namespace Dmtx;

use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Process\Process;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;


abstract class AbstractDmtx
{
protected $options = array();
protected $arguments = array();
protected $messages = array();
protected $options = [];
protected $arguments = [];
protected $messages = [];

public function __construct(array $options = array())
public function __construct(array $options = [])
{
$this->messages = array();
$this->messages = [];

$resolver = new OptionsResolver();
$this->setDefaultOptions($resolver);

$this->options = $resolver->resolve($options);
}

protected function setDefaultOptions(OptionsResolverInterface $resolver)
protected function setDefaultOptions(OptionsResolver $resolver)
{
$resolver->setRequired('command');
}
Expand All @@ -41,19 +41,15 @@ protected function getArgument($argument)
return $this->options[$argument];
}

protected function getProcessBuilder($cmd, array $extras = array(), $input = null)
protected function getProcess($cmd, array $extras = [], $input = null)
{
$builder = new ProcessBuilder();
$builder->add($cmd);
$builder->setTimeout($this->options['process-timeout']);
$cmdArguments = [$cmd];

foreach ($this->arguments as $argument) {
try {
$builder->add(
$this->getFormattedParameter(
$argument,
$this->getArgument($argument)
)
$cmdArguments[] = $this->getFormattedParameter(
$argument,
$this->getArgument($argument)
);
} catch (\InvalidArgumentException $ex) {
//nothing here
Expand All @@ -62,24 +58,22 @@ protected function getProcessBuilder($cmd, array $extras = array(), $input = nul

foreach ($extras as $key => $value) {
try {
$builder->add(
$this->getFormattedParameter(
$key,
$value
)
);
$cmdArguments[] = $this->getFormattedParameter($key, $value);
} catch (\InvalidArgumentException $ex) {
//nothing here
}
}

$process = new Process($cmdArguments);
$process->setTimeout($this->options['process-timeout']);

if (!is_null($input)) {
$builder->setInput(
$process->setInput(
$input
);
}

return $builder;
return $process;
}

private function getFormattedParameter($key, $value)
Expand All @@ -101,11 +95,9 @@ private function getFormattedParameter($key, $value)
return null;
}

protected function run($cmd, $input = null, array $extras = array())
protected function run($cmd, $input = null, array $extras = [])
{
$process = $this
->getProcessBuilder($cmd, $extras, $input)
->getProcess();
$process = $this->getProcess($cmd, $extras, $input);

$process->run();

Expand Down
39 changes: 21 additions & 18 deletions src/Dmtx/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
namespace Dmtx;

use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Process\ProcessBuilder;

class Reader extends AbstractDmtx
{
protected $arguments = array(
protected $arguments = [
'newline',
'unicode',
'milliseconds',
Expand All @@ -32,19 +30,19 @@ class Reader extends AbstractDmtx
'page-numbers',
'corners',
'shrink'
);
];

protected function setDefaultOptions(OptionsResolverInterface $resolver)
protected function setDefaultOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
$resolver->setDefaults([
'unicode' => true,
'milliseconds' => 200,
'symbol-size' => 'square-auto',
'process-timeout' => 600,
'command' => 'dmtxread'
));
]);

$resolver->setOptional(array(
$resolver->setDefined([
'newline',
'unicode',
'milliseconds',
Expand All @@ -68,19 +66,22 @@ protected function setDefaultOptions(OptionsResolverInterface $resolver)
'page-numbers',
'corners',
'shrink'
));
]);

$resolver->setAllowedValues(array(
'symbol-size' => array(
$allowedValues = [
'symbol-size' => [
'square-auto',
'rectangle-auto',
'10x10',
'24x24',
'64x64'
)
));
],
];
foreach ($allowedValues as $option => $allowedValue) {
$resolver->setAllowedValues($option, $allowedValue);
}

$resolver->setAllowedTypes(array(
$allowedTypes = [
'newline' => 'bool',
'unicode' => 'bool',
'milliseconds' => 'integer',
Expand All @@ -99,8 +100,10 @@ protected function setDefaultOptions(OptionsResolverInterface $resolver)
'page-numbers' => 'bool',
'corners' => 'bool',
'shrink' => 'integer'

));
];
foreach ($allowedTypes as $option => $allowedType) {
$resolver->setAllowedTypes($option, $allowedTypes);
}
}

public function decode($encoded_string)
Expand All @@ -120,9 +123,9 @@ public function decodeFile($filename)
}


public function processBuilder()
public function process()
{
return $this->getProcessBuilder(
return $this->getProcess(
$this->getCmd()
);
}
Expand Down
55 changes: 30 additions & 25 deletions src/Dmtx/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,72 @@
namespace Dmtx;

use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class Writer extends AbstractDmtx
{
protected $arguments = array(
protected $arguments = [
'encoding',
'module',
'symbol-size',
'format',
'resolution',
'margin'
);
'margin',
];

protected function setDefaultOptions(OptionsResolverInterface $resolver)
protected function setDefaultOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
$resolver->setDefaults([
'encoding' => 'ascii',
'module' => 5,
'symbol-size' => 'square-auto',
'format' => 'png',
'message-separator' => ' ',
'process-timeout' => 600,
'command' => 'dmtxwrite'
));
'command' => 'dmtxwrite',
]);

$resolver->setOptional(array(
$resolver->setDefined([
'resolution',
'margin'
));
'margin',
]);

$resolver->setAllowedValues(array(
'encoding' => array(
$allowedValues = [
'encoding' => [
'best',
'fast',
'ascii',
'c40',
'text',
'x12',
'edifact',
'8base256'
),
'format' => array(
'8base256',
],
'format' => [
'png',
'tif',
'gif',
'pdf'
),
'symbol-size' => array(
'pdf',
],
'symbol-size' => [
'square-auto',
'rectangle-auto',
'10x10',
'24x24',
'64x64'
)
));
'64x64',
],
];
foreach ($allowedValues as $option => $allowedValue) {
$resolver->setAllowedValues($option, $allowedValue);
}

$resolver->setAllowedTypes(array(
$allowedTypes = [
'resolution' => 'integer',
'module' => 'integer',
'margin' => 'integer'
));
'margin' => 'integer',
];
foreach ($allowedTypes as $option => $allowedType) {
$resolver->setAllowedTypes($option, $allowedTypes);
}
}

public function encode($message)
Expand Down
29 changes: 18 additions & 11 deletions tests/Dmtx/Tests/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,36 @@

class ReaderTest extends TestCase
{
/** @var Reader */
private $reader;

protected function setUp(
$options = array()
$options = []
) {
$this->reader = new Reader($options);
}

public function imageTestProvider()
{
return array(
'simpleMessageShouldBeValid' => array(
array(),
array('yo'),
return [
'simpleMessageShouldBeValid' => [
[],
['yo'],
dirname(__FILE__).'/rsc/yo.png'
),
'multiMessagesShouldBeValid' => array(
array(),
array('yo','this','is','a','message'),
],
'multiMessagesShouldBeValid' => [
[],
['yo','this','is','a','message'],
dirname(__FILE__).'/rsc/yos.png'
)
);
]
];
}

/**
* @dataProvider imageTestProvider
* @param array $options
* @param array $expected_messages
* @param $filename
*/
public function testDecodeShouldReturnValidMessage(
array $options,
Expand All @@ -48,6 +52,9 @@ public function testDecodeShouldReturnValidMessage(

/**
* @dataProvider imageTestProvider
* @param array $options
* @param array $expected_messages
* @param $filename
*/
public function testDecodeFileShouldReturnValidMessage(
array $options,
Expand Down
Loading

0 comments on commit d48c7b2

Please sign in to comment.