From 00cdb74065e9ccc044f47f0e4ec17daaa6ebaf94 Mon Sep 17 00:00:00 2001 From: codemasher Date: Fri, 3 Apr 2020 23:45:44 +0200 Subject: [PATCH] :shower: clean up data interface invocation --- src/QRCode.php | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/QRCode.php b/src/QRCode.php index 64e031f0d..ebdf5fa3a 100755 --- a/src/QRCode.php +++ b/src/QRCode.php @@ -90,6 +90,18 @@ class QRCode{ ], ]; + /** + * Map of data mode => interface + * + * @var string[] + */ + protected const DATA_INTERFACES = [ + 'Number' => Number::class, + 'AlphaNum' => AlphaNum::class, + 'Kanji' => Kanji::class, + 'Byte' => Byte::class, + ]; + /** * @var \chillerlan\QRCode\QROptions|\chillerlan\Settings\SettingsContainerInterface */ @@ -163,21 +175,18 @@ class QRCode{ * @throws \chillerlan\QRCode\Data\QRCodeDataException */ public function initDataInterface(string $data):QRDataInterface{ - $dataModes = ['Number', 'AlphaNum', 'Kanji', 'Byte']; - $dataNamespace = __NAMESPACE__.'\\Data\\'; // allow forcing the data mode // see https://github.com/chillerlan/php-qrcode/issues/39 - if(in_array($this->options->dataMode, $dataModes, true)){ - $dataInterface = $dataNamespace.$this->options->dataMode; + $interface = $this::DATA_INTERFACES[$this->options->dataMode] ?? null; - return new $dataInterface($this->options, $data); + if($interface !== null){ + return new $interface($this->options, $data); } - foreach($dataModes as $mode){ - $dataInterface = $dataNamespace.$mode; + foreach($this::DATA_INTERFACES as $mode => $dataInterface){ - if(call_user_func_array([$this, 'is'.$mode], [$data]) && class_exists($dataInterface)){ + if(call_user_func_array([$this, 'is'.$mode], [$data])){ return new $dataInterface($this->options, $data); }