Skip to content

Commit

Permalink
Added image file type check to PassValidator. Closes #48.
Browse files Browse the repository at this point in the history
  • Loading branch information
g- committed Mar 23, 2016
1 parent 7f7be28 commit d9e1015
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/Passbook/PassInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Passbook\Pass\BarcodeInterface;
use Passbook\Pass\BeaconInterface;
use Passbook\Pass\Image;
use Passbook\Pass\ImageInterface;
use Passbook\Pass\LocalizationInterface;
use Passbook\Pass\LocationInterface;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function getStructure();
public function addImage(ImageInterface $image);

/**
* {@inheritdoc}
* @return Image[]
*/
public function getImages();

Expand Down
22 changes: 15 additions & 7 deletions src/Passbook/PassValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Passbook\Pass\Barcode;
use Passbook\Pass\Beacon;
use Passbook\Pass\Image;
use Passbook\Pass\Location;

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

public function validate(Pass $pass)
{
Expand All @@ -53,7 +53,8 @@ public function validate(Pass $pass)
$this->validateLocationKeys($pass);
$this->validateBarcodeKeys($pass);
$this->validateWebServiceKeys($pass);
$this->validateImages($pass);
$this->validateIcon($pass);
$this->validateImageType($pass);
$this->validateAssociatedStoreIdentifiers($pass);

return count($this->errors) === 0;
Expand Down Expand Up @@ -189,12 +190,9 @@ private function validateWebServiceKeys(Pass $pass)
}
}

private function validateImages(Pass $pass)
private function validateIcon(PassInterface $pass)
{
$images = $pass->getImages();

foreach ($images as $image) {
/* @var Image $image */
foreach ($pass->getImages() as $image) {
if ($image->getContext() === 'icon') {
return;
}
Expand All @@ -203,6 +201,16 @@ private function validateImages(Pass $pass)
$this->addError(self::ICON_REQUIRED);
}

private function validateImageType(PassInterface $pass)
{
foreach ($pass->getImages() as $image) {
$ext = pathinfo($image->getFilename(), PATHINFO_EXTENSION);
if (strcasecmp('png', $ext)) {
$this->addError(self::IMAGE_TYPE_INVALID);
}
}
}

private function validateAssociatedStoreIdentifiers(Pass $pass)
{
//appLaunchURL
Expand Down
13 changes: 13 additions & 0 deletions tests/Passbook/Tests/PassValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ public function testPassAssociatedStoreIdentifiers()
$this->assertFails($this->pass, PassValidator::ASSOCIATED_STORE_IDENTIFIER_INVALID);
}

public function testPassImageType()
{
$this->assertPasses($this->pass, PassValidator::IMAGE_TYPE_INVALID);

$png = new Image(__DIR__ . '/../../img/icon.PNG', 'icon');
$this->pass->addImage($png);
$this->assertPasses($this->pass, PassValidator::IMAGE_TYPE_INVALID);

$jpg = new Image(__DIR__ . '/../../img/icon.jpg', 'icon');
$this->pass->addImage($jpg);
$this->assertFails($this->pass, PassValidator::IMAGE_TYPE_INVALID);
}

private function assertFails($pass, $expectedError)
{
$validator = new PassValidator();
Expand Down
Binary file added tests/img/icon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d9e1015

Please sign in to comment.