Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Feb 21, 2021
1 parent d81401a commit 31b72cd
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.styleci.yml export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/.github export-ignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
/composer.lock
/phpunit.xml
3 changes: 3 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
preset: laravel

risky: true
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# laravel-vcard
# Laravel vCard
39 changes: 39 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "astrotomic/laravel-vcard",
"type": "library",
"description": "",
"keywords": [],
"homepage": "https://github.com/Astrotomic/laravel-vcard",
"license": "MIT",
"authors": [
{
"name": "Tom Witkowski",
"email": "[email protected]",
"homepage": "https://gummibeer.de",
"role": "Developer"
}
],
"require": {
"php": "^8.0",
"illuminate/contracts": "^7.0 || ^8.0",
"illuminate/http": "^7.0 || ^8.0",
"illuminate/support": "^7.0 || ^8.0",
"illuminate/database": "^7.0 || ^8.0"
},
"require-dev": {
},
"config": {
"sort-packages": true
},
"autoload": {
"psr-4": {
"Astrotomic\\CachableAttributes\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Astrotomic\\CachableAttributes\\Tests\\": "tests/"
}
},
"prefer-stable": true
}
70 changes: 70 additions & 0 deletions src/Vcard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace App\Utils;

use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Stringable;
use Symfony\Component\HttpFoundation\HeaderUtils;

class Vcard implements Stringable, Responsable
{
protected string $name;
protected ?string $email = null;
protected ?string $phone = null;

public static function make(string $name): self
{
return new static($name);
}

public function __construct(string $name)
{
$this->name = $name;
}

public function email(?string $email): self
{
$this->email = $email;

return $this;
}

public function phone(?string $phone): self
{
$this->phone = $phone;

return $this;
}

public function toResponse($request)
{
$content = strval($this);

$filename = Str::of($this->name)->slug('_')->append('.vcf');

return new Response($content, 200, [
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-Type' => 'text/vcard',
'Content-Length' => Str::length($content),
'Content-Disposition' => HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
$filename,
$filename->ascii()->replace('%', '')
),
]);
}

public function __toString(): string
{
return collect([
'BEGIN:VCARD',
'VERSION:4.0',
sprintf('FN:%s', $this->name),
$this->email ? sprintf('EMAIL:%s', $this->email) : null,
$this->phone ? sprintf('TEL;TYPE=VOICE:%s', $this->phone) : null,
'END:VCARD',
])->filter()->implode(PHP_EOL);
}
}

0 comments on commit 31b72cd

Please sign in to comment.