Skip to content

Commit c10caea

Browse files
Merge pull request #80 from therouv/master
Save Path for saving vcards on disk
2 parents 0bef369 + 54d12d6 commit c10caea

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ php:
44
- 5.4
55
- 5.5
66
- 5.6
7+
- 7.0
78
- hhvm
89

910
sudo: false

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ $vcard->addPhoto(__DIR__ . '/landscape.jpeg');
5050

5151
// return vcard as a download
5252
return $vcard->download();
53+
54+
// save vcard on disk
55+
//$vcard->setSavePath('/path/to/directory');
56+
//$vcard->save();
57+
5358
```
5459

5560
> [View all examples](/examples/example.php) or check [the VCard class](/src/VCard.php).
@@ -116,4 +121,4 @@ More info on how to work with GitHub on help.github.com.
116121

117122
## License
118123

119-
The module is licensed under [MIT](./LICENSE.md). In short, this license allows you to do everything as long as the copyright statement stays present.
124+
The module is licensed under [MIT](./LICENSE.md). In short, this license allows you to do everything as long as the copyright statement stays present.

src/VCard.php

+32
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class VCard
3232
*/
3333
private $filename;
3434

35+
/**
36+
* Save Path
37+
*
38+
* @var string
39+
*/
40+
private $savePath = null;
41+
3542
/**
3643
* Multiple properties for element allowed
3744
*
@@ -738,6 +745,11 @@ public function save()
738745
{
739746
$file = $this->getFilename() . '.' . $this->getFileExtension();
740747

748+
// Add save path if given
749+
if (null !== $this->savePath) {
750+
$file = $this->savePath . $file;
751+
}
752+
741753
file_put_contents(
742754
$file,
743755
$this->getOutput()
@@ -792,6 +804,26 @@ public function setFilename($value, $overwrite = true, $separator = '_')
792804
$value : $this->filename . $separator . $value;
793805
}
794806

807+
/**
808+
* Set the save path directory
809+
*
810+
* @param string $savePath Save Path
811+
* @throws Exception
812+
*/
813+
public function setSavePath($savePath)
814+
{
815+
if (!is_dir($savePath)) {
816+
throw new Exception('Output directory does not exist.');
817+
}
818+
819+
// Add trailing directory separator the save path
820+
if (substr($savePath, -1) != DIRECTORY_SEPARATOR) {
821+
$savePath .= DIRECTORY_SEPARATOR;
822+
}
823+
824+
$this->savePath = $savePath;
825+
}
826+
795827
/**
796828
* Set property
797829
*

0 commit comments

Comments
 (0)