diff --git a/composer.json b/composer.json index 1291412..344429c 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "tarfin-labs/zbar-php", + "name": "mehr-it/zbar-php", "description": "zbar-php is a php package that provides an interface to the zbar bar-code reading library.", "keywords": [ "tarfin-labs", diff --git a/src/Zbar.php b/src/Zbar.php index 8293dba..a66968d 100644 --- a/src/Zbar.php +++ b/src/Zbar.php @@ -70,11 +70,15 @@ private function runProcess() $this->process->run(); + $output = $this->process->getOutput(); + if (! $this->process->isSuccessful()) { - throw ZbarError::exitStatus($this->process->getExitCode()); + + if ($this->process->getExitCode() !== -1 || strpos($output, 'process->getExitCode()); } - return $this->output = $this->parse($this->process->getOutput()); + return $this->output = $this->parse($output); } /** @@ -87,10 +91,31 @@ private function runProcess() public function scan() { $output = $this->runProcess(); + + if (is_array($output)) + $output = $output[0]; return $output->data; } + /** + * Scan bar-codes and return all values. + * + * @return string[] + * + * @throws \TarfinLabs\ZbarPhp\Exceptions\ZbarError + */ + public function scanAll() { + $output = $this->runProcess(); + + if (!is_array($output)) + $output = [$output]; + + return array_map(function ($item) { + return $item->data; + }, $output); + } + /** * Get the bar-code type after scanning it. * @@ -113,11 +138,34 @@ public function type() public function decode() { $output = $this->runProcess(); + + if (is_array($output)) + $output = $output[0]; + $code = $output->data; $type = $output->{'@attributes'}->type; return new BarCode($code, $type); } + + /** + * Find both the bar-code and type of the bar-codes then returns objects for all barcodes. + * + * @return BarCode[] + * + * @throws \TarfinLabs\ZbarPhp\Exceptions\ZbarError + */ + public function decodeAll() + { + $output = $this->runProcess(); + + if (!is_array($output)) + $output = [$output]; + + return array_map(function ($item) { + return new BarCode($item->data, $item->{'@attributes'}->type); + }, $output); + } /** * Return symbol object. diff --git a/tests/ZbarTest.php b/tests/ZbarTest.php index 1ef0bb7..3a9fc57 100644 --- a/tests/ZbarTest.php +++ b/tests/ZbarTest.php @@ -34,6 +34,11 @@ class ZbarTest extends TestCase */ protected $ean13; + /** + * @var string + */ + protected $code128andEan13; + /** * @var string */ @@ -49,6 +54,7 @@ protected function setUp(): void $this->emptyImage = __DIR__.'/files/empty.png'; $this->ean13 = __DIR__.'/files/ean-13.jpg'; $this->code128 = __DIR__.'/files/code-128.png'; + $this->code128andEan13 = __DIR__.'/files/code-128-and-ean13.png'; } /** @test */ @@ -141,4 +147,69 @@ public function it_can_get_bar_code_and_type_of_qrcode() $this->assertSame('tarfin', $barCode->code()); $this->assertSame('QR-Code', $barCode->type()); } + + /** @test */ + public function it_can_scan_barcode_from_file_with_multiple_barcodes() { + $zbar = new Zbar($this->code128andEan13); + $code = $zbar->scan(); + + $this->assertSame(true, in_array($code, ['1234567890', '1234567890128'])); + } + + /** @test */ + public function it_can_scan_all_barcodes_from_file_with_multiple_barcodes() { + $zbar = new Zbar($this->code128andEan13); + $codes = $zbar->scanAll(); + + $this->assertSame(true, in_array('1234567890', $codes)); + $this->assertSame(true, in_array('1234567890128', $codes)); + $this->assertCount(2, $codes); + } + + /** @test */ + public function it_can_get_barcode_and_type_from_file_with_multiple_barcodes() { + $zbar = new Zbar($this->code128andEan13); + $barcode = $zbar->decode(); + + switch ($barcode->code()) { + case '1234567890128': + $this->assertSame('EAN-13', $barcode->type()); + break; + case '1234567890': + $this->assertSame('CODE-128', $barcode->type()); + break; + default: + $this->fail("Unexpected barcode value \"{$barcode->code()}\"."); + } + } + + /** @test */ + public function it_can_get_all_barcodes_and_types_from_file_with_multiple_barcodes() { + $zbar = new Zbar($this->code128andEan13); + $barcodes = $zbar->decodeAll(); + + $seen = [ + '1234567890128' => false, + '1234567890' => false, + ]; + + foreach($barcodes as $barcode) { + + + switch ($barcode->code()) { + case '1234567890128': + $this->assertSame('EAN-13', $barcode->type()); + break; + case '1234567890': + $this->assertSame('CODE-128', $barcode->type()); + break; + default: + $this->fail("Unexpected barcode value \"{$barcode->code()}\"."); + } + $seen[$barcode->code()] = true; + } + + $this->assertSame(true, $seen['1234567890128']); + $this->assertSame(true, $seen['1234567890']); + } } diff --git a/tests/files/code-128-and-ean13.png b/tests/files/code-128-and-ean13.png new file mode 100644 index 0000000..77df4e2 Binary files /dev/null and b/tests/files/code-128-and-ean13.png differ