Elgg  Version master
Attachment.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Email;
4 
6 use Symfony\Component\Mime\Part\DataPart;
7 
11 class Attachment extends DataPart {
12 
30  public static function factory(mixed $options): static {
31  if ($options instanceof \ElggFile) {
33  }
34 
35  if (!is_array($options)) {
36  throw new InvalidArgumentException(__METHOD__ . ': $options needs to be an array');
37  }
38 
39  if (!isset($options['content']) && !isset($options['filepath'])) {
40  throw new InvalidArgumentException(__METHOD__ . ': $options "content" or "filepath" is required');
41  }
42 
43  $content = elgg_extract('content', $options);
44  unset($options['content']);
45  if (!isset($content)) {
46  $filepath = elgg_extract('filepath', $options);
47  if (empty($filepath) || !is_file($filepath)) {
48  throw new InvalidArgumentException(__METHOD__ . ': $options[filepath] didn\'t result in a valid file');
49  }
50 
51  $content = file_get_contents($filepath);
52 
53  if (!isset($options['filename'])) {
54  $options['filename'] = basename($filepath);
55  }
56 
57  if (!isset($options['type'])) {
58  $options['type'] = _elgg_services()->mimetype->getMimeType($filepath);
59  }
60  }
61 
62  $filename = $options['filename'] ?? null;
63  $content_type = $options['type'] ?? null;
64  $encoding = $options['encoding'] ?? null;
65 
66  $attachment = new self($content, $filename, $content_type, $encoding);
67 
68  if (isset($options['id'])) {
69  $id = $options['id'];
70  if (!str_contains($id, '@')) {
71  $id .= '@elgg';
72  }
73 
74  $attachment->setContentId($id);
75  }
76 
77  return $attachment;
78  }
79 
88  public static function fromElggFile(\ElggFile $file): static {
89  if (!$file->exists()) {
90  throw new InvalidArgumentException(__METHOD__ . ': $file doesn\'t exist');
91  }
92 
93  return self::factory([
94  'content' => $file->grabFile(),
95  'type' => $file->getMimeType(),
96  'filename' => basename($file->getFilename()),
97  ]);
98  }
99 }
$content
Set robots.txt action.
Definition: set_robots.php:6
$id
Generic annotation delete action.
Definition: delete.php:6
Email attachment.
Definition: Attachment.php:11
static fromElggFile(\ElggFile $file)
Create an attachment from an ElggFile.
Definition: Attachment.php:88
static factory(mixed $options)
Create an attachment.
Definition: Attachment.php:30
static factory(array $options=[])
Create an email instance form an array of options.
Definition: Email.php:63
Exception thrown if an argument is not of the expected type.
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
_elgg_services()
Get the global service provider.
Definition: elgglib.php:343
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:246
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10