OwnsModels::getForeignKey()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
3
namespace Iatstuti\Database\Support;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
trait OwnsModels
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12
    abstract public function getForeignKey();
13
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    abstract public function getKey();
19
20
21
    /**
22
     * Determine if this model owns the given model.
23
     *
24
     * @param  \Illuminate\Database\Eloquent\Model  $model
25
     * @param  mixed  $foreignKey
26
     * @param  bool  $strict
27
     * @return bool
28
     */
29 6
    public function owns(Model $model, $foreignKey = null, $strict = false)
30
    {
31 6
        $foreignKey = $foreignKey ?: $this->getForeignKey();
32
33 6
        if ($strict) {
34 2
            return $this->getKey() === $model->{$foreignKey};
35
        }
36
37 4
        return $this->getKey() == $model->{$foreignKey};
38
    }
39
40
41
    /**
42
     * Determine if this model doesn't own the given model.
43
     *
44
     * @param  \Illuminate\Database\Eloquent\Model  $model
45
     * @param  mixed  $foreignKey
46
     * @param  bool  $strict
47
     * @return bool
48
     */
49 6
    public function doesntOwn(Model $model, $foreignKey = null, $strict = false)
50
    {
51 6
        return ! $this->owns($model, $foreignKey, $strict);
52
    }
53
}
54