Completed
Pull Request — master (#280)
by Carl
03:08
created

PrimarySlug::primarySlug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Cviebrock\EloquentSluggable;
4
5
use Cviebrock\EloquentSluggable\Sluggable;
6
7
trait PrimarySlug {
8
9
    /**
10
     * Primary slug column of this model.
11
     *
12
     * @return string
13
     */
14
    public function primarySlug(){
15
        if(isset($this->primarySlug)){
16
            return $this->primarySlug;
0 ignored issues
show
Bug introduced by
The property primarySlug does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
        }
18
        return 'slug';
19
    }
20
//
21
//    /**
22
//     * how to map the primary slug to the router
23
//     *
24
//     * @return string
25
//     */
26
//    public function getRouteKeyName() {
27
//        return $this->primarySlug();
28
//    }
29
//
30
//    /**
31
//     * Return the sluggable configuration array for this model.
32
//     *
33
//     * @return array
34
//     */
35
//    public function sluggable() {
36
//        $config = (method_exists(get_parent_class($this), 'getConfig')) ? parent::sluggable() : [];
37
//        return array_merge($config, [
38
//            $this->primarySlug() => $this->primarySlugConfig()
39
//        ]);
40
//    }
41
42
    /**
43
     * Query scope for finding a model by its slug.
44
     * @param $scope
45
     * @param $slug
46
     * @return mixed
47
     */
48
    public function scopeWhereSlug($scope, $slug) {
49
        return $scope->where($this->primarySlug(), $slug);
50
    }
51
52
    /**
53
     * Find a model by slug.
54
     * @param $slug
55
     * @return \Illuminate\Database\Eloquent\Model|null
56
     */
57
    public static function findBySlug($slug) {
58
        return static::whereSlug($slug)->first();
0 ignored issues
show
Bug introduced by
The method whereSlug() does not exist on Cviebrock\EloquentSluggable\PrimarySlug. Did you maybe mean scopeWhereSlug()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
59
    }
60
61
    /**
62
     * Find a model by slug or fail.
63
     * @param $slug
64
     * @return \Illuminate\Database\Eloquent\Model
65
     */
66
    public static function findBySlugOrFail($slug) {
67
        return static::whereSlug($slug)->firstOrFail();
0 ignored issues
show
Bug introduced by
The method whereSlug() does not exist on Cviebrock\EloquentSluggable\PrimarySlug. Did you maybe mean scopeWhereSlug()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
68
    }
69
70
    /**
71
     * Simple find by Id if it's numeric or slug if not. Fail if not found.
72
     * @param $slug
73
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection
74
     */
75
    public static function findBySlugOrIdOrFail($slug) {
76
        return static::findBySlug($slug) ?: static::findOrFail((int)$slug);
77
    }
78
79
    /**
80
     * Simple find by Id if it's numeric or slug if not.
81
     * @param $slug
82
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection|null
83
     */
84
    public static function findBySlugOrId($slug) {
85
        return  static::findBySlug($slug) ?: static::find($slug);
86
    }
87
}
88