Completed
Push — master ( 1459ee...4560a8 )
by David
03:37
created

BaseModel::formatTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace App\LaravelRestCms;
2
3
use Illuminate\Database\Eloquent\Model;
4
use Illuminate\Foundation\Validation\ValidatesRequests;
5
use Illuminate\Support\Pluralizer;
6
use Illuminate\Support\Singular;
7
8
abstract class BaseModel extends Model {
9
10
	use CacheTrait;
11
	use SearchTrait;
12
	use ValidatesRequests;
13
14
	/**
15
	 * The table name
16
	 * 
17
	 * @var string
18
	 */
19
	protected $table;
20
	
21
	/**
22
	 * The singular version of the table name
23
	 * 
24
	 * @var string
25
	 */
26
	protected $singular;
27
	
28
	/**
29
	 * The plural version of the table name
30
	 * 
31
	* @var string
32
	 */
33
	protected $plural;
34
35
	/**
36
	 * The Validator object
37
	 * 
38
	* @var \Validator
39
	 */
40
	protected $validation;
41
42
	/**
43
	 * Rules to validate when creating a model
44
	 * 
45
	* @var array
46
	 */
47
	protected static $createRules;
48
49
	/**
50
	 * Rules to validate when updating a model
51
	 * 
52
	* @var array
53
	 */
54
	protected static $updateRules;
55
56
	/**
57
     * The "booting" method of the model.
58
     *
59
     * @return void
60
     */
61
    protected static function boot()
62
    {
63
        parent::boot();
64
65
        static::savedEvent();
66
        static::deletingEvent();
67
    }	 
68
	
69
	/**
70
	 * Gets the table name of the model.
71
	 * Override this for custom names to be used in the caching engine.
72
	 * 
73
	 * @return string
74
	 */
75
	public function getTable()
76
	{
77
		return $this->table;
78
	}
79
80
    /**
81
     * Hooks into the "saved" event
82
     * 
83
     * @return void
84
     */
85
    protected static function savedEvent()
86
    {
87
        static::saved(function($model)
88
        {
89
            static::addToCache($model);
90
91
            return true;
92
        });
93
    }
94
95
    /**
96
     * Hooks into the "deleting" event
97
     * 
98
     * @return void
99
     */
100
    protected static function deletingEvent()
101
    {
102
        static::deleting(function($model)
103
        {
104
           static::removeFromCache($model);
105
106
            return true;
107
        });
108
    }
109
	
110
	/**
111
	 * Retrieves the singular name of the table
112
	 * 
113
	 * @param  boolean $format
114
	 * @return string
115
	 */
116
	public function getSingular($format = false)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
	{
118
		if (!isset($this->singular)) {
119
			$this->singular = Pluralizer::singular($this->table);
120
		}
121
122
		return $format ? $this->formatTableName($this->singular) : $this->singular;
123
	}	
124
	
125
	/**
126
	 * Retrieves the plural name of the table
127
	 * 
128
	 * @param  boolean $format
129
	 * @return string
130
	 */
131
	public function getPlural($format = false)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
	{
133
		if (!isset($this->plural)) {
134
			$this->plural = Pluralizer::plural($this->table);
135
		}
136
137
		return $format ? $this->formatTableName($this->plural) : $this->plural;
138
	}
139
	
140
	/**
141
	 * Formats the table name into a human readable format
142
	 * 
143
	 * @param  string $name
144
	 * @return string
145
	 */
146
	protected static function formatTableName($name)
147
	{
148
		return ucwords(str_replace('_', ' ', $name));
149
	}
150
	
151
	/**
152
	 * Validates a model
153
	 * 
154
	 * @param  array  	$data     
155
	 * @param  boolean 	$isUpdate 
156
	 * @return boolean            
157
	 */
158
	protected function validate($data, $isUpdate = false)
159
	{
160
		if ($isUpdate) {
161
			$rules = static::$updateRules;
162
		} else {
163
			$rules = static::$createRules;
164
		}
165
166
		$this->validation = \Validator::make($data, $rules);
167
		
168
		return $this->validation->passes();
169
	}
170
}