From 261ab1364aa40384b0315be60e78cd89ea7fe661 Mon Sep 17 00:00:00 2001 From: winter Date: Sun, 23 Feb 2025 18:32:09 +0000 Subject: implement policies --- Digitigrade/PolicyManager.php | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Digitigrade/PolicyManager.php (limited to 'Digitigrade/PolicyManager.php') diff --git a/Digitigrade/PolicyManager.php b/Digitigrade/PolicyManager.php new file mode 100644 index 0000000..24c2c7f --- /dev/null +++ b/Digitigrade/PolicyManager.php @@ -0,0 +1,46 @@ +policies[] = $policy; + } + + public function registerAll(Policy ...$policies) { + foreach ($policies as $p) { + $this->register($p); + } + } + + /** + * Checks if an object is allowed by all policies and possibly alters it. + * @param FetchableModel $object the object to check (may be modified in-place) + * @return bool true if allowed, false if it should be dropped + */ + public function check(FetchableModel $object): bool { + return match ($object::class) { + Actor::class => self::arrayAll($this->policies, fn(Policy $p) => $p->checkActor($object)), + Interaction::class => self::arrayAll($this->policies, fn(Policy $p) => $p->checkInteraction($object)), + Note::class => self::arrayAll($this->policies, fn(Policy $p) => $p->checkNote($object)), + default => true + }; + } + + private static function arrayAll(array $arr, callable $test): bool { + // i would use array_all but that's php 8.4+ only which i don't have lol + foreach ($arr as $item) { + if (!$test($item)) { + return false; + } + } + return true; + } +} \ No newline at end of file -- cgit v1.3