aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Policy/BasicCensor.php
diff options
context:
space:
mode:
authorwinter2025-02-23 18:32:09 +0000
committerwinter2025-02-23 18:32:09 +0000
commit261ab1364aa40384b0315be60e78cd89ea7fe661 (patch)
treebf114af430c761abfaf33e814e3cf9ba0ed096d2 /Digitigrade/Policy/BasicCensor.php
parent1ba56a5b687e6c906c2b24e5b37df119ef5752e3 (diff)
implement policies
Diffstat (limited to 'Digitigrade/Policy/BasicCensor.php')
-rw-r--r--Digitigrade/Policy/BasicCensor.php47
1 files changed, 47 insertions, 0 deletions
diff --git a/Digitigrade/Policy/BasicCensor.php b/Digitigrade/Policy/BasicCensor.php
new file mode 100644
index 0000000..5866488
--- /dev/null
+++ b/Digitigrade/Policy/BasicCensor.php
@@ -0,0 +1,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;
+ }
+} \ No newline at end of file