Skip to content

Commit

Permalink
[bc] 3x image support
Browse files Browse the repository at this point in the history
  • Loading branch information
eymengunay committed Nov 16, 2016
1 parent 008d571 commit c103b40
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 30 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ PHP-Passbook is a library for creating and packaging passes inside your applicat

## Breaking changes

### Version 1.1.0
### Version 2.0.0

* Pass setRelevantDate now accepts a DateTime object instead of string
* `PassFactory:override` property renamed to `PassFactory:overwrite`
* JMS Serializer dependency removed
* `Image` class `setRetina`/`isRetina` methods replaced with `setDensity`/`getDensity`.

## Installing

Expand All @@ -46,7 +44,7 @@ This example will create a pass of type Ticket and will save the pkpass file in
* [Create a P12 Certificate file](#p12-certificate)
* [Download Apple’s World Wide Developer Relations (WWDR) certificate](#wwdr-certificate)
* [Obtain a Pass Type Identifier and Team Identifier from Apple](#obtaining-the-pass-type-identifier-and-team-id)
* Get an icon (29x29 png file) for the pass
* Get an icon (29x29 png file) for the pass

* Specify a name for your organization

* Specify the output path where the pass will be saved


Expand Down Expand Up @@ -179,7 +177,7 @@ Make sure you download composer.phar in the same folder where the composer.json

Step 2: Install vendors
```
php composer.phar --dev install
php composer.phar install
```

> Note that the script takes some time to finish.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.2.x-dev"
"dev-master": "2.0.x-dev"
}
}
}
14 changes: 7 additions & 7 deletions src/Passbook/Pass/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class Image extends \SplFileObject implements ImageInterface

/**
* All of the pass’s images are loaded using standard UIImage image-loading methods.
* This means, for example, the file name of high resolution version of the image ends with @2x.png.
* @var bool
* This means, for example, the file name of high resolution versions of the image ends with @2x.png/@3x.png.
* @var integer
*/
protected $isRetina;
protected $density;

public function __construct($filename, $context)
{
Expand Down Expand Up @@ -69,18 +69,18 @@ public function getContext()
/**
* {@inheritdoc}
*/
public function setIsRetina($isRetina)
public function setDensity($density)
{
$this->isRetina = $isRetina;
$this->density = $density;

return $this;
}

/**
* {@inheritdoc}
*/
public function isRetina()
public function getDensity()
{
return $this->isRetina;
return $this->density;
}
}
12 changes: 6 additions & 6 deletions src/Passbook/Pass/ImageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public function setContext($context);
public function getContext();

/**
* Sets image is retina
* @param boolean
* Sets image density
* @param integer
*/
public function setIsRetina($isRetina);
public function setDensity($density);

/**
* Returns image is retina
* @param boolean
* Returns image density
* @param integer
*/
public function isRetina();
public function getDensity();
}
18 changes: 11 additions & 7 deletions src/Passbook/PassFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ class PassFactory

/**
* P12 file
*
*
* @var \Passbook\Certificate\P12Interface
*/
protected $p12;

/**
* WWDR file
*
*
* @var \Passbook\Certificate\WWDRInterface
*/
protected $wwdr;
Expand Down Expand Up @@ -106,7 +106,7 @@ public function __construct($passTypeIdentifier, $teamIdentifier, $organizationN
// Create certificate objects
$this->p12 = new P12($p12File, $p12Pass);
$this->wwdr = new WWDR($wwdrFile);

// By default use the PassValidator
$this->passValidator = new PassValidator();
}
Expand Down Expand Up @@ -348,7 +348,7 @@ private function zip($source, $destination)
if (!is_dir($source)) {
throw new FileException("Source must be a directory.");
}

$zip = new ZipArchive();
$shouldOverwrite = $this->isOverwrite() ? ZipArchive::OVERWRITE : 0;
if (!$zip->open($destination, ZipArchive::CREATE | $shouldOverwrite)) {
Expand Down Expand Up @@ -416,7 +416,7 @@ private static function jsonEncode($array)
$options = defined('JSON_UNESCAPED_SLASHES') ? JSON_UNESCAPED_SLASHES : 0;
return json_encode($array, $options);
}

/**
* @param $passName
* @param PassInterface $pass
Expand Down Expand Up @@ -486,8 +486,10 @@ private function prepareImages(PassInterface $pass, $passDir)
/** @var Image $image */
foreach ($pass->getImages() as $image) {
$fileName = $passDir . $image->getContext();
if ($image->isRetina()) {
if ($image->getDensity() === 2) {
$fileName .= '@2x';
} else if ($image->getDensity() === 3) {
$fileName .= '@3x';
}
$fileName .= '.' . $image->getExtension();
copy($image->getPathname(), $fileName);
Expand All @@ -512,8 +514,10 @@ private function prepareLocalizations(PassInterface $pass, $passDir)
// Localization images
foreach ($localization->getImages() as $image) {
$fileName = $localizationDir . $image->getContext();
if ($image->isRetina()) {
if ($image->getDensity() === 2) {
$fileName .= '@2x';
} else if ($image->getDensity() === 3) {
$fileName .= '@3x';
}
$fileName .= '.' . $image->getExtension();
copy($image->getPathname(), $fileName);
Expand Down
15 changes: 12 additions & 3 deletions tests/Passbook/Tests/Pass/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ class ImageTest extends \PHPUnit_Framework_TestCase
public function testImage()
{
$image = new Image(__DIR__.'/../../../img/icon.png', 'thumbnail');
$image->setIsRetina(true);
$image->setDensity(2);

$this->assertEquals($image->getContext(), 'thumbnail');
$this->assertEquals($image->isRetina(), true);
$this->assertEquals($image->getDensity(), 2);
}
}

public function testImage3x()
{
$image = new Image(__DIR__.'/../../../img/icon.png', 'thumbnail');
$image->setDensity(3);

$this->assertEquals($image->getContext(), 'thumbnail');
$this->assertEquals($image->getDensity(), 3);
}
}

0 comments on commit c103b40

Please sign in to comment.