Skip to content

Commit

Permalink
Fix create parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Apr 22, 2017
1 parent d847326 commit 7bf9217
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,37 @@ public function registerBundles()
}
```

The default parameters can be overridden via the configuration.
The bundle makes use of a factory to create QR codes. The default parameters
applied by the factory can optionally be overridden via the configuration.

```yaml
endroid_qr_code:
size: 300
quiet_zone: 2
foreground_color: { r: 0, g: 0, b: 0 }
background_color: { r: 255, g: 255, b: 255 }
error_correction_level: high
error_correction_level: high # low, medium, quartile or high
encoding: UTF-8
label: Scan the code
label_font_size: 20
label_alignment: left
label_alignment: left # left, center or right
label_margin: { b: 20 }
logo_path: '%kernel.root_dir%/../vendor/endroid/qrcode/logo/symfony.png'
logo_size: 150
validate_result: true
validate_result: true # checks if the result is readable
```
Now you can retrieve the factory as follows and create a QR code.
The validate_result option uses a built-in reader to validate the resulting
image. Please note that each QR code reader has its own reliability so this
is not an absolute guarantee that the code will be readable by all readers.
Also, if the built-in reader fails the Qr code might still be readable by an
other reader. This setting just helps you guarantee a minimum level of quality.
Now you can retrieve the factory from the service container and create a QR
code. For instance in your controller this would look like this.
```php
$factory = $this->get('endroid.qrcode.factory');
$qrCode = $factory->create('Life is too short to be generating QR codes');
$qrCode = $this->get('endroid.qrcode.factory')->create('Bladiebla');
```

Add the following section to your routing to be able to handle QR code URLs.
Expand Down
3 changes: 1 addition & 2 deletions src/Bundle/Controller/QrCodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class QrCodeController extends Controller
public function generateAction(Request $request, $text, $extension)
{
$options = $request->query->all();
$options['text'] = $text;

$qrCode = $this->getQrCodeFactory()->create($options);
$qrCode = $this->getQrCodeFactory()->create($text, $options);
$writer = $qrCode->getWriterByExtension($extension);

return new Response(
Expand Down
6 changes: 3 additions & 3 deletions src/Factory/QrCodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class QrCodeFactory
* @var array
*/
private $definedOptions = [
'text',
'size',
'quiet_zone',
'foreground_color',
Expand Down Expand Up @@ -58,15 +57,16 @@ public function __construct(array $defaultOptions = [])
}

/**
* @param string $text
* @param array $options
* @return QrCode
*/
public function create(array $options = [])
public function create($text = '', array $options = [])
{
$options = $this->getOptionsResolver()->resolve($options);
$accessor = PropertyAccess::createPropertyAccessor();

$qrCode = new QrCode();
$qrCode = new QrCode($text);
foreach ($this->definedOptions as $option) {
if (isset($options[$option])) {
$accessor->setValue($qrCode, $option, $options[$option]);
Expand Down

0 comments on commit 7bf9217

Please sign in to comment.