Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On dirty #32

Merged
merged 4 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ matrix:
- php: 7.0
env: PHPCS=1 DEFAULT=0

- php: 7.0
env: PHPSTAN=1 DEFAULT=0

before_script:
- if [[ $TRAVIS_PHP_VERSION != 7.0 ]]; then phpenv config-rm xdebug.ini; fi

Expand All @@ -38,8 +41,11 @@ before_script:
script:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi

- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -n -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi

- if [[ $PHPSTAN = 1 ]]; then composer require --dev phpstan/phpstan:^0.8 && vendor/bin/phpstan analyse -l 5 src; fi

after_success:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi

Expand Down
57 changes: 37 additions & 20 deletions src/Model/Behavior/SlugBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class SlugBehavior extends Behavior
* `['Model.beforeSave' => 'beforeSave']`.
* - onUpdate: Boolean indicating whether slug should be updated when
* updating record, defaults to `false`.
* - onDirty: Boolean indicating whether slug should be updated when
* slug field is dirty, defaults to `false`.
*
* @var array
*/
Expand Down Expand Up @@ -72,13 +74,14 @@ class SlugBehavior extends Behavior
'implementedMethods' => [
'slug' => 'slug',
],
'onUpdate' => false
'onUpdate' => false,
'onDirty' => false
];

/**
* Slugger instance
* Slugger instance or callable
*
* @var \Muffin\Slug\SluggerInterface
* @var \Muffin\Slug\SluggerInterface|callable
*/
protected $_slugger;

Expand Down Expand Up @@ -121,16 +124,16 @@ public function initialize(array $config)
/**
* Get/set slugger instance.
*
* @param callable $slugger Sets slugger instance if passed.
* @param \Muffin\Slug\SluggerInterface|callable $slugger Sets slugger instance if passed.
* If no argument is passed return slugger intance based on behavior config.
* @return callable|void
* @return callable|\Muffin\Slug\SluggerInterface|null
*/
public function slugger($slugger = null)
{
if ($slugger !== null) {
$this->_slugger = $slugger;

return;
return null;
}

if ($this->_slugger !== null) {
Expand Down Expand Up @@ -190,22 +193,31 @@ public function buildValidator(Event $event, Validator $validator, $name)
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
$config = $this->_config;

if (!$entity->isNew() && !$config['onUpdate']) {
$onUpdate = $this->config('onUpdate');
if (!$entity->isNew() && !$onUpdate) {
return;
}

if ($entity->dirty($config['field']) &&
(!$entity->isNew() || (!empty($entity->{$config['field']})))
$onDirty = $this->config('onDirty');
$field = $this->config('field');
if (!$onDirty
&& $entity->dirty($field)
&& (!$entity->isNew() || (!empty($entity->{$field})))
) {
return;
}

$fields = (array)$config['displayField'];
$separator = $this->config('separator');
if ($entity->dirty($field) && !empty($entity->{$field})) {
$slug = $this->slug($entity, $entity->{$field}, $separator);
$entity->set($field, $slug);

return;
}

$parts = [];
foreach ($fields as $field) {
$value = Hash::get($entity, $field);
foreach ((array)$this->config('displayField') as $displayField) {
$value = Hash::get($entity, $displayField);

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

$slug = $this->slug($entity, implode($config['separator'], $parts), $config['separator']);
$entity->set($config['field'], $slug);
$slug = $this->slug($entity, implode($separator, $parts), $separator);
$entity->set($field, $slug);
}

/**
Expand All @@ -245,11 +257,15 @@ public function findSlugged(Query $query, array $options)
*
* @param \Cake\ORM\Entity|string $entity Entity to create slug for
* @param string $string String to create slug for.
* @param string $separator Separator.
* @param string|null $separator Separator.
* @return string Slug.
*/
public function slug($entity, $string = null, $separator = '-')
public function slug($entity, $string = null, $separator = null)
{
if ($separator === null) {
$separator = $this->config('separator');
}

if (is_string($entity)) {
if ($string !== null) {
$separator = $string;
Expand All @@ -269,7 +285,8 @@ public function slug($entity, $string = null, $separator = '-')

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

if (isset($entity) && $unique = $this->config('unique')) {
$unique = $this->config('unique');
if (isset($entity) && $unique) {
$slug = $unique($entity, $slug, $separator);
}

Expand All @@ -284,7 +301,7 @@ public function slug($entity, $string = null, $separator = '-')
* @param string $separator Separator.
* @return string Unique slug.
*/
protected function _uniqueSlug(Entity $entity, $slug, $separator = '-')
protected function _uniqueSlug(Entity $entity, $slug, $separator)
{
$primaryKey = $this->_table->primaryKey();
$field = $this->_table->aliasField($this->config('field'));
Expand Down
25 changes: 25 additions & 0 deletions tests/TestCase/Model/Behavior/SlugBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,31 @@ public function testBeforeSaveDirtyField()
$result = $this->Tags->save($tag)->slug;
$expected = 'baz';
$this->assertEquals($expected, $result);

$this->Tags->behaviors()->Slug->config('onDirty', true);

$data = ['name' => 'I am nice', 'slug' => 'make ME Nice'];
$tag = $this->Tags->newEntity($data);

$result = $this->Tags->save($tag)->slug;
$expected = 'make-me-nice';
$this->assertEquals($expected, $result);

$data = ['name' => 'Fooz', 'slug' => ''];
$tag = $this->Tags->newEntity($data);

$result = $this->Tags->save($tag)->slug;
$expected = 'fooz';
$this->assertEquals($expected, $result);

$this->Tags->behaviors()->Slug->config('onUpdate', true);

$tag = $this->Tags->find()->where(['name' => 'I am nice'])->first();
$tag->slug = 'I is NICE';

$result = $this->Tags->save($tag)->slug;
$expected = 'i-is-nice';
$this->assertEquals($expected, $result);
}

/**
Expand Down