aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Policy/BasicCensor.php
blob: 58664887012c02551c7cb88283bf607217e94f57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Digitigrade\Policy;

use Digitigrade\Model\Actor;
use Digitigrade\Model\Note;
use Digitigrade\Policy;

/**
 * Simple word censorship policy that replaces all instances of some text with another string.
 * 
 * Suffers from the Scunthorpe problem.
 * 
 * Can also be (ab)used to replace some word with another word for humorous effect :3
 */
class BasicCensor extends Policy {
    /** @var string[] */
    private array $words;
    private string $replacement;

    public function __construct(array $words, string $replacement) {
        $this->words = $words;
        $this->replacement = $replacement;
    }

    public function checkNote(Note $note): bool {
        foreach ($this->words as $w) {
            if (isset($note->summary))
                $note->summary = str_replace($w, $this->replacement, $note->summary);
            $note->plainContent = str_replace($w, $this->replacement, $note->plainContent);
            foreach ($note->formattedContent as $type => $content) {
                $note->formattedContent[$type] = str_replace($w, $this->replacement, $content);
            }
        }
        return true;
    }

    public function checkActor(Actor $actor): bool {
        foreach ($this->words as $w) {
            $actor->displayName = str_replace($w, $this->replacement, $actor->displayName);
            if (isset($actor->pronouns))
                $actor->pronouns = str_replace($w, $this->replacement, $actor->pronouns);
            if (isset($actor->bio))
                $actor->bio = str_replace($w, $this->replacement, $actor->bio);
        }
        return true;
    }
}