1: <?php
2: /**
3: * Created by PhpStorm.
4: * User: theo
5: * Date: 9/1/17
6: * Time: 1:53 PM
7: */
8:
9: namespace Jlab\Eloglib;
10:
11: use XMLWriter;
12:
13: /**
14: * Class Attachment
15: *
16: * @package Jlab\Eloglib
17: */
18: abstract class Attachment
19: {
20: /**
21: * Attachment content.
22: *
23: * Can be base64-encoded bytes of the file or just a URL
24: *
25: * @var string
26: */
27: protected $data;
28:
29: /**
30: * Attachment MIME-TYPE.
31: *
32: * @var string
33: */
34: protected $type;
35:
36:
37: /**
38: * Indicates the format of $data.
39: *
40: * Valid values are "base64" or "url"
41: *
42: * @var string
43: *
44: */
45: protected $encoding;
46:
47:
48: /**
49: * Text describing the attachment
50: * @var string username
51: *
52: */
53: protected $caption;
54:
55: /**
56: * Return Attachment object as an XML DOMDocument
57: *
58: * @param string $name A name to use for the DOMElement being returned.
59: *
60: * @return string
61: */
62: function getXML($name = 'Attachment')
63: {
64: $xw = new xmlWriter();
65: $xw->openMemory();
66: $xw->setIndent(true);
67: $xw->startElement($name);
68: $xw->writeElement('caption', $this->getCaption());
69: $xw->writeElement('type', $this->type);
70: $xw->writeElement('filename', $this->filename);
71: $this->xmlWriteData($xw);
72: $xw->endElement();
73: return $xw->outputMemory(true);
74: }
75:
76: /**
77: * Write the data and its encoding attribute to XML.
78: * @param \xmlWriter $xw
79: */
80: protected function xmlWriteData(xmlWriter $xw){
81: $xw->startElement('data');
82: $xw->writeAttribute('encoding', $this->encoding);
83: $xw->text($this->data);
84: $xw->endElement();
85: }
86:
87:
88: /**
89: * Returns the caption text if it is set, or the base filename
90: * of the attachment if it is not.
91: *
92: * This method can be called explicitly, but is also an interceptor method
93: * that will get invoked when a client reads $this->caption variable.
94: *
95: * @see __get()
96: */
97: function getCaption()
98: {
99: if ($this->caption) {
100: return $this->caption;
101: } else {
102: return basename($this->filename);
103: }
104: }
105:
106: /**
107: * Magic method allows controlled access to class properties
108: * by checking to see if a "getVariableName()" method exists, and if so,
109: * invoking it in lieu of direct read of "variableName".
110: *
111: * @param string $var
112: * @return mixed
113: */
114: function __get($var)
115: {
116: if (property_exists($this, $var)) {
117: $getterFunction = 'get' . ucfirst($var);
118: if (method_exists($this, $getterFunction)) {
119: return $this->$getterFunction();
120: } else {
121: return $this->$var;
122: }
123: }
124:
125: $trace = debug_backtrace();
126: trigger_error(
127: 'Undefined property via __get(): ' . $var .
128: ' in ' . $trace[0]['file'] .
129: ' on line ' . $trace[0]['line'], E_USER_NOTICE);
130: return null;
131: }
132:
133:
134: }