+ /**
+ * Checks if an entity matches or contains another given entity.
+ * @param Entity $entity
+ * @return bool
+ */
+ public function matchesOrContains(Entity $entity)
+ {
+ $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
+
+ if ($matches) return true;
+
+ if ($entity->isA('chapter') && $this->isA('book')) {
+ return $entity->book_id === $this->id;
+ }
+
+ if ($entity->isA('page') && $this->isA('book')) {
+ return $entity->book_id === $this->id;
+ }
+
+ if ($entity->isA('page') && $this->isA('chapter')) {
+ return $entity->chapter_id === $this->id;
+ }
+
+ return false;
+ }
+