Skip to content

Commit

Permalink
Complete new version
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Apr 17, 2017
1 parent 381fb65 commit 9ed1d30
Show file tree
Hide file tree
Showing 16 changed files with 526 additions and 116 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,22 @@ public function registerBundles()
```

The default parameters can be overridden via the configuration.
Alpha channel available range is [0, 127] in foreground and background colors.

```yaml
endroid_qr_code:
size: 100
size: 300
quiet_zone: 2
foreground_color: { r: 0, g: 0, b: 0, a: 0 }
background_color: { r: 255, g: 255, b: 255, a: 0 }
encoding: 'UTF-8'
error_correction_level: H
label: 'Scan the code'
label_font_size: 16
foreground_color: { r: 0, g: 0, b: 0 }
background_color: { r: 255, g: 255, b: 255 }
error_correction_level: high
encoding: UTF-8
label: Scan the code
label_font_size: 20
label_alignment: left
label_margin: { b: 20 }
logo_path: '%kernel.root_dir%/../vendor/endroid/qrcode/logo/symfony.png'
logo_size: 150
validate_result: true
```
Now you can retrieve the factory as follows and create a QR code.
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
"php": ">=5.6",
"ext-gd": "*",
"symfony/options-resolver": "^3.0",
"bacon/bacon-qr-code": "^1.0"
"bacon/bacon-qr-code": "^1.0",
"myclabs/php-enum": "^1.5",
"khanamiryan/qrcode-detector-decoder": "dev-master"
},
"require-dev": {
"symfony/browser-kit": "^3.0",
"symfony/framework-bundle": "^3.0",
"symfony/http-kernel": "^3.0",
"symfony/yaml": "^3.0",
"sensio/framework-extra-bundle": "^3.0",
"khanamiryan/qrcode-detector-decoder": "dev-master",
"phpunit/phpunit": "^5.0|^6.0"
},
"autoload": {
Expand Down
Binary file added logo/symfony.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 25 additions & 9 deletions src/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

namespace Endroid\QrCode\Bundle\DependencyInjection;

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Predis\Response\Error;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -24,34 +27,47 @@ public function getConfigTreeBuilder()
->children()
->integerNode('size')->min(0)->defaultValue(200)->end()
->integerNode('quiet_zone')->min(0)->end()
->scalarNode('label')->end()
->integerNode('label_font_size')->end()
->scalarNode('label_font_path')->end()
->scalarNode('encoding')->defaultValue('UTF-8')->end()
->scalarNode('error_correction_level')
->validate()
->ifTrue(function ($value) {
return !in_array($value, QrCode::getAvailableErrorCorrectionLevels());
})
->thenInvalid('Invalid error correction level "%s"')
->ifNotInArray(ErrorCorrectionLevel::toArray())
->thenInvalid('Invalid error correction level %s')
->end()
->end()
->arrayNode('foreground_color')
->children()
->scalarNode('r')->isRequired()->end()
->scalarNode('g')->isRequired()->end()
->scalarNode('b')->isRequired()->end()
->scalarNode('a')->isRequired()->end()
->end()
->end()
->arrayNode('background_color')
->children()
->scalarNode('r')->isRequired()->end()
->scalarNode('g')->isRequired()->end()
->scalarNode('b')->isRequired()->end()
->scalarNode('a')->isRequired()->end()
->end()
->end()
->scalarNode('label')->end()
->integerNode('label_font_size')->end()
->scalarNode('label_font_path')->end()
->scalarNode('label_alignment')
->validate()
->ifNotInArray(LabelAlignment::toArray())
->thenInvalid('Invalid label alignment %s')
->end()
->end()
->arrayNode('label_margin')
->children()
->scalarNode('t')->end()
->scalarNode('r')->end()
->scalarNode('b')->end()
->scalarNode('l')->end()
->end()
->end()
->scalarNode('logo_path')->end()
->integerNode('logo_size')->end()
->booleanNode('validate_result')->end()
->end()
->end()
;
Expand Down
20 changes: 20 additions & 0 deletions src/ErrorCorrectionLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* (c) Jeroen van den Enden <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Endroid\QrCode;

use MyCLabs\Enum\Enum;

class ErrorCorrectionLevel extends Enum
{
const LOW = 'low';
const MEDIUM = 'medium';
const QUARTILE = 'quartile';
const HIGH = 'high';
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

namespace Endroid\QrCode\Exception;

class InvalidLabelFontPathException extends QrCodeException
class InvalidPathException extends QrCodeException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

namespace Endroid\QrCode\Exception;

class InvalidErrorCorrectionLevelException extends QrCodeException
class MissingFunctionException extends QrCodeException
{
}
14 changes: 14 additions & 0 deletions src/Exception/ValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* (c) Jeroen van den Enden <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Endroid\QrCode\Exception;

class ValidationException extends QrCodeException
{
}
105 changes: 69 additions & 36 deletions src/Factory/QrCodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,52 @@

namespace Endroid\QrCode\Factory;

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccess;

class QrCodeFactory
{
/**
* @var array
*/
private $definedOptions = [
'text',
'size',
'quiet_zone',
'foreground_color',
'background_color',
'encoding',
'error_correction_level',
'label',
'label_font_size',
'label_font_path',
'label_alignment',
'label_margin',
'logo_path',
'logo_size',
'validate_result'
];

/**
* @var array
*/
private $defaultOptions;

/**
* @var OptionsResolver
*/
protected $optionsResolver;
private $optionsResolver;

/**
* @param array $defaults
* @param array $defaultOptions
*/
public function __construct(array $defaults = [])
public function __construct(array $defaultOptions = [])
{
$defaults = array_merge($this->getAvailableOptions(), $defaults);
$this->optionsResolver = new OptionsResolver();
$this->optionsResolver->setDefaults($defaults);
$this->defaultOptions = $defaultOptions;
}

/**
Expand All @@ -35,49 +63,54 @@ public function __construct(array $defaults = [])
*/
public function create(array $options = [])
{
$options = $this->optionsResolver->resolve($options);
$options = $this->getOptionsResolver()->resolve($options);
$accessor = PropertyAccess::createPropertyAccessor();

$qrCode = new QrCode($options['text']);
$qrCode
->setText($options['text'])
->setSize($options['size'])
->setQuietZone($options['quiet_zone'])
->setForegroundColor($options['foreground_color'])
->setBackgroundColor($options['background_color'])
->setEncoding($options['encoding'])
->setErrorCorrectionLevel($options['error_correction_level'])
->setLabel($options['label'], $options['label_font_size'], $options['label_font_path'])
;
$qrCode = new QrCode();
foreach ($this->definedOptions as $option) {
if (isset($options[$option])) {
$accessor->setValue($qrCode, $option, $options[$option]);
}
}

return $qrCode;
}

/**
* @return array
* @return OptionsResolver
*/
public function getAvailableOptions()
private function getOptionsResolver()
{
$options = [
'text' => null,
'size' => null,
'quiet_zone' => null,
'foreground_color' => null,
'background_color' => null,
'encoding' => null,
'error_correction_level' => null,
'label' => null,
'label_font_size' => null,
'label_font_path' => null,
];
if (!$this->optionsResolver instanceof OptionsResolver) {
$this->optionsResolver = $this->createOptionsResolver();
}

return $options;
return $this->optionsResolver;
}

/**
* @return array
* @return OptionsResolver
*/
public function getDefaultOptions()
private function createOptionsResolver()
{
return $this->optionsResolver->resolve();
$optionsResolver = new OptionsResolver();
$optionsResolver
->setDefaults($this->defaultOptions)
->setDefined($this->definedOptions)
->setNormalizer('error_correction_level', function (Options $options, $value) {
if ($value !== null) {
$value = new ErrorCorrectionLevel($value);
}
return $value;
})
->setNormalizer('label_alignment', function (Options $options, $value) {
if ($value !== null) {
$value = new LabelAlignment($value);
}
return $value;
})
;

return $optionsResolver;
}
}
19 changes: 19 additions & 0 deletions src/LabelAlignment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* (c) Jeroen van den Enden <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Endroid\QrCode;

use MyCLabs\Enum\Enum;

class LabelAlignment extends Enum
{
const LEFT = 'left';
const CENTER = 'center';
const RIGHT = 'right';
}
Loading

0 comments on commit 9ed1d30

Please sign in to comment.