Skip to content

Commit d9e1015

Browse files
committed
Added image file type check to PassValidator. Closes #48.
1 parent 7f7be28 commit d9e1015

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

src/Passbook/PassInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Passbook\Pass\BarcodeInterface;
1515
use Passbook\Pass\BeaconInterface;
16+
use Passbook\Pass\Image;
1617
use Passbook\Pass\ImageInterface;
1718
use Passbook\Pass\LocalizationInterface;
1819
use Passbook\Pass\LocationInterface;
@@ -84,7 +85,7 @@ public function getStructure();
8485
public function addImage(ImageInterface $image);
8586

8687
/**
87-
* {@inheritdoc}
88+
* @return Image[]
8889
*/
8990
public function getImages();
9091

src/Passbook/PassValidator.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Passbook\Pass\Barcode;
66
use Passbook\Pass\Beacon;
7-
use Passbook\Pass\Image;
87
use Passbook\Pass\Location;
98

109
/**
@@ -43,6 +42,7 @@ class PassValidator
4342
const WEB_SERVICE_AUTHENTICATION_TOKEN_INVALID = 'authenticationToken is invalid; must be at least 16 characters';
4443
const ASSOCIATED_STORE_IDENTIFIER_INVALID = 'associatedStoreIdentifiers is invalid; must be an integer';
4544
const ASSOCIATED_STORE_IDENTIFIER_REQUIRED = 'appLaunchURL is required when associatedStoreIdentifiers is present';
45+
const IMAGE_TYPE_INVALID = 'image files must be PNG format';
4646

4747
public function validate(Pass $pass)
4848
{
@@ -53,7 +53,8 @@ public function validate(Pass $pass)
5353
$this->validateLocationKeys($pass);
5454
$this->validateBarcodeKeys($pass);
5555
$this->validateWebServiceKeys($pass);
56-
$this->validateImages($pass);
56+
$this->validateIcon($pass);
57+
$this->validateImageType($pass);
5758
$this->validateAssociatedStoreIdentifiers($pass);
5859

5960
return count($this->errors) === 0;
@@ -189,12 +190,9 @@ private function validateWebServiceKeys(Pass $pass)
189190
}
190191
}
191192

192-
private function validateImages(Pass $pass)
193+
private function validateIcon(PassInterface $pass)
193194
{
194-
$images = $pass->getImages();
195-
196-
foreach ($images as $image) {
197-
/* @var Image $image */
195+
foreach ($pass->getImages() as $image) {
198196
if ($image->getContext() === 'icon') {
199197
return;
200198
}
@@ -203,6 +201,16 @@ private function validateImages(Pass $pass)
203201
$this->addError(self::ICON_REQUIRED);
204202
}
205203

204+
private function validateImageType(PassInterface $pass)
205+
{
206+
foreach ($pass->getImages() as $image) {
207+
$ext = pathinfo($image->getFilename(), PATHINFO_EXTENSION);
208+
if (strcasecmp('png', $ext)) {
209+
$this->addError(self::IMAGE_TYPE_INVALID);
210+
}
211+
}
212+
}
213+
206214
private function validateAssociatedStoreIdentifiers(Pass $pass)
207215
{
208216
//appLaunchURL

tests/Passbook/Tests/PassValidatorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ public function testPassAssociatedStoreIdentifiers()
215215
$this->assertFails($this->pass, PassValidator::ASSOCIATED_STORE_IDENTIFIER_INVALID);
216216
}
217217

218+
public function testPassImageType()
219+
{
220+
$this->assertPasses($this->pass, PassValidator::IMAGE_TYPE_INVALID);
221+
222+
$png = new Image(__DIR__ . '/../../img/icon.PNG', 'icon');
223+
$this->pass->addImage($png);
224+
$this->assertPasses($this->pass, PassValidator::IMAGE_TYPE_INVALID);
225+
226+
$jpg = new Image(__DIR__ . '/../../img/icon.jpg', 'icon');
227+
$this->pass->addImage($jpg);
228+
$this->assertFails($this->pass, PassValidator::IMAGE_TYPE_INVALID);
229+
}
230+
218231
private function assertFails($pass, $expectedError)
219232
{
220233
$validator = new PassValidator();

tests/img/icon.jpg

4.58 KB
Loading

0 commit comments

Comments
 (0)