Skip to content

Commit fe73fe7

Browse files
authored
Merge pull request #32 from UseMuffin/on-dirty
On dirty
2 parents f0fd10e + e396d8c commit fe73fe7

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ matrix:
2424
- php: 7.0
2525
env: PHPCS=1 DEFAULT=0
2626

27+
- php: 7.0
28+
env: PHPSTAN=1 DEFAULT=0
29+
2730
before_script:
2831
- if [[ $TRAVIS_PHP_VERSION != 7.0 ]]; then phpenv config-rm xdebug.ini; fi
2932

@@ -38,8 +41,11 @@ before_script:
3841
script:
3942
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
4043
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi
44+
4145
- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -n -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi
4246

47+
- if [[ $PHPSTAN = 1 ]]; then composer require --dev phpstan/phpstan:^0.8 && vendor/bin/phpstan analyse -l 5 src; fi
48+
4349
after_success:
4450
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi
4551

src/Model/Behavior/SlugBehavior.php

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class SlugBehavior extends Behavior
4343
* `['Model.beforeSave' => 'beforeSave']`.
4444
* - onUpdate: Boolean indicating whether slug should be updated when
4545
* updating record, defaults to `false`.
46+
* - onDirty: Boolean indicating whether slug should be updated when
47+
* slug field is dirty, defaults to `false`.
4648
*
4749
* @var array
4850
*/
@@ -72,13 +74,14 @@ class SlugBehavior extends Behavior
7274
'implementedMethods' => [
7375
'slug' => 'slug',
7476
],
75-
'onUpdate' => false
77+
'onUpdate' => false,
78+
'onDirty' => false
7679
];
7780

7881
/**
79-
* Slugger instance
82+
* Slugger instance or callable
8083
*
81-
* @var \Muffin\Slug\SluggerInterface
84+
* @var \Muffin\Slug\SluggerInterface|callable
8285
*/
8386
protected $_slugger;
8487

@@ -121,16 +124,16 @@ public function initialize(array $config)
121124
/**
122125
* Get/set slugger instance.
123126
*
124-
* @param callable $slugger Sets slugger instance if passed.
127+
* @param \Muffin\Slug\SluggerInterface|callable $slugger Sets slugger instance if passed.
125128
* If no argument is passed return slugger intance based on behavior config.
126-
* @return callable|void
129+
* @return callable|\Muffin\Slug\SluggerInterface|null
127130
*/
128131
public function slugger($slugger = null)
129132
{
130133
if ($slugger !== null) {
131134
$this->_slugger = $slugger;
132135

133-
return;
136+
return null;
134137
}
135138

136139
if ($this->_slugger !== null) {
@@ -190,22 +193,31 @@ public function buildValidator(Event $event, Validator $validator, $name)
190193
*/
191194
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
192195
{
193-
$config = $this->_config;
194-
195-
if (!$entity->isNew() && !$config['onUpdate']) {
196+
$onUpdate = $this->config('onUpdate');
197+
if (!$entity->isNew() && !$onUpdate) {
196198
return;
197199
}
198200

199-
if ($entity->dirty($config['field']) &&
200-
(!$entity->isNew() || (!empty($entity->{$config['field']})))
201+
$onDirty = $this->config('onDirty');
202+
$field = $this->config('field');
203+
if (!$onDirty
204+
&& $entity->dirty($field)
205+
&& (!$entity->isNew() || (!empty($entity->{$field})))
201206
) {
202207
return;
203208
}
204209

205-
$fields = (array)$config['displayField'];
210+
$separator = $this->config('separator');
211+
if ($entity->dirty($field) && !empty($entity->{$field})) {
212+
$slug = $this->slug($entity, $entity->{$field}, $separator);
213+
$entity->set($field, $slug);
214+
215+
return;
216+
}
217+
206218
$parts = [];
207-
foreach ($fields as $field) {
208-
$value = Hash::get($entity, $field);
219+
foreach ((array)$this->config('displayField') as $displayField) {
220+
$value = Hash::get($entity, $displayField);
209221

210222
if ($value === null && !$entity->isNew()) {
211223
return;
@@ -220,8 +232,8 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
220232
return;
221233
}
222234

223-
$slug = $this->slug($entity, implode($config['separator'], $parts), $config['separator']);
224-
$entity->set($config['field'], $slug);
235+
$slug = $this->slug($entity, implode($separator, $parts), $separator);
236+
$entity->set($field, $slug);
225237
}
226238

227239
/**
@@ -245,11 +257,15 @@ public function findSlugged(Query $query, array $options)
245257
*
246258
* @param \Cake\ORM\Entity|string $entity Entity to create slug for
247259
* @param string $string String to create slug for.
248-
* @param string $separator Separator.
260+
* @param string|null $separator Separator.
249261
* @return string Slug.
250262
*/
251-
public function slug($entity, $string = null, $separator = '-')
263+
public function slug($entity, $string = null, $separator = null)
252264
{
265+
if ($separator === null) {
266+
$separator = $this->config('separator');
267+
}
268+
253269
if (is_string($entity)) {
254270
if ($string !== null) {
255271
$separator = $string;
@@ -269,7 +285,8 @@ public function slug($entity, $string = null, $separator = '-')
269285

270286
$slug = $this->_slug($string, $separator);
271287

272-
if (isset($entity) && $unique = $this->config('unique')) {
288+
$unique = $this->config('unique');
289+
if (isset($entity) && $unique) {
273290
$slug = $unique($entity, $slug, $separator);
274291
}
275292

@@ -284,7 +301,7 @@ public function slug($entity, $string = null, $separator = '-')
284301
* @param string $separator Separator.
285302
* @return string Unique slug.
286303
*/
287-
protected function _uniqueSlug(Entity $entity, $slug, $separator = '-')
304+
protected function _uniqueSlug(Entity $entity, $slug, $separator)
288305
{
289306
$primaryKey = $this->_table->primaryKey();
290307
$field = $this->_table->aliasField($this->config('field'));

tests/TestCase/Model/Behavior/SlugBehaviorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,31 @@ public function testBeforeSaveDirtyField()
119119
$result = $this->Tags->save($tag)->slug;
120120
$expected = 'baz';
121121
$this->assertEquals($expected, $result);
122+
123+
$this->Tags->behaviors()->Slug->config('onDirty', true);
124+
125+
$data = ['name' => 'I am nice', 'slug' => 'make ME Nice'];
126+
$tag = $this->Tags->newEntity($data);
127+
128+
$result = $this->Tags->save($tag)->slug;
129+
$expected = 'make-me-nice';
130+
$this->assertEquals($expected, $result);
131+
132+
$data = ['name' => 'Fooz', 'slug' => ''];
133+
$tag = $this->Tags->newEntity($data);
134+
135+
$result = $this->Tags->save($tag)->slug;
136+
$expected = 'fooz';
137+
$this->assertEquals($expected, $result);
138+
139+
$this->Tags->behaviors()->Slug->config('onUpdate', true);
140+
141+
$tag = $this->Tags->find()->where(['name' => 'I am nice'])->first();
142+
$tag->slug = 'I is NICE';
143+
144+
$result = $this->Tags->save($tag)->slug;
145+
$expected = 'i-is-nice';
146+
$this->assertEquals($expected, $result);
122147
}
123148

124149
/**

0 commit comments

Comments
 (0)