From d34057ae81cc0814485482559f62cf7c76596a81 Mon Sep 17 00:00:00 2001 From: endroid Date: Thu, 30 Nov 2017 00:38:56 +0000 Subject: [PATCH] Major refactoring --- .gitignore | 2 - .travis.yml | 7 +- README.md | 117 ++--- assets/{ => fonts}/noto_sans.otf | Bin assets/{ => fonts}/open_sans.ttf | Bin assets/{ => images}/symfony.png | Bin composer.json | 27 +- phpunit.xml.dist | 5 +- .../Controller/QrCodeController.php | 64 --- .../Compiler/WriterRegistryCompilerPass.php | 36 -- .../DependencyInjection/Configuration.php | 77 ---- .../EndroidQrCodeExtension.php | 34 -- .../QrCodeBundle/EndroidQrCodeBundle.php | 27 -- .../QrCodeBundle/Resources/config/routing.yml | 11 - .../Resources/config/services.yml | 31 -- .../views/QrCode/twigFunctions.html.twig | 3 - src/Factory/QrCodeFactory.php | 48 +- src/Factory/QrCodeFactoryInterface.php | 17 + src/QrCode.php | 420 +++--------------- src/QrCodeInterface.php | 97 +--- src/StaticWriterRegistry.php | 11 +- src/Twig/Extension/QrCodeExtension.php | 67 +-- src/Writer/AbstractBaconWriter.php | 14 +- src/Writer/AbstractWriter.php | 32 +- src/Writer/BinaryWriter.php | 20 +- src/Writer/DebugWriter.php | 15 +- src/Writer/EpsWriter.php | 29 +- src/Writer/PngWriter.php | 73 +-- src/Writer/SvgWriter.php | 29 +- src/Writer/WriterInterface.php | 48 +- src/WriterRegistry.php | 58 +-- src/WriterRegistryInterface.php | 21 +- .../Controller/QrCodeControllerTest.php | 45 -- tests/Bundle/EndroidQrCodeBundleTest.php | 20 - tests/Bundle/app/.gitignore | 2 - tests/Bundle/app/AppKernel.php | 36 -- tests/Bundle/app/bootstrap.php | 3 - tests/Bundle/app/config/config.yml | 8 - tests/Bundle/app/config/routing.yml | 3 - tests/QrCodeTest.php | 14 +- 40 files changed, 254 insertions(+), 1317 deletions(-) rename assets/{ => fonts}/noto_sans.otf (100%) rename assets/{ => fonts}/open_sans.ttf (100%) rename assets/{ => images}/symfony.png (100%) delete mode 100755 src/Bundle/QrCodeBundle/Controller/QrCodeController.php delete mode 100644 src/Bundle/QrCodeBundle/DependencyInjection/Compiler/WriterRegistryCompilerPass.php delete mode 100755 src/Bundle/QrCodeBundle/DependencyInjection/Configuration.php delete mode 100644 src/Bundle/QrCodeBundle/DependencyInjection/EndroidQrCodeExtension.php delete mode 100644 src/Bundle/QrCodeBundle/EndroidQrCodeBundle.php delete mode 100644 src/Bundle/QrCodeBundle/Resources/config/routing.yml delete mode 100755 src/Bundle/QrCodeBundle/Resources/config/services.yml delete mode 100644 src/Bundle/QrCodeBundle/Resources/views/QrCode/twigFunctions.html.twig create mode 100644 src/Factory/QrCodeFactoryInterface.php delete mode 100644 tests/Bundle/Controller/QrCodeControllerTest.php delete mode 100644 tests/Bundle/EndroidQrCodeBundleTest.php delete mode 100644 tests/Bundle/app/.gitignore delete mode 100644 tests/Bundle/app/AppKernel.php delete mode 100644 tests/Bundle/app/bootstrap.php delete mode 100644 tests/Bundle/app/config/config.yml delete mode 100644 tests/Bundle/app/config/routing.yml diff --git a/.gitignore b/.gitignore index d0eeeda..bc959c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ -/bin /composer.lock -/composer.phar /phpunit.xml /vendor diff --git a/.travis.yml b/.travis.yml index 1a695e5..632190f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,17 @@ language: php php: - - 5.6 - - 7.0 - 7.1 + - 7.2 matrix: fast_finish: true before_install: - phpenv config-rm xdebug.ini - - composer self-update && composer install --no-interaction + - composer install --no-interaction -script: bin/phpunit +script: vendor/bin/phpunit notifications: email: info@endroid.nl diff --git a/README.md b/README.md index e732875..0fb28db 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,7 @@ QR Code [![Monthly Downloads](http://img.shields.io/packagist/dm/endroid/qrcode.svg)](https://packagist.org/packages/endroid/qrcode) [![License](http://img.shields.io/packagist/l/endroid/qrcode.svg)](https://packagist.org/packages/endroid/qrcode) -This library helps you generate QR codes in an easy way and provides a Symfony -bundle for rapid integration in your project. +This library helps you generate QR codes in a jiffy. ## Installation @@ -44,18 +43,16 @@ $qrCode = new QrCode('Life is too short to be generating QR codes'); $qrCode->setSize(300); // Set advanced options -$qrCode - ->setWriterByName('png') - ->setMargin(10) - ->setEncoding('UTF-8') - ->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH) - ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0]) - ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255]) - ->setLabel('Scan the code', 16, __DIR__.'/../assets/noto_sans.otf', LabelAlignment::CENTER) - ->setLogoPath(__DIR__.'/../assets/symfony.png') - ->setLogoWidth(150) - ->setValidateResult(false) -; +$qrCode->setWriterByName('png'); +$qrCode->setMargin(10); +$qrCode->setEncoding('UTF-8'); +$qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH); +$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0]); +$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255]); +$qrCode->setLabel('Scan the code', 16, __DIR__.'/../assets/fonts/noto_sans.otf', LabelAlignment::CENTER); +$qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png'); +$qrCode->setLogoWidth(150); +$qrCode->setValidateResult(false); // Directly output the QR code header('Content-Type: '.$qrCode->getContentType()); @@ -68,84 +65,32 @@ $qrCode->writeFile(__DIR__.'/qrcode.png'); $response = new Response($qrCode->writeString(), Response::HTTP_OK, ['Content-Type' => $qrCode->getContentType()]); ``` -![QR Code](http://endroid.nl/qrcode/Dit%20is%20een%20test.png) +![QR Code](https://endroid.nl/qrcode/Life%20is%20too%20short%20to%20be%20generating%20QR%20codes.png) -## Symfony integration - -When you use Symfony Flex, the bundle is automatically registered and the -configuration and routes are automatically created when you installed the -package. In other scenarios you can register the bundle as follows. - -```php -// app/AppKernel.php - -public function registerBundles() -{ - $bundles = [ - // ... - new Endroid\QrCode\Bundle\QrCodeBundle\EndroidQrCodeBundle(), - ]; -} -``` - -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: - writer: 'png' - size: 300 - margin: 10 - foreground_color: { r: 0, g: 0, b: 0 } - background_color: { r: 255, g: 255, b: 255 } - error_correction_level: low # low, medium, quartile or high - encoding: UTF-8 - label: Scan the code - label_font_size: 20 - label_alignment: left # left, center or right - label_margin: { b: 20 } - logo_path: '%kernel.root_dir%/../vendor/endroid/qrcode/assets/symfony.png' - logo_width: 150 - validate_result: false # checks if the result is readable -``` +## Built-in validation reader +You can enable the built-in validation reader (disabled by default) by calling +setValidateResult(true). This validation reader does not guarantee that the QR +code will be readable by all readers but it helps you provide a minimum level +of quality. + The readability of a QR code is primarily determined by the size, the input -length, the error correction level and any possible logo over the image. The -`validate_result` option uses a built-in reader to validate the resulting -image. This does not guarantee that the code will be readable by all readers -but this helps you provide a minimum level of quality. Take note that the -validator can consume quite an amount of resources and is disabled by default. +length, the error correction level and any possible logo over the image so you +can tweak these parameters if you are looking for optimal results. Take note +that the validator can consume quite amount of additional resources. -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 -$qrCode = $this->get('endroid.qrcode.factory')->create('QR Code', ['size' => 200]); -``` - -Add the following section to your routing to be able to handle QR code URLs. -This step can be skipped if you only use data URIs to display your images. - -``` yml -EndroidQrCodeBundle: - resource: "@EndroidQrCodeBundle/Resources/config/routing.yml" - prefix: /qrcode -``` - -After installation and configuration, QR codes can be generated by appending -the QR code text to the url followed by any of the supported extensions. - -## Twig extension +## Symfony integration -The bundle provides a Twig extension for generating a QR code URL, path or data -URI. You can use the second argument of any of these functions to override any -defaults defined by the bundle or set via your configuration. +The [endroid/qrcode-bundle](https://github.com/endroid/EndroidQrCodeBundle) +integrates the QR code library in Symfony for an even better experience. -``` twig - - - -``` +* Configure your defaults (like image size, default writer etc.) +* Generate QR codes quickly from anywhere via the factory service +* Generate QR codes directly by typing an URL like /qrcode/\.png?size=300 +* Generate QR codes or URLs directly from Twig using dedicated functions + +Read the [bundle documentation](https://github.com/endroid/EndroidQrCodeBundle) +for more information. ## Versioning diff --git a/assets/noto_sans.otf b/assets/fonts/noto_sans.otf similarity index 100% rename from assets/noto_sans.otf rename to assets/fonts/noto_sans.otf diff --git a/assets/open_sans.ttf b/assets/fonts/open_sans.ttf similarity index 100% rename from assets/open_sans.ttf rename to assets/fonts/open_sans.ttf diff --git a/assets/symfony.png b/assets/images/symfony.png similarity index 100% rename from assets/symfony.png rename to assets/images/symfony.png diff --git a/composer.json b/composer.json index 8ff53a6..add3587 100755 --- a/composer.json +++ b/composer.json @@ -1,36 +1,28 @@ { "name": "endroid/qrcode", "description": "Endroid QR Code", - "keywords": ["endroid", "qrcode", "qr", "code", "bundle", "symfony", "flex"], + "keywords": ["endroid", "qrcode", "qr", "code", "bundle", "php"], "homepage": "https://github.com/endroid/QrCode", - "type": "symfony-bundle", + "type": "library", "license": "MIT", "authors": [ { "name": "Jeroen van den Enden", "email": "info@endroid.nl", - "homepage": "http://endroid.nl/" + "homepage": "https://endroid.nl/" } ], "require": { - "php": ">=5.6", + "php": ">=7.1", "ext-gd": "*", - "symfony/options-resolver": ">=2.7", + "symfony/options-resolver": "^2.7|^3.0|^4.0", "bacon/bacon-qr-code": "^1.0.3", "khanamiryan/qrcode-detector-decoder": "^1.0", - "symfony/property-access": ">=2.7", + "symfony/property-access": "^2.7|^3.0|^4.0", "myclabs/php-enum": "^1.5" }, "require-dev": { - "symfony/asset": ">=2.7", - "symfony/browser-kit": ">=2.7", - "symfony/finder": ">=2.7", - "symfony/framework-bundle": ">=2.7", - "symfony/http-kernel": ">=2.7", - "symfony/templating": ">=2.7", - "symfony/twig-bundle": ">=2.7", - "symfony/yaml": ">=2.7", - "phpunit/phpunit": ">=5.7" + "phpunit/phpunit": "^5.7|^6.0" }, "autoload": { "psr-4": { @@ -42,12 +34,9 @@ "Endroid\\QrCode\\Tests\\": "tests/" } }, - "config": { - "bin-dir": "bin" - }, "extra": { "branch-alias": { - "dev-master": "2.x-dev" + "dev-master": "3.x-dev" } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0b5f706..2c56aa1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,11 +1,8 @@ - + tests - - - diff --git a/src/Bundle/QrCodeBundle/Controller/QrCodeController.php b/src/Bundle/QrCodeBundle/Controller/QrCodeController.php deleted file mode 100755 index 389c420..0000000 --- a/src/Bundle/QrCodeBundle/Controller/QrCodeController.php +++ /dev/null @@ -1,64 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Endroid\QrCode\Bundle\QrCodeBundle\Controller; - -use Endroid\QrCode\Factory\QrCodeFactory; -use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpFoundation\Request; - -/** - * QR code controller. - */ -class QrCodeController extends Controller -{ - /** - * @param Request $request - * @param string $text - * @param string $extension - * - * @return Response - */ - public function generateAction(Request $request, $text, $extension) - { - $options = $request->query->all(); - - $qrCode = $this->getQrCodeFactory()->create($text, $options); - $qrCode->setWriterByExtension($extension); - - return new Response($qrCode->writeString(), Response::HTTP_OK, ['Content-Type' => $qrCode->getContentType()]); - } - - /** - * @return Response - */ - public function twigFunctionsAction() - { - if (!$this->has('twig')) { - throw new \LogicException('You can not use the "@Template" annotation if the Twig Bundle is not available.'); - } - - $param = [ - 'message' => 'QR Code', - ]; - - $renderedView = $this->get('twig')->render('@EndroidQrCode/QrCode/twigFunctions.html.twig', $param); - - return new Response($renderedView, Response::HTTP_OK); - } - - /** - * @return QrCodeFactory - */ - protected function getQrCodeFactory() - { - return $this->get('endroid.qrcode.factory'); - } -} diff --git a/src/Bundle/QrCodeBundle/DependencyInjection/Compiler/WriterRegistryCompilerPass.php b/src/Bundle/QrCodeBundle/DependencyInjection/Compiler/WriterRegistryCompilerPass.php deleted file mode 100644 index fcbc79d..0000000 --- a/src/Bundle/QrCodeBundle/DependencyInjection/Compiler/WriterRegistryCompilerPass.php +++ /dev/null @@ -1,36 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Endroid\QrCode\Bundle\QrCodeBundle\DependencyInjection\Compiler; - -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Reference; - -class WriterRegistryCompilerPass implements CompilerPassInterface -{ - /** - * {@inheritdoc} - */ - public function process(ContainerBuilder $container) - { - if (!$container->has('endroid.qrcode.writer_registry')) { - return; - } - - $writerRegistryDefinition = $container->findDefinition('endroid.qrcode.writer_registry'); - - $taggedServices = $container->findTaggedServiceIds('endroid.qrcode.writer'); - foreach ($taggedServices as $id => $tags) { - foreach ($tags as $attributes) { - $writerRegistryDefinition->addMethodCall('addWriter', [new Reference($id), isset($attributes['set_as_default']) && $attributes['set_as_default']]); - } - } - } -} diff --git a/src/Bundle/QrCodeBundle/DependencyInjection/Configuration.php b/src/Bundle/QrCodeBundle/DependencyInjection/Configuration.php deleted file mode 100755 index 7b50de0..0000000 --- a/src/Bundle/QrCodeBundle/DependencyInjection/Configuration.php +++ /dev/null @@ -1,77 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Endroid\QrCode\Bundle\QrCodeBundle\DependencyInjection; - -use Endroid\QrCode\ErrorCorrectionLevel; -use Endroid\QrCode\LabelAlignment; -use Predis\Response\Error; -use Symfony\Component\Config\Definition\Builder\TreeBuilder; -use Symfony\Component\Config\Definition\ConfigurationInterface; - -class Configuration implements ConfigurationInterface -{ - public function getConfigTreeBuilder() - { - $treeBuilder = new TreeBuilder(); - - $treeBuilder - ->root('endroid_qr_code') - ->children() - ->scalarNode('writer')->end() - ->integerNode('size')->min(0)->end() - ->integerNode('margin')->min(0)->end() - ->scalarNode('encoding')->defaultValue('UTF-8')->end() - ->scalarNode('error_correction_level') - ->validate() - ->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() - ->end() - ->end() - ->arrayNode('background_color') - ->children() - ->scalarNode('r')->isRequired()->end() - ->scalarNode('g')->isRequired()->end() - ->scalarNode('b')->isRequired()->end() - ->end() - ->end() - ->scalarNode('logo_path')->end() - ->integerNode('logo_width')->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() - ->booleanNode('validate_result')->end() - ->end() - ->end() - ; - - return $treeBuilder; - } -} diff --git a/src/Bundle/QrCodeBundle/DependencyInjection/EndroidQrCodeExtension.php b/src/Bundle/QrCodeBundle/DependencyInjection/EndroidQrCodeExtension.php deleted file mode 100644 index 2bc69b9..0000000 --- a/src/Bundle/QrCodeBundle/DependencyInjection/EndroidQrCodeExtension.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Endroid\QrCode\Bundle\QrCodeBundle\DependencyInjection; - -use Symfony\Component\Config\Definition\Processor; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; -use Symfony\Component\Config\FileLocator; - -class EndroidQrCodeExtension extends Extension -{ - /** - * {@inheritdoc} - */ - public function load(array $configs, ContainerBuilder $container) - { - $processor = new Processor(); - $config = $processor->processConfiguration(new Configuration(), $configs); - - $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); - $loader->load('services.yml'); - - $factoryDefinition = $container->getDefinition('endroid.qrcode.factory'); - $factoryDefinition->replaceArgument(0, $config); - } -} diff --git a/src/Bundle/QrCodeBundle/EndroidQrCodeBundle.php b/src/Bundle/QrCodeBundle/EndroidQrCodeBundle.php deleted file mode 100644 index db7cf5f..0000000 --- a/src/Bundle/QrCodeBundle/EndroidQrCodeBundle.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Endroid\QrCode\Bundle\QrCodeBundle; - -use Endroid\QrCode\Bundle\QrCodeBundle\DependencyInjection\Compiler\WriterRegistryCompilerPass; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\HttpKernel\Bundle\Bundle; - -class EndroidQrCodeBundle extends Bundle -{ - /** - * {@inheritdoc} - */ - public function build(ContainerBuilder $container) - { - parent::build($container); - - $container->addCompilerPass(new WriterRegistryCompilerPass()); - } -} diff --git a/src/Bundle/QrCodeBundle/Resources/config/routing.yml b/src/Bundle/QrCodeBundle/Resources/config/routing.yml deleted file mode 100644 index bfc3228..0000000 --- a/src/Bundle/QrCodeBundle/Resources/config/routing.yml +++ /dev/null @@ -1,11 +0,0 @@ -endroid_qrcode_generate: - path: /{text}.{extension} - requirements: - text: "[\\w\\W]+" - defaults: - _controller: EndroidQrCodeBundle:QrCode:generate - -endroid_qrcode_twig_functions: - path: /twig - defaults: - _controller: EndroidQrCodeBundle:QrCode:twigFunctions diff --git a/src/Bundle/QrCodeBundle/Resources/config/services.yml b/src/Bundle/QrCodeBundle/Resources/config/services.yml deleted file mode 100755 index cbb4d6a..0000000 --- a/src/Bundle/QrCodeBundle/Resources/config/services.yml +++ /dev/null @@ -1,31 +0,0 @@ -services: - endroid.qrcode.factory: - class: Endroid\QrCode\Factory\QrCodeFactory - arguments: [ null, '@endroid.qrcode.writer_registry' ] - endroid.qrcode.twig.extension: - class: Endroid\QrCode\Twig\Extension\QrCodeExtension - arguments: [ '@endroid.qrcode.factory', '@router'] - tags: - - { name: twig.extension } - endroid.qrcode.writer_registry: - class: Endroid\QrCode\WriterRegistry - endroid.qrcode.writer.binary_writer: - class: Endroid\QrCode\Writer\BinaryWriter - tags: - - { name: endroid.qrcode.writer } - endroid.qrcode.writer.debug_writer: - class: Endroid\QrCode\Writer\DebugWriter - tags: - - { name: endroid.qrcode.writer } - endroid.qrcode.writer.eps_writer: - class: Endroid\QrCode\Writer\EpsWriter - tags: - - { name: endroid.qrcode.writer } - endroid.qrcode.writer.png_writer: - class: Endroid\QrCode\Writer\PngWriter - tags: - - { name: endroid.qrcode.writer, set_as_default: true } - endroid.qrcode.writer.svg_writer: - class: Endroid\QrCode\Writer\SvgWriter - tags: - - { name: endroid.qrcode.writer } \ No newline at end of file diff --git a/src/Bundle/QrCodeBundle/Resources/views/QrCode/twigFunctions.html.twig b/src/Bundle/QrCodeBundle/Resources/views/QrCode/twigFunctions.html.twig deleted file mode 100644 index ff448c1..0000000 --- a/src/Bundle/QrCodeBundle/Resources/views/QrCode/twigFunctions.html.twig +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/src/Factory/QrCodeFactory.php b/src/Factory/QrCodeFactory.php index 383e486..c25f48e 100644 --- a/src/Factory/QrCodeFactory.php +++ b/src/Factory/QrCodeFactory.php @@ -10,16 +10,17 @@ namespace Endroid\QrCode\Factory; use Endroid\QrCode\QrCode; +use Endroid\QrCode\QrCodeInterface; use Endroid\QrCode\WriterRegistryInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\PropertyAccess\PropertyAccess; -class QrCodeFactory +class QrCodeFactory implements QrCodeFactoryInterface { - /** - * @var array - */ - protected $definedOptions = [ + private $writerRegistry; + private $optionsResolver; + private $defaultOptions; + private $definedOptions = [ 'writer', 'size', 'margin', @@ -37,38 +38,13 @@ class QrCodeFactory 'validate_result', ]; - /** - * @var array - */ - protected $defaultOptions; - - /** - * @var WriterRegistryInterface - */ - protected $writerRegistry; - - /** - * @var OptionsResolver - */ - protected $optionsResolver; - - /** - * @param array $defaultOptions - * @param WriterRegistryInterface $writerRegistry - */ public function __construct(array $defaultOptions = [], WriterRegistryInterface $writerRegistry = null) { $this->defaultOptions = $defaultOptions; $this->writerRegistry = $writerRegistry; } - /** - * @param string $text - * @param array $options - * - * @return QrCode - */ - public function create($text = '', array $options = []) + public function create(string $text = '', array $options = []): QrCodeInterface { $options = $this->getOptionsResolver()->resolve($options); $accessor = PropertyAccess::createPropertyAccessor(); @@ -92,10 +68,7 @@ public function create($text = '', array $options = []) return $qrCode; } - /** - * @return OptionsResolver - */ - protected function getOptionsResolver() + private function getOptionsResolver(): OptionsResolver { if (!$this->optionsResolver instanceof OptionsResolver) { $this->optionsResolver = $this->createOptionsResolver(); @@ -104,10 +77,7 @@ protected function getOptionsResolver() return $this->optionsResolver; } - /** - * @return OptionsResolver - */ - protected function createOptionsResolver() + private function createOptionsResolver(): OptionsResolver { $optionsResolver = new OptionsResolver(); $optionsResolver diff --git a/src/Factory/QrCodeFactoryInterface.php b/src/Factory/QrCodeFactoryInterface.php new file mode 100644 index 0000000..18aa8fb --- /dev/null +++ b/src/Factory/QrCodeFactoryInterface.php @@ -0,0 +1,17 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Endroid\QrCode\Factory; + +use Endroid\QrCode\QrCodeInterface; + +interface QrCodeFactoryInterface +{ + public function create(string $text = '', array $options = []): QrCodeInterface; +} diff --git a/src/QrCode.php b/src/QrCode.php index 900e3fe..ed0bad5 100644 --- a/src/QrCode.php +++ b/src/QrCode.php @@ -16,110 +16,47 @@ class QrCode implements QrCodeInterface { - const LABEL_FONT_PATH_DEFAULT = __DIR__.'/../assets/noto_sans.otf'; - - /** - * @var string - */ - protected $text; - - /** - * @var int - */ - protected $size = 300; - - /** - * @var int - */ - protected $margin = 10; - - /** - * @var array - */ - protected $foregroundColor = [ + const LABEL_FONT_PATH_DEFAULT = __DIR__.'/../assets/fonts/noto_sans.otf'; + + private $text; + + private $size = 300; + private $margin = 10; + + private $foregroundColor = [ 'r' => 0, 'g' => 0, 'b' => 0, ]; - /** - * @var array - */ - protected $backgroundColor = [ + private $backgroundColor = [ 'r' => 255, 'g' => 255, 'b' => 255, ]; - /** - * @var string - */ - protected $encoding = 'UTF-8'; - - /** - * @var ErrorCorrectionLevel - */ - protected $errorCorrectionLevel; - - /** - * @var string - */ - protected $logoPath; - - /** - * @var int - */ - protected $logoWidth; - - /** - * @var string - */ - protected $label; - - /** - * @var int - */ - protected $labelFontSize = 16; - - /** - * @var string - */ - protected $labelFontPath = self::LABEL_FONT_PATH_DEFAULT; - - /** - * @var LabelAlignment - */ - protected $labelAlignment; - - /** - * @var array - */ - protected $labelMargin = [ + private $encoding = 'UTF-8'; + private $errorCorrectionLevel; + + private $logoPath; + private $logoWidth; + + private $label; + private $labelFontSize = 16; + private $labelFontPath = self::LABEL_FONT_PATH_DEFAULT; + private $labelAlignment; + private $labelMargin = [ 't' => 0, 'r' => 10, 'b' => 10, 'l' => 10, ]; - /** - * @var WriterRegistryInterface - */ - protected $writerRegistry; + private $writerRegistry; + private $writer; + private $validateResult = false; - /** - * @var WriterInterface - */ - protected $writer; - - /** - * @var bool - */ - protected $validateResult = false; - - /** - * @param string $text - */ - public function __construct($text = '') + public function __construct(string $text = '') { $this->text = $text; @@ -129,154 +66,77 @@ public function __construct($text = '') $this->writerRegistry = new StaticWriterRegistry(); } - /** - * @param string $text - * - * @return $this - */ - public function setText($text) + public function setText(string $text): void { $this->text = $text; - - return $this; } - /** - * {@inheritdoc} - */ - public function getText() + public function getText(): string { return $this->text; } - /** - * @param int $size - * - * @return $this - */ - public function setSize($size) + public function setSize(int $size): void { $this->size = $size; - - return $this; } - /** - * {@inheritdoc} - */ - public function getSize() + public function getSize(): int { return $this->size; } - /** - * @param int $margin - * - * @return $this - */ - public function setMargin($margin) + public function setMargin(int $margin): void { $this->margin = $margin; - - return $this; } - /** - * {@inheritdoc} - */ - public function getMargin() + public function getMargin(): int { return $this->margin; } - /** - * @param array $foregroundColor - * - * @return $this - */ - public function setForegroundColor($foregroundColor) + public function setForegroundColor(array $foregroundColor): void { $this->foregroundColor = $foregroundColor; - - return $this; } - /** - * {@inheritdoc} - */ - public function getForegroundColor() + public function getForegroundColor(): array { return $this->foregroundColor; } - /** - * @param array $backgroundColor - * - * @return $this - */ - public function setBackgroundColor($backgroundColor) + public function setBackgroundColor(array $backgroundColor): void { $this->backgroundColor = $backgroundColor; - - return $this; } - /** - * {@inheritdoc} - */ - public function getBackgroundColor() + public function getBackgroundColor(): array { return $this->backgroundColor; } - /** - * @param string $encoding - * - * @return $this - */ - public function setEncoding($encoding) + public function setEncoding(string $encoding): void { $this->encoding = $encoding; - - return $this; } - /** - * {@inheritdoc} - */ - public function getEncoding() + public function getEncoding(): string { return $this->encoding; } - /** - * @param string $errorCorrectionLevel - * - * @return $this - */ - public function setErrorCorrectionLevel($errorCorrectionLevel) + public function setErrorCorrectionLevel(string $errorCorrectionLevel): void { $this->errorCorrectionLevel = new ErrorCorrectionLevel($errorCorrectionLevel); - - return $this; } - /** - * {@inheritdoc} - */ - public function getErrorCorrectionLevel() + public function getErrorCorrectionLevel(): string { return $this->errorCorrectionLevel->getValue(); } - /** - * @param string $logoPath - * - * @return $this - * - * @throws InvalidPathException - */ - public function setLogoPath($logoPath) + public function setLogoPath(string $logoPath): void { $logoPath = realpath($logoPath); @@ -285,48 +145,24 @@ public function setLogoPath($logoPath) } $this->logoPath = $logoPath; - - return $this; } - /** - * {@inheritdoc} - */ - public function getLogoPath() + public function getLogoPath(): string { return $this->logoPath; } - /** - * @param int $logoWidth - * - * @return $this - */ - public function setLogoWidth($logoWidth) + public function setLogoWidth(int $logoWidth): void { $this->logoWidth = $logoWidth; - - return $this; } - /** - * {@inheritdoc} - */ - public function getLogoWidth() + public function getLogoWidth(): int { return $this->logoWidth; } - /** - * @param string $label - * @param int $labelFontSize - * @param string $labelFontPath - * @param string $labelAlignment - * @param array $labelMargin - * - * @return $this - */ - public function setLabel($label, $labelFontSize = null, $labelFontPath = null, $labelAlignment = null, $labelMargin = null) + public function setLabel(string $label, int $labelFontSize = null, string $labelFontPath = null, string $labelAlignment = null, array $labelMargin = null): void { $this->label = $label; @@ -345,46 +181,24 @@ public function setLabel($label, $labelFontSize = null, $labelFontPath = null, $ if (null !== $labelMargin) { $this->setLabelMargin($labelMargin); } - - return $this; } - /** - * {@inheritdoc} - */ - public function getLabel() + public function getLabel(): string { return $this->label; } - /** - * @param int $labelFontSize - * - * @return $this - */ - public function setLabelFontSize($labelFontSize) + public function setLabelFontSize(int $labelFontSize): void { $this->labelFontSize = $labelFontSize; - - return $this; } - /** - * {@inheritdoc} - */ - public function getLabelFontSize() + public function getLabelFontSize(): int { return $this->labelFontSize; } - /** - * @param string $labelFontPath - * - * @return $this - * - * @throws InvalidPathException - */ - public function setLabelFontPath($labelFontPath) + public function setLabelFontPath(string $labelFontPath): void { $labelFontPath = realpath($labelFontPath); @@ -393,88 +207,44 @@ public function setLabelFontPath($labelFontPath) } $this->labelFontPath = $labelFontPath; - - return $this; } - /** - * {@inheritdoc} - */ - public function getLabelFontPath() + public function getLabelFontPath(): string { return $this->labelFontPath; } - /** - * @param string $labelAlignment - * - * @return $this - */ - public function setLabelAlignment($labelAlignment) + public function setLabelAlignment(string $labelAlignment): void { $this->labelAlignment = new LabelAlignment($labelAlignment); - - return $this; } - /** - * {@inheritdoc} - */ - public function getLabelAlignment() + public function getLabelAlignment(): string { return $this->labelAlignment->getValue(); } - /** - * @param int[] $labelMargin - * - * @return $this - */ - public function setLabelMargin(array $labelMargin) + public function setLabelMargin(array $labelMargin): void { $this->labelMargin = array_merge($this->labelMargin, $labelMargin); - - return $this; } - /** - * {@inheritdoc} - */ - public function getLabelMargin() + public function getLabelMargin(): array { return $this->labelMargin; } - /** - * @param WriterRegistryInterface $writerRegistry - * - * @return $this - */ - public function setWriterRegistry(WriterRegistryInterface $writerRegistry) + public function setWriterRegistry(WriterRegistryInterface $writerRegistry): void { $this->writerRegistry = $writerRegistry; - - return $this; } - /** - * @param WriterInterface $writer - * - * @return $this - */ - public function setWriter(WriterInterface $writer) + public function setWriter(WriterInterface $writer): void { $this->writer = $writer; - - return $this; } - /** - * @param WriterInterface $name - * - * @return WriterInterface - */ - public function getWriter($name = null) + public function getWriter(string $name = null): WriterInterface { if (!is_null($name)) { return $this->writerRegistry->getWriter($name); @@ -487,105 +257,57 @@ public function getWriter($name = null) return $this->writerRegistry->getDefaultWriter(); } - /** - * @param string $name - * - * @return $this - * - * @throws InvalidWriterException - */ - public function setWriterByName($name) + public function setWriterByName(string $name) { $this->writer = $this->writerRegistry->getWriter($name); - - return $this; } - /** - * @param string $path - * - * @return $this - */ - public function setWriterByPath($path) + public function setWriterByPath(string $path): void { $extension = pathinfo($path, PATHINFO_EXTENSION); $this->setWriterByExtension($extension); - - return $this; } - /** - * @param string $extension - * - * @return $this - * - * @throws UnsupportedExtensionException - */ - public function setWriterByExtension($extension) + public function setWriterByExtension(string $extension): void { foreach ($this->writerRegistry->getWriters() as $writer) { if ($writer->supportsExtension($extension)) { $this->writer = $writer; - - return $this; + return; } } throw new UnsupportedExtensionException('Missing writer for extension "'.$extension.'"'); } - /** - * @param bool $validateResult - * - * @return $this - */ - public function setValidateResult($validateResult) + public function writeString(): string { - $this->validateResult = $validateResult; - - return $this; + return $this->getWriter()->writeString($this); } - /** - * {@inheritdoc} - */ - public function getValidateResult() + public function writeDataUri(): string { - return $this->validateResult; + return $this->getWriter()->writeDataUri($this); } - /** - * @return string - */ - public function writeString() + public function writeFile(string $path): string { - return $this->getWriter()->writeString($this); + return $this->getWriter()->writeFile($this, $path); } - /** - * @return string - */ - public function writeDataUri() + public function getContentType(): string { - return $this->getWriter()->writeDataUri($this); + return $this->getWriter()->getContentType(); } - /** - * @param string $path - */ - public function writeFile($path) + public function setValidateResult(bool $validateResult): void { - return $this->getWriter()->writeFile($this, $path); + $this->validateResult = $validateResult; } - /** - * @return string - * - * @throws InvalidWriterException - */ - public function getContentType() + public function getValidateResult(): bool { - return $this->getWriter()->getContentType(); + return $this->validateResult; } } diff --git a/src/QrCodeInterface.php b/src/QrCodeInterface.php index 0fdcb96..50d94b8 100644 --- a/src/QrCodeInterface.php +++ b/src/QrCodeInterface.php @@ -11,85 +11,20 @@ interface QrCodeInterface { - /** - * @return string - */ - public function getText(); - - /** - * @return int - */ - public function getSize(); - - /** - * @return int - */ - public function getMargin(); - - /** - * @return int[] - */ - public function getForegroundColor(); - - /** - * @return int[] - */ - public function getBackgroundColor(); - - /** - * @return string - */ - public function getEncoding(); - - /** - * @return string - */ - public function getErrorCorrectionLevel(); - - /** - * @return string - */ - public function getLogoPath(); - - /** - * @return int - */ - public function getLogoWidth(); - - /** - * @return string - */ - public function getLabel(); - - /** - * @return string - */ - public function getLabelFontPath(); - - /** - * @return int - */ - public function getLabelFontSize(); - - /** - * @return string - */ - public function getLabelAlignment(); - - /** - * @return int[] - */ - public function getLabelMargin(); - - /** - * @return bool - */ - public function getValidateResult(); - - /** - * @param WriterRegistryInterface $writerRegistry - * - * @return mixed - */ - public function setWriterRegistry(WriterRegistryInterface $writerRegistry); + public function getText(): string; + public function getSize(): int; + public function getMargin(): int; + public function getForegroundColor(): array; + public function getBackgroundColor(): array; + public function getEncoding(): string; + public function getErrorCorrectionLevel(): string; + public function getLogoPath(): string; + public function getLogoWidth(): int; + public function getLabel(): string; + public function getLabelFontPath(): string; + public function getLabelFontSize(): int; + public function getLabelAlignment(): string; + public function getLabelMargin(): array; + public function getValidateResult(): bool; + public function setWriterRegistry(WriterRegistryInterface $writerRegistry): void; } diff --git a/src/StaticWriterRegistry.php b/src/StaticWriterRegistry.php index 9ab9a86..a8fd061 100644 --- a/src/StaticWriterRegistry.php +++ b/src/StaticWriterRegistry.php @@ -17,9 +17,6 @@ class StaticWriterRegistry extends WriterRegistry { - /** - * {@inheritdoc} - */ public function __construct() { parent::__construct(); @@ -27,16 +24,18 @@ public function __construct() $this->loadWriters(); } - protected function loadWriters() + private function loadWriters(): void { - if (count($this->writers) > 0) { + if (count($this->getWriters()) > 0) { return; } $this->addWriter(new BinaryWriter()); $this->addWriter(new DebugWriter()); $this->addWriter(new EpsWriter()); - $this->addWriter(new PngWriter(), true); + $this->addWriter(new PngWriter()); $this->addWriter(new SvgWriter()); + + $this->setDefaultWriter('png'); } } diff --git a/src/Twig/Extension/QrCodeExtension.php b/src/Twig/Extension/QrCodeExtension.php index cca3c2e..936a271 100755 --- a/src/Twig/Extension/QrCodeExtension.php +++ b/src/Twig/Extension/QrCodeExtension.php @@ -11,6 +11,7 @@ use Endroid\QrCode\Exception\UnsupportedExtensionException; use Endroid\QrCode\Factory\QrCodeFactory; +use Endroid\QrCode\Factory\QrCodeFactoryInterface; use Endroid\QrCode\WriterRegistryInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RouterInterface; @@ -19,31 +20,16 @@ class QrCodeExtension extends Twig_Extension { - /** - * @var QrCodeFactory - */ - protected $qrCodeFactory; + private $qrCodeFactory; + private $router; - /** - * @var RouterInterface - */ - protected $router; - - /** - * @param QrCodeFactory $qrCodeFactory - * @param RouterInterface $router - * @param WriterRegistryInterface $writerRegistry - */ - public function __construct(QrCodeFactory $qrCodeFactory, RouterInterface $router) + public function __construct(QrCodeFactoryInterface $qrCodeFactory, RouterInterface $router) { $this->qrCodeFactory = $qrCodeFactory; $this->router = $router; } - /** - * {@inheritdoc} - */ - public function getFunctions() + public function getFunctions(): array { return [ new Twig_SimpleFunction('qrcode_path', [$this, 'qrCodePathFunction']), @@ -52,36 +38,17 @@ public function getFunctions() ]; } - /** - * @param string $text - * @param array $options - * - * @return string - */ - public function qrcodeUrlFunction($text, array $options = []) + public function qrcodeUrlFunction(string $text, array $options = []): string { return $this->getQrCodeReference($text, $options, UrlGeneratorInterface::ABSOLUTE_URL); } - /** - * @param string $text - * @param array $options - * - * @return string - */ - public function qrCodePathFunction($text, array $options = []) + public function qrCodePathFunction(string $text, array $options = []): string { return $this->getQrCodeReference($text, $options, UrlGeneratorInterface::ABSOLUTE_PATH); } - /** - * @param string $text - * @param array $options - * @param int $referenceType - * - * @return string - */ - public function getQrCodeReference($text, array $options = [], $referenceType) + public function getQrCodeReference(string $text, array $options = [], int $referenceType): string { $qrCode = $this->qrCodeFactory->create($text, $options); $supportedExtensions = $qrCode->getWriter()->getSupportedExtensions(); @@ -92,26 +59,10 @@ public function getQrCodeReference($text, array $options = [], $referenceType) return $this->router->generate('endroid_qrcode_generate', $options, $referenceType); } - /** - * @param string $text - * @param array $options - * - * @return string - * - * @throws UnsupportedExtensionException - */ - public function qrcodeDataUriFunction($text, array $options = []) + public function qrcodeDataUriFunction(string $text, array $options = []): string { $qrCode = $this->qrCodeFactory->create($text, $options); return $qrCode->writeDataUri(); } - - /** - * @return string - */ - public function getName() - { - return 'qrcode'; - } } diff --git a/src/Writer/AbstractBaconWriter.php b/src/Writer/AbstractBaconWriter.php index a52e3a0..0cc4bf6 100644 --- a/src/Writer/AbstractBaconWriter.php +++ b/src/Writer/AbstractBaconWriter.php @@ -13,24 +13,14 @@ abstract class AbstractBaconWriter extends AbstractWriter { - /** - * @param array $color - * - * @return Rgb - */ - protected function convertColor(array $color) + protected function convertColor(array $color): Rgb { $color = new Rgb($color['r'], $color['g'], $color['b']); return $color; } - /** - * @param string $errorCorrectionLevel - * - * @return string - */ - protected function convertErrorCorrectionLevel($errorCorrectionLevel) + protected function convertErrorCorrectionLevel(string $errorCorrectionLevel): string { $name = strtoupper(substr($errorCorrectionLevel, 0, 1)); $errorCorrectionLevel = constant('BaconQrCode\Common\ErrorCorrectionLevel::'.$name); diff --git a/src/Writer/AbstractWriter.php b/src/Writer/AbstractWriter.php index 27c564c..c066894 100644 --- a/src/Writer/AbstractWriter.php +++ b/src/Writer/AbstractWriter.php @@ -14,50 +14,28 @@ abstract class AbstractWriter implements WriterInterface { - /** - * {@inheritdoc} - */ - public function writeDataUri(QrCodeInterface $qrCode) + public function writeDataUri(QrCodeInterface $qrCode): string { $dataUri = 'data:'.$this->getContentType().';base64,'.base64_encode($this->writeString($qrCode)); return $dataUri; } - /** - * {@inheritdoc} - */ - public function writeFile(QrCodeInterface $qrCode, $path) + public function writeFile(QrCodeInterface $qrCode, string $path): string { $string = $this->writeString($qrCode); file_put_contents($path, $string); } - /** - * {@inheritdoc} - */ - public static function supportsExtension($extension) + public static function supportsExtension(string $extension): bool { return in_array($extension, static::getSupportedExtensions()); } - /** - * {@inheritdoc} - */ - public static function getSupportedExtensions() + public static function getSupportedExtensions(): array { return []; } - /** - * {@inheritdoc} - */ - public function getName() - { - $reflectionClass = new ReflectionClass($this); - $className = $reflectionClass->getShortName(); - $name = strtolower(preg_replace('/(?setWidth($qrCode->getSize()); @@ -29,19 +26,12 @@ public function writeString(QrCodeInterface $qrCode) $writer = new Writer($renderer); $string = $writer->writeString($qrCode->getText(), $qrCode->getEncoding(), $this->convertErrorCorrectionLevel($qrCode->getErrorCorrectionLevel())); - $string = $this->addMargin($string, $qrCode); return $string; } - /** - * @param string $string - * @param QrCodeInterface $qrCode - * - * @return string - */ - protected function addMargin($string, QrCodeInterface $qrCode) + protected function addMargin(string $string, QrCodeInterface $qrCode): string { $targetSize = $qrCode->getSize() + $qrCode->getMargin() * 2; @@ -80,19 +70,18 @@ protected function addMargin($string, QrCodeInterface $qrCode) return $string; } - /** - * {@inheritdoc} - */ - public static function getContentType() + public static function getContentType(): string { return 'image/eps'; } - /** - * {@inheritdoc} - */ - public static function getSupportedExtensions() + public static function getSupportedExtensions(): array { return ['eps']; } + + public function getName(): string + { + return 'eps'; + } } diff --git a/src/Writer/PngWriter.php b/src/Writer/PngWriter.php index d35656d..119b062 100644 --- a/src/Writer/PngWriter.php +++ b/src/Writer/PngWriter.php @@ -19,10 +19,7 @@ class PngWriter extends AbstractBaconWriter { - /** - * {@inheritdoc} - */ - public function writeString(QrCodeInterface $qrCode) + public function writeString(QrCodeInterface $qrCode): string { $renderer = new Png(); $renderer->setWidth($qrCode->getSize()); @@ -59,16 +56,7 @@ public function writeString(QrCodeInterface $qrCode) return $string; } - /** - * @param resource $sourceImage - * @param int $margin - * @param int $size - * @param int[] $foregroundColor - * @param int[] $backgroundColor - * - * @return resource - */ - protected function addMargin($sourceImage, $margin, $size, array $foregroundColor, array $backgroundColor) + protected function addMargin(resource $sourceImage, int $margin, int $size, array $foregroundColor, array $backgroundColor): resource { $additionalWhitespace = $this->calculateAdditionalWhiteSpace($sourceImage, $foregroundColor); @@ -84,13 +72,7 @@ protected function addMargin($sourceImage, $margin, $size, array $foregroundColo return $targetImage; } - /** - * @param resource $image - * @param int[] $foregroundColor - * - * @return int - */ - protected function calculateAdditionalWhiteSpace($image, array $foregroundColor) + protected function calculateAdditionalWhiteSpace(resource $image, array $foregroundColor): int { $width = imagesx($image); $height = imagesy($image); @@ -111,14 +93,7 @@ protected function calculateAdditionalWhiteSpace($image, array $foregroundColor) return $whitespace; } - /** - * @param resource $sourceImage - * @param string $logoPath - * @param int $logoWidth - * - * @return resource - */ - protected function addLogo($sourceImage, $logoPath, $logoWidth = null) + protected function addLogo(resource $sourceImage, string $logoPath, int $logoWidth = null): resource { $logoImage = imagecreatefromstring(file_get_contents($logoPath)); $logoSourceWidth = imagesx($logoImage); @@ -140,24 +115,10 @@ protected function addLogo($sourceImage, $logoPath, $logoWidth = null) return $sourceImage; } - /** - * @param resource $sourceImage - * @param string $label - * @param string $labelFontPath - * @param int $labelFontSize - * @param string $labelAlignment - * @param int[] $labelMargin - * @param int[] $foregroundColor - * @param int[] $backgroundColor - * - * @return resource - * - * @throws MissingFunctionException - */ - protected function addLabel($sourceImage, $label, $labelFontPath, $labelFontSize, $labelAlignment, $labelMargin, array $foregroundColor, array $backgroundColor) + protected function addLabel(resource $sourceImage, string $label, string $labelFontPath, int $labelFontSize, string $labelAlignment, array $labelMargin, array $foregroundColor, array $backgroundColor): resource { if (!function_exists('imagettfbbox')) { - throw new MissingFunctionException('Missing function "imagettfbbox". Did you install the FreeType library?'); + throw new MissingFunctionException('Missing function "imagettfbbox", please make sure you installed the FreeType library'); } $labelBox = imagettfbbox($labelFontSize, 0, $labelFontPath, $label); @@ -196,12 +157,7 @@ protected function addLabel($sourceImage, $label, $labelFontPath, $labelFontSize return $targetImage; } - /** - * @param resource $image - * - * @return string - */ - protected function imageToString($image) + protected function imageToString(resource $image): string { ob_start(); imagepng($image); @@ -210,19 +166,18 @@ protected function imageToString($image) return $string; } - /** - * {@inheritdoc} - */ - public static function getContentType() + public static function getContentType(): string { return 'image/png'; } - /** - * {@inheritdoc} - */ - public static function getSupportedExtensions() + public static function getSupportedExtensions(): array { return ['png']; } + + public function getName():string + { + return 'png'; + } } diff --git a/src/Writer/SvgWriter.php b/src/Writer/SvgWriter.php index 204d34f..a13c30c 100644 --- a/src/Writer/SvgWriter.php +++ b/src/Writer/SvgWriter.php @@ -16,10 +16,7 @@ class SvgWriter extends AbstractBaconWriter { - /** - * {@inheritdoc} - */ - public function writeString(QrCodeInterface $qrCode) + public function writeString(QrCodeInterface $qrCode): string { $renderer = new Svg(); $renderer->setWidth($qrCode->getSize()); @@ -36,14 +33,7 @@ public function writeString(QrCodeInterface $qrCode) return $string; } - /** - * @param string $string - * @param int $margin - * @param int $size - * - * @return string - */ - protected function addMargin($string, $margin, $size) + protected function addMargin(string $string, int $margin, int $size): string { $targetSize = $size + $margin * 2; @@ -74,19 +64,18 @@ protected function addMargin($string, $margin, $size) return $xml->asXML(); } - /** - * {@inheritdoc} - */ - public static function getContentType() + public static function getContentType(): string { return 'image/svg+xml'; } - /** - * {@inheritdoc} - */ - public static function getSupportedExtensions() + public static function getSupportedExtensions(): array { return ['svg']; } + + public function getName(): string + { + return 'svg'; + } } diff --git a/src/Writer/WriterInterface.php b/src/Writer/WriterInterface.php index 5395412..2709799 100644 --- a/src/Writer/WriterInterface.php +++ b/src/Writer/WriterInterface.php @@ -13,45 +13,11 @@ interface WriterInterface { - /** - * @param QrCodeInterface $qrCode - * - * @return string - */ - public function writeString(QrCodeInterface $qrCode); - - /** - * @param QrCodeInterface $qrCode - * - * @return string - */ - public function writeDataUri(QrCodeInterface $qrCode); - - /** - * @param QrCodeInterface $qrCode - * @param string $path - */ - public function writeFile(QrCodeInterface $qrCode, $path); - - /** - * @return string - */ - public static function getContentType(); - - /** - * @param string $extension - * - * @return bool - */ - public static function supportsExtension($extension); - - /** - * @return string[] - */ - public static function getSupportedExtensions(); - - /** - * @return string - */ - public function getName(); + public function writeString(QrCodeInterface $qrCode): string; + public function writeDataUri(QrCodeInterface $qrCode): string; + public function writeFile(QrCodeInterface $qrCode, string $path): string; + public static function getContentType(): string; + public static function supportsExtension(string $extension): bool; + public static function getSupportedExtensions(): array; + public function getName(): string; } diff --git a/src/WriterRegistry.php b/src/WriterRegistry.php index c346fe5..7bfea2f 100644 --- a/src/WriterRegistry.php +++ b/src/WriterRegistry.php @@ -14,50 +14,21 @@ class WriterRegistry implements WriterRegistryInterface { - /** - * @var WriterInterface[] - */ - protected $writers; + private $writers = []; + private $defaultWriter; - /** - * @var WriterInterface - */ - protected $defaultWriter; - - public function __construct() - { - $this->writers = []; - } - - /** - * {@inheritdoc} - */ - public function addWriter(WriterInterface $writer, $setAsDefault = false) + public function addWriter(WriterInterface $writer): void { $this->writers[$writer->getName()] = $writer; - - if ($setAsDefault || 1 === count($this->writers)) { - $this->defaultWriter = $writer; - } } - /** - * @param $name - * - * @return WriterInterface - */ - public function getWriter($name) + public function getWriter(string $name): WriterInterface { $this->assertValidWriter($name); return $this->writers[$name]; } - /** - * @return WriterInterface - * - * @throws InvalidWriterException - */ public function getDefaultWriter() { if ($this->defaultWriter instanceof WriterInterface) { @@ -67,23 +38,20 @@ public function getDefaultWriter() throw new InvalidWriterException('Please set the default writer via the second argument of addWriter'); } - /** - * @return WriterInterface[] - */ - public function getWriters() + public function setDefaultWriter(string $name) + { + $this->defaultWriter = $this->writers[$name]; + } + + public function getWriters(): array { return $this->writers; } - /** - * @param string $writer - * - * @throws InvalidWriterException - */ - protected function assertValidWriter($writer) + private function assertValidWriter(string $name) { - if (!isset($this->writers[$writer])) { - throw new InvalidWriterException('Invalid writer "'.$writer.'"'); + if (!isset($this->writers[$name])) { + throw new InvalidWriterException('Invalid writer "'.$name.'"'); } } } diff --git a/src/WriterRegistryInterface.php b/src/WriterRegistryInterface.php index d172eca..4b73fb9 100644 --- a/src/WriterRegistryInterface.php +++ b/src/WriterRegistryInterface.php @@ -13,22 +13,7 @@ interface WriterRegistryInterface { - /** - * @param WriterInterface $writer - * - * @return $this - */ - public function addWriter(WriterInterface $writer); - - /** - * @param $name - * - * @return WriterInterface - */ - public function getWriter($name); - - /** - * @return WriterInterface[] - */ - public function getWriters(); + public function addWriter(WriterInterface $writer): void; + public function getWriter(string $name): WriterInterface; + public function getWriters(): array; } diff --git a/tests/Bundle/Controller/QrCodeControllerTest.php b/tests/Bundle/Controller/QrCodeControllerTest.php deleted file mode 100644 index 4a591d8..0000000 --- a/tests/Bundle/Controller/QrCodeControllerTest.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Endroid\QrCode\Tests\Bundle\Controller; - -use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; -use Symfony\Component\HttpFoundation\Response; - -class QrCodeControllerTest extends WebTestCase -{ - public function testGenerateAction() - { - $client = static::createClient(); - $client->request('GET', $client->getContainer()->get('router')->generate('endroid_qrcode_generate', [ - 'text' => 'Life is too short to be generating QR codes', - 'extension' => 'png', - 'size' => 200, - 'margin' => 10, - 'label' => 'Scan the code', - 'label_font_size' => 16, - ])); - - $response = $client->getResponse(); - $image = imagecreatefromstring($response->getContent()); - - $this->assertTrue(220 == imagesx($image)); - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); - } - - public function testTwigFunctionsAction() - { - $client = static::createClient(); - $client->request('GET', $client->getContainer()->get('router')->generate('endroid_qrcode_twig_functions')); - - $response = $client->getResponse(); - - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); - } -} diff --git a/tests/Bundle/EndroidQrCodeBundleTest.php b/tests/Bundle/EndroidQrCodeBundleTest.php deleted file mode 100644 index f0a1544..0000000 --- a/tests/Bundle/EndroidQrCodeBundleTest.php +++ /dev/null @@ -1,20 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Endroid\QrCode\Tests\Bundle; - -use PHPUnit\Framework\TestCase; - -class EndroidQrCodeBundleTest extends TestCase -{ - public function testNoTestsYet() - { - $this->assertTrue(true); - } -} diff --git a/tests/Bundle/app/.gitignore b/tests/Bundle/app/.gitignore deleted file mode 100644 index 6dd26c5..0000000 --- a/tests/Bundle/app/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/cache -/logs diff --git a/tests/Bundle/app/AppKernel.php b/tests/Bundle/app/AppKernel.php deleted file mode 100644 index d30b3de..0000000 --- a/tests/Bundle/app/AppKernel.php +++ /dev/null @@ -1,36 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -use Symfony\Component\Config\Loader\LoaderInterface; -use Symfony\Component\HttpKernel\Kernel; - -class AppKernel extends Kernel -{ - /** - * {@inheritdoc} - */ - public function registerBundles() - { - $bundles = [ - new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), - new Symfony\Bundle\TwigBundle\TwigBundle(), - new Endroid\QrCode\Bundle\QrCodeBundle\EndroidQrCodeBundle(), - ]; - - return $bundles; - } - - /** - * {@inheritdoc} - */ - public function registerContainerConfiguration(LoaderInterface $loader) - { - $loader->load(__DIR__.'/config/config.yml'); - } -} diff --git a/tests/Bundle/app/bootstrap.php b/tests/Bundle/app/bootstrap.php deleted file mode 100644 index 15c731c..0000000 --- a/tests/Bundle/app/bootstrap.php +++ /dev/null @@ -1,3 +0,0 @@ -create('QR Code', [ @@ -50,7 +50,7 @@ public function testFactory() $this->assertTrue(is_string($pngData)); } - public function testWriteQrCode() + public function testWriteQrCode(): void { $qrCode = new QrCode('QrCode'); @@ -79,7 +79,7 @@ public function testWriteQrCode() $this->assertTrue(0 === strpos($svgDataUriData, 'data:image/svg+xml;base64')); } - public function testSetSize() + public function testSetSize(): void { $size = 400; $margin = 10; @@ -95,7 +95,7 @@ public function testSetSize() $this->assertTrue(imagesy($image) === $size + 2 * $margin); } - public function testSetLabel() + public function testSetLabel(): void { $qrCode = new QrCode('QrCode'); $qrCode @@ -107,12 +107,12 @@ public function testSetLabel() $this->assertTrue(is_string($pngData)); } - public function testSetLogo() + public function testSetLogo(): void { $qrCode = new QrCode('QrCode'); $qrCode ->setSize(400) - ->setLogoPath(__DIR__.'/../assets/symfony.png') + ->setLogoPath(__DIR__.'/../assets/images/symfony.png') ->setLogoWidth(150) ->setValidateResult(true);