1 | <?php |
||
10 | class OracleEloquent extends Model |
||
11 | { |
||
12 | /** |
||
13 | * List of binary (blob) columns |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $binaries = []; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $wrapBinaries = []; |
||
23 | |||
24 | /** |
||
25 | * Sequence name variable |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $sequence = null; |
||
30 | |||
31 | /** |
||
32 | * Get model's sequence name |
||
33 | * |
||
34 | * @return string |
||
35 | */ |
||
36 | public function getSequenceName() |
||
37 | { |
||
38 | if ($this->sequence) { |
||
39 | return $this->sequence; |
||
40 | } |
||
41 | |||
42 | return $this->getTable() . '_' . $this->getKeyName() . '_seq'; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Set sequence name |
||
47 | * |
||
48 | * @param string $name |
||
49 | * @return string |
||
50 | */ |
||
51 | public function setSequenceName($name) |
||
52 | { |
||
53 | return $this->sequence = $name; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Update the model in the database. |
||
58 | * |
||
59 | * @param array $attributes |
||
60 | * @param array $options |
||
61 | * @return bool|int |
||
62 | */ |
||
63 | public function update(array $attributes = [], array $options = []) |
||
77 | |||
78 | /** |
||
79 | * wrap binaries to each attributes |
||
80 | * |
||
81 | * @param array $attributes |
||
82 | * @return array |
||
83 | */ |
||
84 | public function wrapBinary(&$attributes) |
||
85 | { |
||
86 | // If attributes contains binary field |
||
87 | // extract binary fields to new array |
||
88 | $binaries = []; |
||
89 | if ($this->checkBinary($attributes) and $this->getConnection() instanceof Oci8Connection) { |
||
90 | foreach ($attributes as $key => $value) { |
||
91 | if (in_array($key, $this->binaries)) { |
||
92 | $binaries[$key] = $value; |
||
93 | unset($attributes[$key]); |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $this->wrapBinaries = $binaries; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Check if attributes contains binary field |
||
103 | * |
||
104 | * @param array $attributes |
||
105 | * @return boolean |
||
106 | */ |
||
107 | public function checkBinary(array $attributes) |
||
108 | { |
||
109 | foreach ($attributes as $key => $value) { |
||
110 | // if attribute is in binary field list |
||
111 | if (in_array($key, $this->binaries)) { |
||
112 | return true; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | return false; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get the table qualified key name. |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getQualifiedKeyName() |
||
125 | { |
||
126 | $pos = strpos($this->getTable(), '@'); |
||
127 | |||
128 | if ($pos === false) { |
||
129 | return $this->getTable() . '.' . $this->getKeyName(); |
||
130 | } else { |
||
131 | $table = substr($this->getTable(), 0, $pos); |
||
132 | $dblink = substr($this->getTable(), $pos); |
||
133 | |||
134 | return $table . '.' . $this->getKeyName() . $dblink; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Get a new query builder instance for the connection. |
||
140 | * |
||
141 | * @return \Yajra\Oci8\Query\OracleBuilder |
||
142 | */ |
||
143 | protected function newBaseQueryBuilder() |
||
144 | { |
||
145 | $conn = $this->getConnection(); |
||
146 | |||
147 | $grammar = $conn->getQueryGrammar(); |
||
148 | |||
149 | return new QueryBuilder($conn, $grammar, $conn->getPostProcessor()); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Perform a model update operation. |
||
154 | * |
||
155 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
156 | * @param array $options |
||
157 | * @return boolean |
||
158 | */ |
||
159 | protected function performUpdate(Builder $query, array $options = []) |
||
160 | { |
||
161 | $dirty = $this->getDirty(); |
||
162 | |||
163 | if (count($dirty) > 0) { |
||
164 | // If the updating event returns false, we will cancel the update operation so |
||
165 | // developers can hook Validation systems into their models and cancel this |
||
166 | // operation if the model does not pass validation. Otherwise, we update. |
||
167 | if ($this->fireModelEvent('updating') === false) { |
||
168 | return false; |
||
169 | } |
||
170 | |||
171 | // First we need to create a fresh query instance and touch the creation and |
||
172 | // update timestamp on the model which are maintained by us for developer |
||
173 | // convenience. Then we will just continue saving the model instances. |
||
174 | if ($this->timestamps && array_get($options, 'timestamps', true)) { |
||
175 | $this->updateTimestamps(); |
||
176 | } |
||
177 | |||
178 | // Once we have run the update operation, we will fire the "updated" event for |
||
179 | // this model instance. This will allow developers to hook into these after |
||
180 | // models are updated, giving them a chance to do any special processing. |
||
181 | $dirty = $this->getDirty(); |
||
182 | |||
183 | if (count($dirty) > 0) { |
||
184 | // If dirty attributes contains binary field |
||
185 | // extract binary fields to new array |
||
186 | $this->updateBinary($query, $dirty, $options); |
||
187 | |||
188 | $this->fireModelEvent('updated', false); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | return true; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param Builder $query |
||
197 | * @param array $dirty |
||
198 | * @param array $options |
||
199 | */ |
||
200 | protected function updateBinary(Builder $query, $dirty, $options = []) |
||
208 | |||
209 | /** |
||
210 | * Perform a model insert operation. |
||
211 | * |
||
212 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
213 | * @param array $options |
||
214 | * @return bool |
||
215 | */ |
||
216 | protected function performInsert(Builder $query, array $options = []) |
||
262 | |||
263 | /** |
||
264 | * Insert the given attributes and set the ID on the model. |
||
265 | * |
||
266 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
267 | * @param array $attributes |
||
268 | * @return int|void |
||
269 | */ |
||
270 | protected function insertAndSetId(Builder $query, $attributes) |
||
280 | } |
||
281 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.