From 19fc8601cfbc866300f6d642775de783967b8938 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 9 Jun 2023 19:31:40 +0200 Subject: [PATCH] Implement helper trait --- Models/EditorDocListTrait.php | 117 ++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 Models/EditorDocListTrait.php diff --git a/Models/EditorDocListTrait.php b/Models/EditorDocListTrait.php new file mode 100644 index 0000000..212d990 --- /dev/null +++ b/Models/EditorDocListTrait.php @@ -0,0 +1,117 @@ +notes as $note) { + if ($note->type?->id === $type) { + return $note; + } + } + + return new NullEditorDoc(); + } + + /** + * Get all notess by type name + * + * @param string $type EditorDoc type + * + * @return EditorDoc + * + * @since 1.0.0 + */ + public function getEditorDocByTypeName(string $type) : EditorDoc + { + foreach ($this->notes as $note) { + if ($note->type?->name === $type) { + return $note; + } + } + + return new NullEditorDoc(); + } + + /** + * Get all notess by type name + * + * @param string $type EditorDoc type + * + * @return EditorDoc[] + * + * @since 1.0.0 + */ + public function getEditorDocsByTypeName(string $type) : array + { + $notes = []; + foreach ($this->notes as $note) { + if ($note->type?->name === $type) { + $notes[] = $note; + } + } + + return $notes; + } + + /** + * Check if file with a certain type name exists + * + * @param string $type Type name + * + * @return bool + * + * @since 1.0.0 + */ + public function hasEditorDocTypeName(string $type) : bool + { + foreach ($this->notes as $note) { + if ($note->type?->name === $type) { + return true; + } + } + + return false; + } +}