3
3
namespace Tools \Model \Behavior ;
4
4
5
5
use Cake \Core \Configure ;
6
+ use Cake \Datasource \EntityInterface ;
6
7
use Cake \Event \Event ;
7
8
use Cake \ORM \Behavior ;
8
- use Cake \ORM \Entity ;
9
9
use Cake \ORM \Query ;
10
10
use Cake \ORM \Table ;
11
11
use Cake \Utility \Inflector ;
12
- use Exception ;
13
12
use InvalidArgumentException ;
13
+ use RuntimeException ;
14
14
15
15
/**
16
16
* SluggedBehavior
@@ -131,11 +131,11 @@ public function initialize(array $config) {
131
131
if (strpos ($ field , '. ' )) {
132
132
list ($ alias , $ field ) = explode ('. ' , $ field );
133
133
if (!$ this ->_table ->$ alias ->hasField ($ field )) {
134
- throw new Exception ('(SluggedBehavior::setup) model ' . $ this ->_table ->$ alias ->name . ' is missing the field ' . $ field .
134
+ throw new RuntimeException ('(SluggedBehavior::setup) model ' . $ this ->_table ->$ alias ->name . ' is missing the field ' . $ field .
135
135
' (specified in the setup for model ' . $ this ->_table ->name . ') ' );
136
136
}
137
137
} elseif (!$ this ->_table ->hasField ($ field ) && !method_exists ($ this ->_table ->entityClass (), '_get ' . Inflector::classify ($ field ))) {
138
- throw new Exception ('(SluggedBehavior::setup) model ' . $ this ->_table ->name . ' is missing the field ' . $ field . ' specified in the setup. ' );
138
+ throw new RuntimeException ('(SluggedBehavior::setup) model ' . $ this ->_table ->name . ' is missing the field ' . $ field . ' specified in the setup. ' );
139
139
}
140
140
}
141
141
}
@@ -161,10 +161,10 @@ public function findSlugged(Query $query, array $options) {
161
161
* SluggedBehavior::beforeRules()
162
162
*
163
163
* @param \Cake\Event\Event $event
164
- * @param \Cake\ORM\Entity $entity
164
+ * @param \Cake\Datasource\EntityInterface $entity
165
165
* @return void
166
166
*/
167
- public function beforeRules (Event $ event , Entity $ entity ) {
167
+ public function beforeRules (Event $ event , EntityInterface $ entity ) {
168
168
if ($ this ->_config ['on ' ] === 'beforeRules ' ) {
169
169
$ this ->slug ($ entity );
170
170
}
@@ -174,10 +174,10 @@ public function beforeRules(Event $event, Entity $entity) {
174
174
* SluggedBehavior::beforeSave()
175
175
*
176
176
* @param \Cake\Event\Event $event
177
- * @param \Cake\ORM\Entity $entity
177
+ * @param \Cake\Datasource\EntityInterface $entity
178
178
* @return void
179
179
*/
180
- public function beforeSave (Event $ event , Entity $ entity ) {
180
+ public function beforeSave (Event $ event , EntityInterface $ entity ) {
181
181
if ($ this ->_config ['on ' ] === 'beforeSave ' ) {
182
182
$ this ->slug ($ entity );
183
183
}
@@ -186,11 +186,11 @@ public function beforeSave(Event $event, Entity $entity) {
186
186
/**
187
187
* SluggedBehavior::slug()
188
188
*
189
- * @param \Cake\ORM\Entity $entity Entity
189
+ * @param \Cake\Datasource\EntityInterface $entity Entity
190
190
* @param array $options Options
191
191
* @return void
192
192
*/
193
- public function slug (Entity $ entity , array $ options = []) {
193
+ public function slug (EntityInterface $ entity , array $ options = []) {
194
194
$ overwrite = isset ($ options ['overwrite ' ]) ? $ options ['overwrite ' ] : $ this ->_config ['overwrite ' ];
195
195
if (!$ overwrite && $ entity ->get ($ this ->_config ['overwriteField ' ])) {
196
196
$ overwrite = true ;
@@ -216,11 +216,11 @@ public function slug(Entity $entity, array $options = []) {
216
216
* of maybe some not in sync slugs anymore (saving the same title again,
217
217
* but the slug is completely different, for example).
218
218
*
219
- * @param \Cake\ORM\Entity $entity
219
+ * @param \Cake\Datasource\EntityInterface $entity
220
220
* @param bool $deep If true it will generate a new slug and compare it to the currently stored one.
221
221
* @return bool
222
222
*/
223
- public function needsSlugUpdate ($ entity , $ deep = false ) {
223
+ public function needsSlugUpdate (EntityInterface $ entity , $ deep = false ) {
224
224
foreach ((array )$ this ->_config ['label ' ] as $ label ) {
225
225
if ($ entity ->dirty ($ label )) {
226
226
return true ;
@@ -245,10 +245,11 @@ public function needsSlugUpdate($entity, $deep = false) {
245
245
* until a unique slug is found
246
246
*
247
247
* @param string $value
248
- * @param \Cake\ORM\Entity |null $entity
248
+ * @param \Cake\Datasource\EntityInterface |null $entity
249
249
* @return string A slug
250
+ * @throws \RuntimeException
250
251
*/
251
- public function generateSlug ($ value , Entity $ entity = null ) {
252
+ public function generateSlug ($ value , EntityInterface $ entity = null ) {
252
253
$ separator = $ this ->_config ['separator ' ];
253
254
254
255
$ string = str_replace (["\r\n" , "\r" , "\n" ], ' ' , $ value );
@@ -300,7 +301,7 @@ public function generateSlug($value, Entity $entity = null) {
300
301
}
301
302
if ($ this ->_config ['unique ' ]) {
302
303
if (!$ entity ) {
303
- throw new Exception ('Needs an Entity to work on ' );
304
+ throw new RuntimeException ('Needs an Entity to work on ' );
304
305
}
305
306
$ field = $ this ->_table ->alias () . '. ' . $ this ->_config ['field ' ];
306
307
$ conditions = [$ field => $ slug ];
@@ -339,10 +340,11 @@ public function generateSlug($value, Entity $entity = null) {
339
340
*
340
341
* @param array $params
341
342
* @return bool Success
343
+ * @throws \RuntimeException
342
344
*/
343
345
public function resetSlugs ($ params = []) {
344
346
if (!$ this ->_table ->hasField ($ this ->_config ['field ' ])) {
345
- throw new Exception ('Table does not have field ' . $ this ->_config ['field ' ]);
347
+ throw new RuntimeException ('Table does not have field ' . $ this ->_config ['field ' ]);
346
348
}
347
349
$ defaults = [
348
350
'page ' => 1 ,
@@ -361,14 +363,15 @@ public function resetSlugs($params = []) {
361
363
362
364
$ this ->_table ->behaviors ()->Slugged ->config ($ params , null , false );
363
365
while (($ records = $ this ->_table ->find ('all ' , $ params )->toArray ())) {
366
+ /** @var \Cake\ORM\Entity $record */
364
367
foreach ($ records as $ record ) {
365
368
$ record ->isNew (true );
366
369
$ options = [
367
370
'validate ' => true ,
368
371
'fieldList ' => array_merge ([$ this ->_table ->primaryKey (), $ this ->_config ['field ' ]], $ this ->_config ['label ' ])
369
372
];
370
373
if (!$ this ->_table ->save ($ record , $ options )) {
371
- throw new Exception (print_r ($ this -> _table ->errors (), true ));
374
+ throw new RuntimeException (print_r ($ record ->errors (), true ));
372
375
}
373
376
}
374
377
$ params ['page ' ]++;
@@ -384,10 +387,10 @@ public function resetSlugs($params = []) {
384
387
*
385
388
* //FIXME
386
389
*
387
- * @param \Cake\ORM\Entity $entity
390
+ * @param \Cake\Datasource\EntityInterface $entity
388
391
* @return void
389
392
*/
390
- protected function _multiSlug (Entity $ entity ) {
393
+ protected function _multiSlug (EntityInterface $ entity ) {
391
394
$ label = $ this ->config ('label ' );
392
395
$ field = current ($ label );
393
396
$ fields = (array )$ entity ->get ($ field );
0 commit comments