generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to Introduce QR Code Types and Factory
Removed `QrCodeCommand` and added new types: `PhoneNumberQrCode`, `EmailQrCode`, `WifiQrCode`, and `TextQrCode`. Implemented a `QrCodeFactory` for instantiating these types. Improved `QrCode` class with configurable parameters and added comprehensive tests to validate functionality and error handling.
- Loading branch information
Dominik Eller
committed
Sep 20, 2024
1 parent
a3891c7
commit d8872de
Showing
20 changed files
with
583 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Deller\QrCode\Factories; | ||
|
||
use Deller\QrCode\QrCode; | ||
use Deller\QrCode\Types\EmailQrCode; | ||
use Deller\QrCode\Types\PhoneNumberQrCode; | ||
use Deller\QrCode\Types\TextQrCode; | ||
use Deller\QrCode\Types\UrlQrCode; | ||
use InvalidArgumentException; | ||
|
||
class QrCodeFactory | ||
{ | ||
protected static $types = [ | ||
'url' => UrlQrCode::class, | ||
'text' => TextQrCode::class, | ||
'email' => EmailQrCode::class, | ||
'phone' => PhoneNumberQrCode::class, | ||
]; | ||
|
||
/** | ||
* Create a QR code generator based on type. | ||
* | ||
* @param string $type | ||
* @return QrCode | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function create(string $type): QrCode | ||
{ | ||
if (!array_key_exists($type, self::$types)) { | ||
throw new InvalidArgumentException("QR code type [$type] is not supported."); | ||
} | ||
|
||
$className = self::$types[$type]; | ||
|
||
return new $className(); | ||
} | ||
|
||
/** | ||
* Register a custom QR code type. | ||
* | ||
* @param string $type | ||
* @param string $class | ||
* @return void | ||
*/ | ||
public static function registerType(string $type, string $class) | ||
{ | ||
self::$types[$type] = $class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Deller\QrCode\Types; | ||
|
||
use Deller\QrCode\QrCode; | ||
|
||
class EmailQrCode extends QrCode | ||
{ | ||
protected $email; | ||
|
||
/** | ||
* Set the email address for the QR code. | ||
* | ||
* @param string $email | ||
* @return $this | ||
*/ | ||
public function setEmail(string $email) | ||
{ | ||
$this->email = $email; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Return the formatted email data for the QR code. | ||
* | ||
* @return string | ||
*/ | ||
public function getData(): string | ||
{ | ||
return "mailto:" . $this->email; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Deller\QrCode\Types; | ||
|
||
use Deller\QrCode\QrCode; | ||
|
||
class PhoneNumberQrCode extends QrCode | ||
{ | ||
protected $phoneNumber; | ||
|
||
/** | ||
* Set the phone number for the QR code. | ||
* | ||
* @param string $phoneNumber | ||
* @return $this | ||
*/ | ||
public function setPhoneNumber(string $phoneNumber) | ||
{ | ||
$this->phoneNumber = $phoneNumber; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Return the formatted phone number data for the QR code. | ||
* | ||
* @return string | ||
*/ | ||
public function getData(): string | ||
{ | ||
return "tel:" . $this->phoneNumber; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Deller\QrCode\Types; | ||
|
||
use Deller\QrCode\QrCode; | ||
|
||
class TextQrCode extends QrCode | ||
{ | ||
protected $text; | ||
|
||
public function setText(string $text) | ||
{ | ||
$this->text = $text; | ||
return $this; | ||
} | ||
|
||
public function getData(): string | ||
{ | ||
return $this->text; | ||
} | ||
} |
Oops, something went wrong.