| 1: | <?php |
| 2: | /* |
| 3: | * SimpleID |
| 4: | * |
| 5: | * Copyright (C) Kelvin Mo 2021-2025 |
| 6: | * |
| 7: | * This program is free software; you can redistribute it and/or |
| 8: | * modify it under the terms of the GNU General Public |
| 9: | * License as published by the Free Software Foundation; either |
| 10: | * version 2 of the License, or (at your option) any later version. |
| 11: | * |
| 12: | * This program is distributed in the hope that it will be useful, |
| 13: | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14: | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15: | * General Public License for more details. |
| 16: | * |
| 17: | * You should have received a copy of the GNU General Public |
| 18: | * License along with this program; if not, write to the Free |
| 19: | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20: | * |
| 21: | */ |
| 22: | |
| 23: | namespace SimpleID\Util\UI; |
| 24: | |
| 25: | /** |
| 26: | * A trait to implement AttachmentManagerInterface. |
| 27: | * |
| 28: | * @see AttachmentManagerInterface |
| 29: | * |
| 30: | */ |
| 31: | trait AttachmentManagerTrait { |
| 32: | /** @var array<string, array<mixed>> */ |
| 33: | protected $attachments = []; |
| 34: | |
| 35: | /** |
| 36: | * Adds an attachment. |
| 37: | * |
| 38: | * @param string $attachment_type the type of attachment |
| 39: | * @param array<mixed> $data the details of the attachment |
| 40: | * @return AttachmentManagerInterface |
| 41: | */ |
| 42: | public function addAttachment(string $attachment_type, array $data): AttachmentManagerInterface { |
| 43: | if (isset($this->attachments[$attachment_type])) { |
| 44: | $this->attachments[$attachment_type][] = $data; |
| 45: | } else { |
| 46: | $this->attachments[$attachment_type] = [ $data ]; |
| 47: | } |
| 48: | |
| 49: | return $this; |
| 50: | } |
| 51: | |
| 52: | /** |
| 53: | * Retrieves all the attachments. |
| 54: | * |
| 55: | * This function returns an array of all attachments, with the |
| 56: | * key being the attachment type, and the value an array of the |
| 57: | * attachment details. |
| 58: | * |
| 59: | * Note that the value array may contain duplicates. To filter |
| 60: | * for unique values, use the {@link getAttachmentsByType()} method. |
| 61: | * |
| 62: | * @return array<string, array<mixed>> the attachments |
| 63: | */ |
| 64: | public function getAttachments(): array { |
| 65: | return $this->attachments; |
| 66: | } |
| 67: | |
| 68: | /** |
| 69: | * Returns an array of attachment types currently attached to the |
| 70: | * builder. |
| 71: | * |
| 72: | * @return array<string> |
| 73: | */ |
| 74: | public function getAttachmentTypes(): array { |
| 75: | return array_keys($this->attachments); |
| 76: | } |
| 77: | |
| 78: | /** |
| 79: | * Returns the attachments of a particular type. |
| 80: | * |
| 81: | * Only unique elements are returned. |
| 82: | * |
| 83: | * @param string $attachment_type the attachment type |
| 84: | * @return array<array<mixed>> |
| 85: | */ |
| 86: | public function getAttachmentsByType(string $attachment_type): array { |
| 87: | if (!isset($this->attachments[$attachment_type])) return []; |
| 88: | return array_unique($this->attachments[$attachment_type]); |
| 89: | } |
| 90: | |
| 91: | /** |
| 92: | * Merges another attachment manager. |
| 93: | * |
| 94: | * @param AttachmentManagerInterface $manager the attachment manager to merge |
| 95: | * @return void |
| 96: | */ |
| 97: | public function mergeAttachments(AttachmentManagerInterface $manager) { |
| 98: | $this->attachments = array_merge_recursive($this->attachments, $manager->getAttachments()); |
| 99: | } |
| 100: | |
| 101: | /** |
| 102: | * Remove all attachments. |
| 103: | * |
| 104: | * @return void |
| 105: | */ |
| 106: | protected function resetAttachments() { |
| 107: | $this->attachments = []; |
| 108: | } |
| 109: | } |
| 110: | |
| 111: | ?> |