Skip to content

Commit ebab2c9

Browse files
committed
Initial support for attachments
1 parent 13d637c commit ebab2c9

4 files changed

Lines changed: 144 additions & 0 deletions

File tree

src/Client.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public function __get($name)
6363
return $this->model(Type::modelify($name));
6464
}
6565

66+
/**
67+
* Get an instance of the attachment service.
68+
*
69+
* @return \Pace\Services\AttachmentService
70+
*/
71+
public function attachment()
72+
{
73+
return $this->service('AttachmentService');
74+
}
75+
6676
/**
6777
* Clone an object.
6878
*

src/Model.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
use ArrayAccess;
66
use JsonSerializable;
77
use Pace\XPath\Builder;
8+
use Pace\Model\Attachments;
89
use InvalidArgumentException;
910
use UnexpectedValueException;
1011

1112
class Model implements ArrayAccess, JsonSerializable
1213
{
14+
use Attachments;
15+
1316
/**
1417
* The model type.
1518
*

src/Model/Attachments.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Pace\Model;
4+
5+
use BadMethodCallException;
6+
7+
trait Attachments
8+
{
9+
/**
10+
* Attach a file to the model.
11+
*
12+
* @param string $name
13+
* @param string $content
14+
* @param int|string|null $primaryKey
15+
* @return \Pace\Model
16+
*/
17+
public function attach($name, $content, $primaryKey = null)
18+
{
19+
$key = $this->client->attachment()->add($this->type, $this->key($primaryKey), null, $name, $content);
20+
21+
return $this->client->model('FileAttachment')->read($key);
22+
}
23+
24+
/**
25+
* Get the model's attachments.
26+
*
27+
* @return \Pace\KeyCollection
28+
*/
29+
public function attachments()
30+
{
31+
return $this->morphMany('FileAttachment')->get();
32+
}
33+
34+
/**
35+
* Get the file attachment content.
36+
*
37+
* @return string
38+
*/
39+
public function getContent()
40+
{
41+
if ($this->type !== 'FileAttachment') {
42+
throw new BadMethodCallException('Call to method which only exists on FileAttachment');
43+
}
44+
45+
return $this->client->attachment()->getByKey($this->attachment)['content'];
46+
}
47+
}

src/Services/AttachmentService.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Pace\Services;
4+
5+
use Finfo;
6+
use Pace\Service;
7+
8+
class AttachmentService extends Service
9+
{
10+
/**
11+
* Add a new attachment to the vault.
12+
*
13+
* @param string $object
14+
* @param mixed $key
15+
* @param string|null $attribute
16+
* @param string $name
17+
* @param string $content
18+
* @return string
19+
*/
20+
public function add($object, $key, $attribute, $name, $content)
21+
{
22+
$attachment = [
23+
'name' => $name,
24+
'content' => base64_encode($content),
25+
'mimeType' => $this->guessMimeType($name, $content),
26+
'fileExtension' => pathinfo($name, PATHINFO_EXTENSION),
27+
];
28+
29+
$request = [
30+
'in0' => $object,
31+
'in1' => $key,
32+
'in2' => $attribute,
33+
'in3' => $attachment,
34+
];
35+
36+
$response = $this->soap->addAttachment($request);
37+
38+
return $response->out;
39+
}
40+
41+
/**
42+
* Get an attachment from the vault by the specified key.
43+
*
44+
* @param string $key
45+
* @return array
46+
*/
47+
public function getByKey($key)
48+
{
49+
$request = ['in0' => $key];
50+
51+
$response = $this->soap->getAttachmentFromKey($request);
52+
53+
$attachment = (array)$response->out;
54+
$attachment['content'] = base64_decode($attachment['content']);
55+
56+
return $attachment;
57+
}
58+
59+
/**
60+
* Remove an attachment from the vault by the specified key.
61+
*
62+
* @param string $key
63+
*/
64+
public function removeByKey($key)
65+
{
66+
$request = ['in0' => $key];
67+
68+
$this->soap->removeAttachmentFromKey($request);
69+
}
70+
71+
/**
72+
* Guess the MIME type for the specified file.
73+
*
74+
* @param string $name
75+
* @param string $content
76+
* @return string
77+
*/
78+
protected function guessMimeType($name, $content)
79+
{
80+
$finfo = new Finfo(FILEINFO_MIME_TYPE);
81+
82+
return $finfo->buffer($content) ?: 'application/octet-stream';
83+
}
84+
}

0 commit comments

Comments
 (0)