]> BookStack Code Mirror - bookstack/blob - app/Entity.php
Improved empty lists. Fixes #10.
[bookstack] / app / Entity.php
1 <?php
2
3 namespace Oxbow;
4
5 use Illuminate\Database\Eloquent\Model;
6
7 class Entity extends Model
8 {
9     /**
10      * Relation for the user that created this entity.
11      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
12      */
13     public function createdBy()
14     {
15         return $this->belongsTo('Oxbow\User', 'created_by');
16     }
17
18     /**
19      * Relation for the user that updated this entity.
20      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
21      */
22     public function updatedBy()
23     {
24         return $this->belongsTo('Oxbow\User', 'updated_by');
25     }
26
27     /**
28      * Compares this entity to another given entity.
29      * Matches by comparing class and id.
30      * @param $entity
31      * @return bool
32      */
33     public function matches($entity)
34     {
35         return [get_class($this), $this->id] === [get_class($entity), $entity->id];
36     }
37
38     /**
39      * Gets the activity for this entity.
40      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
41      */
42     public function activity()
43     {
44         return $this->morphMany('Oxbow\Activity', 'entity')->orderBy('created_at', 'desc');
45     }
46
47     /**
48      * Allows checking of the exact class, Used to check entity type.
49      * Cleaner method for is_a.
50      * @param $type
51      * @return bool
52      */
53     public function isA($type)
54     {
55         return $this->getName() === strtolower($type);
56     }
57
58     public function getName()
59     {
60         $fullClassName = get_class($this);
61         return strtolower(array_slice(explode('\\', $fullClassName), -1, 1)[0]);
62     }
63
64 }