Completed
Pull Request — master (#4205)
by Craig
09:54 queued 03:35
created

UserAccountDisplayEvent::addContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\UsersModule\Event;
15
16
/**
17
 * A UI event that is triggered when a user's account detail is viewed. This allows another module
18
 * to intercept the display of the user account detail in order to add its own information.
19
 * To add content to the user account detail, render output and add it to this event.
20
 *
21
 * When rendering this events output in a template, simply render the event itself
22
 * and the magic __toString() method will take care of the rest:
23
 *
24
 * {% set userAccountDisplayEvent = dispatchEvent('Zikula\\UsersModule\\Event\\UserAccountDisplayEvent', [userEntity]) %}
25
 * {{ userAccountDisplayEvent|raw }}
26
 */
27
class UserAccountDisplayEvent extends UserEntityEvent
28
{
29
    /**
30
     * @var array
31
     */
32
    private $contents = [];
33
34
    public function addContent(string $key = null, string $content = ''): void
35
    {
36
        $this->contents[$key] = $content;
37
    }
38
39
    public function getContents(): array
40
    {
41
        return $this->contents;
42
    }
43
44
    public function getContent(string $key): string
45
    {
46
        if (isset($this->contents[$key])) {
47
            return $this->contents[$key];
48
        }
49
50
        return '';
51
    }
52
53
    public function __toString(): string
54
    {
55
        $contents = '';
56
        foreach ($this->contents as $content) {
57
            $contents .= $content;
58
        }
59
60
        return $contents;
61
    }
62
}
63