Skip to content

Commit

Permalink
Merge pull request #121 from netolicak/master
Browse files Browse the repository at this point in the history
Added PKPass::addFileContent and PKPass::addLocaleFileContent methods…
  • Loading branch information
tschoffelen authored Dec 30, 2022
2 parents 8981689 + b2434bc commit dddfca2
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/PKPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class PKPass
*/
protected $remote_file_urls = [];

/**
* Holds the files content to include in the pass.
* @var string[]
*/
protected $files_content = [];

/**
* Holds the JSON payload.
* @var object|array
Expand Down Expand Up @@ -250,7 +256,7 @@ public function addRemoteFile($url, $name = null)
* Add a locale file from a url to the remote file urls array.
*
* @param string $language language for which file to be added
* @param string $url URL to file
* @param string $content Content of file
* @param string $name Filename to use in pass archive (default is equal to $url)
*/
public function addLocaleRemoteFile($language, $url, $name = null)
Expand All @@ -259,6 +265,29 @@ public function addLocaleRemoteFile($language, $url, $name = null)
$this->remote_file_urls[$language . '.lproj/' . $name] = $url;
}

/**
* Add a file from a string to the string files array.
*
* @param string $content Content of file
* @param string $name Filename to use in pass archive (default is equal to $url)
*/
public function addFileContent($content, $name)
{
$this->files_content[$name] = $content;
}

/**
* Add a locale file from a string to the string files array.
*
* @param string $language language for which file to be added
* @param string $content Content of file
* @param string $name Filename to use in pass archive (default is equal to $url)
*/
public function addLocaleFileContent($language, $content, $name)
{
$this->files_content[$language . '.lproj/' . $name] = $content;
}

/**
* Create the actual .pkpass file.
*
Expand Down Expand Up @@ -353,6 +382,13 @@ protected function createManifest()
$sha[$name] = sha1(file_get_contents($url));
}

foreach ($this->files_content as $name => $content) {
if (strtolower($name) == 'icon.png') {
$has_icon = true;
}
$sha[$name] = sha1($content);
}

if (!$has_icon) {
throw new PKPassException('Missing required icon.png file.');
}
Expand Down Expand Up @@ -470,6 +506,11 @@ protected function createZip($manifest, $signature)
$download_file = file_get_contents($url);
$zip->addFromString($name, $download_file);
}

foreach ($this->files_content as $name => $content) {
$zip->addFromString($name, $content);
}

$zip->close();

if (!file_exists($filename) || filesize($filename) < 1) {
Expand Down

0 comments on commit dddfca2

Please sign in to comment.