Overview
  • Namespace
  • Class

Namespaces

  • Slack
    • Message

Classes

  • Slack\ApiClient
  • Slack\Bot
  • Slack\Channel
  • Slack\ClientObject
  • Slack\DataObject
  • Slack\DirectMessageChannel
  • Slack\Group
  • Slack\Message\Attachment
  • Slack\Message\AttachmentBuilder
  • Slack\Message\AttachmentField
  • Slack\Message\Message
  • Slack\Message\MessageBuilder
  • Slack\Payload
  • Slack\RealTimeClient
  • Slack\Team
  • Slack\User

Interfaces

  • Slack\ChannelInterface
  • Slack\Exception

Exceptions

  • Slack\ApiException
  • Slack\ConnectionException
  • Slack\UserNotFoundException
  1 <?php
  2 namespace Slack\Message;
  3 
  4 use Slack\DataObject;
  5 
  6 /**
  7  * A message attachment containing rich text data.
  8  *
  9  * @see https://api.slack.com/docs/attachments
 10  */
 11 class Attachment extends DataObject
 12 {
 13     /**
 14      * Creates a new message attachment.
 15      *
 16      * @param string $title    The attachment title.
 17      * @param string $text     The attachment body text.
 18      * @param string $fallback A plain-text summary of the attachment.
 19      */
 20     public function __construct($title, $text, $fallback = null, $color = null, $pretext = null, array $fields = [])
 21     {
 22         $this->data['title'] = $title;
 23         $this->data['text'] = $text;
 24         $this->data['fallback'] = $fallback ?: $text;
 25         $this->data['color'] = $color;
 26         $this->data['pretext'] = $pretext;
 27         $this->data['fields'] = $fields;
 28     }
 29 
 30     /**
 31      * Gets a plain-text summary of the attachment.
 32      *
 33      * @return string A plain-text summary of the attachment.
 34      */
 35     public function getFallbackText()
 36     {
 37         return $this->data['fallback'];
 38     }
 39 
 40     /**
 41      * Gets the attachment border color.
 42      *
 43      * @return string The attachment border color. Can be "good", "warning", "danger", or a hex color code.
 44      */
 45     public function getColor()
 46     {
 47         return isset($this->data['color']) ? $this->data['color'] : null;
 48     }
 49 
 50     /**
 51      * Gets the attachment pretext.
 52      *
 53      * @return string Optional text that appears above the message attachment block.
 54      */
 55     public function getPretext()
 56     {
 57         return isset($this->data['pretext']) ? $this->data['pretext'] : '';
 58     }
 59 
 60     /**
 61      * Gets the author name.
 62      *
 63      * @return string The attachment author's name.
 64      */
 65     public function getAuthorName()
 66     {
 67         return isset($this->data['author_name']) ? $this->data['author_name'] : null;
 68     }
 69 
 70     /**
 71      * Gets the author link.
 72      *
 73      * @return string A link URL for the author.
 74      */
 75     public function getAuthorLink()
 76     {
 77         return isset($this->data['author_link']) ? $this->data['author_link'] : null;
 78     }
 79 
 80     /**
 81      * Gets the author icon.
 82      *
 83      * @return string An icon URL to show next to the author name.
 84      */
 85     public function getAuthorIcon()
 86     {
 87         return isset($this->data['author_icon']) ? $this->data['author_icon'] : null;
 88     }
 89 
 90     /**
 91      * Gets the title.
 92      *
 93      * @return string The attachment title.
 94      */
 95     public function getTitle()
 96     {
 97         return $this->data['title'];
 98     }
 99 
100     /**
101      * Gets the title link.
102      *
103      * @return string A link URL the title should link to.
104      */
105     public function getTitleLink()
106     {
107         return isset($this->data['title_link']) ? $this->data['title_link'] : null;
108     }
109 
110     /**
111      * Gets the attachment body text.
112      *
113      * @return string The attachment body text.
114      */
115     public function getText()
116     {
117         return $this->data['text'];
118     }
119 
120     /**
121      * Gets the image URL.
122      *
123      * @return string A URL to an image to display in the attachment body.
124      */
125     public function getImageUrl()
126     {
127         return isset($this->data['image_url']) ? $this->data['image_url'] : null;
128     }
129 
130     /**
131      * Gets the thumbnail URL.
132      *
133      * @return string A URL to an image to display as a thumbnail.
134      */
135     public function getThumbUrl()
136     {
137         return isset($this->data['thumb_url']) ? $this->data['thumb_url'] : null;
138     }
139 
140     /**
141      * Gets the footer text.
142      *
143      * @return string The footer text.
144      */
145     public function getFooterText()
146     {
147         return isset($this->data['footer']) ? $this->data['footer'] : null;
148     }
149 
150     /**
151      * Gets a URL to an image to show to the left of the footer text.
152      *
153      * @return string The footer icon URL.
154      */
155     public function getFooterIcon()
156     {
157         return isset($this->data['footer_icon']) ? $this->data['footer_icon'] : null;
158     }
159 
160     /**
161      * Gets an extra timestamp value in the footer.
162      *
163      * @return \DateTime The time of the timestamp.
164      */
165     public function getTimestamp()
166     {
167         if (!isset($this->data['ts'])) {
168             return null;
169         }
170 
171         $time = new \DateTime();
172         $time->setTimestamp($this->data['ts']);
173         return $time;
174     }
175 
176     /**
177      * Checks if the attachment has fields.
178      *
179      * @return bool
180      */
181     public function hasFields()
182     {
183         return isset($this->data['fields']) && count($this->data['fields']) > 0;
184     }
185 
186     /**
187      * Gets all the attachment's fields.
188      *
189      * @return AttachmentField[]
190      */
191     public function getFields()
192     {
193         return isset($this->data['fields']) ? $this->data['fields'] : [];
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     public function jsonUnserialize(array $data)
200     {
201         if (!isset($this->data['fields'])) {
202             return;
203         }
204 
205         for ($i = 0; $i < count($this->data['fields']); $i++) {
206             $this->data['fields'][$i] = AttachmentField::fromData($this->data['fields'][$i]);
207         }
208     }
209 }
210 
API documentation generated by ApiGen