Completed
Push — master ( ac13d3...9f68b4 )
by
unknown
01:57
created

ForumCategory::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Taskforcedev\LaravelForum\Models;
4
5
use \Validator;
6
7
/**
8
 * Class ForumCategory
9
 *
10
 * @property integer $id   Category ID.
11
 * @property string  $name Category Name.
12
 *
13
 * @package Taskforcedev\LaravelForum\Models
14
 */
15
class ForumCategory extends AbstractModel
16
{
17
    public $table = 'forum_categories';
18
19
    public $fillable = ['name'];
20
21
    /**
22
     * Eloquent Relation.
23
     * @return mixed
24
     */
25
    public function forums()
26
    {
27
        return $this->hasMany('Taskforcedev\LaravelForum\Forum', 'category_id', 'id');
28
    }
29
30
    /**
31
     * Is model data valid.
32
     * @param array|object $data The data to validate.
33
     * @return boolean
34
     */
35
    public static function valid($data)
36
    {
37
        $rules = [
38
            'name' => ['required', 'min:3'],
39
        ];
40
        $validator = Validator::make($data, $rules);
41
42
        if ($validator->passes()) {
43
            return true;
44
        }
45
        return false;
46
    }
47
48
    /**
49
     * Returns the category as an html option.
50
     * @return string
51
     */
52
    public function toOption()
53
    {
54
        return "<option value=\"{$this->id}\">{$this->name}</option>";
55
    }
56
57
    /**
58
     * Return the category name as a string.
59
     * @return string
60
     */
61
    public function __toString()
62
    {
63
        return (string)$this->name;
64
    }
65
}
66