]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Models/Book.php
Fixes for CodeStyle vol.2
[bookstack] / app / Entities / Models / Book.php
index 5a7bae0ce90f2a3054f593b155d793f219ea1183..df30c1c714abb4fff2ddd9d92e2b3c4fa6119623 100644 (file)
@@ -1,10 +1,7 @@
-<?php namespace BookStack\Entities\Models;
+<?php
+
+namespace BookStack\Entities\Models;
 
-use BookStack\Entities\Models\Bookshelf;
-use BookStack\Entities\Models\Chapter;
-use BookStack\Entities\Models\Entity;
-use BookStack\Entities\Models\HasCoverImage;
-use BookStack\Entities\Models\Page;
 use BookStack\Uploads\Image;
 use Exception;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -13,9 +10,10 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
 use Illuminate\Support\Collection;
 
 /**
- * Class Book
- * @property string $description
- * @property int $image_id
+ * Class Book.
+ *
+ * @property string     $description
+ * @property int        $image_id
  * @property Image|null $cover
  */
 class Book extends Entity implements HasCoverImage
@@ -23,25 +21,22 @@ class Book extends Entity implements HasCoverImage
     public $searchFactor = 2;
 
     protected $fillable = ['name', 'description'];
-    protected $hidden = ['restricted', 'pivot', 'image_id'];
+    protected $hidden = ['restricted', 'pivot', 'image_id', 'deleted_at'];
 
     /**
      * Get the url for this book.
-     * @param string|bool $path
-     * @return string
      */
-    public function getUrl($path = false)
+    public function getUrl(string $path = ''): string
     {
-        if ($path !== false) {
-            return url('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
-        }
-        return url('/books/' . urlencode($this->slug));
+        return url('/books/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
     }
 
     /**
      * Returns book cover image, if book cover not exists return default cover image.
-     * @param int $width - Width of the image
+     *
+     * @param int $width  - Width of the image
      * @param int $height - Height of the image
+     *
      * @return string
      */
     public function getBookCover($width = 440, $height = 250)
@@ -56,11 +51,12 @@ class Book extends Entity implements HasCoverImage
         } catch (Exception $err) {
             $cover = $default;
         }
+
         return $cover;
     }
 
     /**
-     * Get the cover image of the book
+     * Get the cover image of the book.
      */
     public function cover(): BelongsTo
     {
@@ -77,6 +73,7 @@ class Book extends Entity implements HasCoverImage
 
     /**
      * Get all pages within this book.
+     *
      * @return HasMany
      */
     public function pages()
@@ -86,6 +83,7 @@ class Book extends Entity implements HasCoverImage
 
     /**
      * Get the direct child pages of this book.
+     *
      * @return HasMany
      */
     public function directPages()
@@ -95,6 +93,7 @@ class Book extends Entity implements HasCoverImage
 
     /**
      * Get all chapters within this book.
+     *
      * @return HasMany
      */
     public function chapters()
@@ -104,6 +103,7 @@ class Book extends Entity implements HasCoverImage
 
     /**
      * Get the shelves this book is contained within.
+     *
      * @return BelongsToMany
      */
     public function shelves()
@@ -113,23 +113,14 @@ class Book extends Entity implements HasCoverImage
 
     /**
      * Get the direct child items within this book.
+     *
      * @return Collection
      */
     public function getDirectChildren(): Collection
     {
         $pages = $this->directPages()->visible()->get();
         $chapters = $this->chapters()->visible()->get();
-        return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
-    }
 
-    /**
-     * Get an excerpt of this book's description to the specified length or less.
-     * @param int $length
-     * @return string
-     */
-    public function getExcerpt(int $length = 100)
-    {
-        $description = $this->description;
-        return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
+        return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
     }
 }