From 6ffc3f7990c98e3993e44f0d9e1c7ac50ec1d683 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Fri, 25 Aug 2017 12:27:15 +0200 Subject: [PATCH] remove Yii2 requirements when used as standalone --- src/Exception/InvalidConfigException.php | 17 +++++++++++++++++ src/Format/BookMarkFormat.php | 4 ++-- src/Format/WifiFormat.php | 2 +- src/Format/vCardFormat.php | 2 +- src/Traits/EmailTrait.php | 9 ++++----- src/Traits/UrlTrait.php | 9 ++++----- tests/FormatsTest.php | 16 ++++++++-------- 7 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 src/Exception/InvalidConfigException.php diff --git a/src/Exception/InvalidConfigException.php b/src/Exception/InvalidConfigException.php new file mode 100644 index 0000000..ebc65f1 --- /dev/null +++ b/src/Exception/InvalidConfigException.php @@ -0,0 +1,17 @@ + +* +* For the full copyright and license information, please view +* the LICENSE file that was distributed with this source code. +*/ + +namespace Da\QrCode\Exception; + +class InvalidConfigException extends Exception +{ + +} diff --git a/src/Format/BookMarkFormat.php b/src/Format/BookMarkFormat.php index d32dbcf..594ff63 100644 --- a/src/Format/BookMarkFormat.php +++ b/src/Format/BookMarkFormat.php @@ -11,8 +11,8 @@ namespace Da\QrCode\Format; +use Da\QrCode\Exception\InvalidConfigException; use Da\QrCode\Traits\UrlTrait; -use yii\base\InvalidConfigException; /** * Class BookMark formats a string to properly create a Bookmark QrCode @@ -33,7 +33,7 @@ class BookMarkFormat extends AbstractFormat /** * @inheritdoc - * @throws \yii\base\InvalidConfigException + * @throws InvalidConfigException */ public function init() { diff --git a/src/Format/WifiFormat.php b/src/Format/WifiFormat.php index c7dc9c0..16aea41 100644 --- a/src/Format/WifiFormat.php +++ b/src/Format/WifiFormat.php @@ -11,7 +11,7 @@ namespace Da\QrCode\Format; -use yii\base\InvalidConfigException; +use Da\QrCode\Exception\InvalidConfigException; /** * Class Wifi formats a string to properly create a Wifi QrCode diff --git a/src/Format/vCardFormat.php b/src/Format/vCardFormat.php index 79379c6..2876599 100644 --- a/src/Format/vCardFormat.php +++ b/src/Format/vCardFormat.php @@ -11,9 +11,9 @@ namespace Da\QrCode\Format; +use Da\QrCode\Exception\InvalidConfigException; use Da\QrCode\Traits\EmailTrait; use Da\QrCode\Traits\UrlTrait; -use yii\base\InvalidConfigException; /** * Class vCard creates a valid vCard 4.0 QrCode string diff --git a/src/Traits/EmailTrait.php b/src/Traits/EmailTrait.php index 4977666..1028150 100644 --- a/src/Traits/EmailTrait.php +++ b/src/Traits/EmailTrait.php @@ -11,8 +11,7 @@ namespace Da\QrCode\Traits; -use yii\base\InvalidConfigException; -use yii\validators\EmailValidator; +use Da\QrCode\Exception\InvalidConfigException; /** * EmailTrait @@ -39,9 +38,9 @@ trait EmailTrait public function setEmail($value) { $error = null; - $validator = new EmailValidator(); - if (!$validator->validate($value, $error)) { - throw new InvalidConfigException($error); + + if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { + throw new InvalidConfigException('Email seems incorrect.'); } $this->email = $value; diff --git a/src/Traits/UrlTrait.php b/src/Traits/UrlTrait.php index 6c4c9f5..dce7b8d 100644 --- a/src/Traits/UrlTrait.php +++ b/src/Traits/UrlTrait.php @@ -11,8 +11,7 @@ namespace Da\QrCode\Traits; -use yii\base\InvalidConfigException; -use yii\validators\UrlValidator; +use Da\QrCode\Exception\InvalidConfigException; trait UrlTrait { @@ -29,9 +28,9 @@ trait UrlTrait public function setUrl($value) { $error = null; - $validator = new UrlValidator(); - if (!$validator->validate($value, $error)) { - throw new InvalidConfigException($error); + + if (!filter_var($value, FILTER_VALIDATE_URL)) { + throw new InvalidConfigException('Url seems invalid.'); } $this->url = $value; diff --git a/tests/FormatsTest.php b/tests/FormatsTest.php index f8366b6..d35a157 100644 --- a/tests/FormatsTest.php +++ b/tests/FormatsTest.php @@ -35,13 +35,13 @@ public function testBookMark() $this->assertEquals("http://2amigos.us", $bookmark->getUrl()); // using __toString() $this->assertEquals("MEBKM:TITLE:test-title;URL:http://2amigos.us;;", $bookmark); - $this->expectException('yii\base\InvalidConfigException'); + $this->expectException('Da\QrCode\Exception\InvalidConfigException'); $bookmark->url = 'wrong!url'; } public function testBookMarkFailed() { - $this->expectException('yii\base\InvalidConfigException'); + $this->expectException('Da\QrCode\Exception\InvalidConfigException'); $bookmark = new BookMarkFormat(); } @@ -57,7 +57,7 @@ public function testMailMessage() $this->assertEquals("hola@2amigos.us", $message->getEmail()); $this->assertEquals("MATMSG:TO:hola@2amigos.us;SUB:test;BODY:test-body;;", $message->getText()); - $this->expectException('yii\base\InvalidConfigException'); + $this->expectException('Da\QrCode\Exception\InvalidConfigException'); $message = new MailMessageFormat(['email' => 'wrongaddress!!']); } @@ -71,7 +71,7 @@ public function testMailTo() public function testMailToWrongEmail() { - $this->expectException('yii\base\InvalidConfigException'); + $this->expectException('Da\QrCode\Exception\InvalidConfigException'); $mailTo = new MailToFormat(['email' => 'wrongaddress-@...']); } @@ -94,7 +94,7 @@ public function testMeCard() "NOTE:test-note;\nBDAY:19711201;\nADR:test-address;\nURL:http://2amigos.us;\nNICKNAME:tonydspaniard;\n;"; $this->assertEquals($expected, $card->getText()); - $this->expectException('yii\base\InvalidConfigException'); + $this->expectException('Da\QrCode\Exception\InvalidConfigException'); $card->email = 'wrongaddress!!!'; $card->getText(); @@ -152,7 +152,7 @@ public function testVCard() $this->assertEquals($expected, $vcard->getText()); - $this->expectException('yii\base\InvalidConfigException'); + $this->expectException('Da\QrCode\Exception\InvalidConfigException'); $vcard->email = "wrongaddress"; } @@ -175,7 +175,7 @@ public function testVCardPhoto() $vcard->photo = 'wrongimage.superb'; - $this->expectException('yii\base\InvalidConfigException'); + $this->expectException('Da\QrCode\Exception\InvalidConfigException'); $method->invoke($vcard); } @@ -185,7 +185,7 @@ public function testWifi() $this->assertEquals("WIFI:T:WPA;S:testSSID;P:HAKUNAMATATA;;", $wifi->getText()); $wifi->hidden = 'true'; $this->assertEquals("WIFI:T:WPA;S:testSSID;P:HAKUNAMATATA;H:true;", $wifi->getText()); - $this->expectException('yii\base\InvalidConfigException'); + $this->expectException('Da\QrCode\Exception\InvalidConfigException'); $wifi = new WifiFormat(['authentication' => 'WPA', 'password' => 'HAKUNAMATATA']); }