Skip to content

Commit

Permalink
Added fallback for seo values
Browse files Browse the repository at this point in the history
  • Loading branch information
IsraelOrtuno committed Jun 18, 2018
1 parent 7a16a25 commit e277412
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
26 changes: 24 additions & 2 deletions src/HasPermalinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function permalink()
*/
public function storePermalink($attributes = [])
{
$attributes = array_undot($attributes);
$attributes = $this->preparePermalinkAttributes($attributes);

// Once we have the attributes we need to set, we will perform a new
// query in order to find if there is any parent class set for the
Expand All @@ -76,7 +76,6 @@ public function storePermalink($attributes = [])
$attributes['parent_id'] = $parent->getKey();
}


// Then we are ready to perform the creation or update action based on
// the model existence. If the model was recently created, we'll add
// a new permalink, otherwise, we'll update the existing permalink.
Expand All @@ -89,6 +88,29 @@ public function storePermalink($attributes = [])
return $this;
}

/**
* Prepare the seo attributes looking for default values in fallback methods.
*
* @param array $attributes
* @return array
*/
protected function preparePermalinkAttributes($attributes = [])
{
$attributes = array_undot($attributes);

$checks = ['seo.meta.title', 'seo.meta.description'];

foreach ($checks as $check) {
$method = 'permalink' . studly_case(str_replace('.', ' ', $check));

if (! array_has($attributes, $check) && method_exists($this, $method)) {
array_set($attributes, $check, call_user_func([$this, $method]));
}
}

return $attributes;
}

/**
* Set the permalink parent.
*
Expand Down
1 change: 0 additions & 1 deletion src/Permalink.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public static function boot()
$model->slug = SlugService::createSlug($model, 'slug', $model->slug, []);
}
});

}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Dummy/DummyUserWithFallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Devio\Permalink\Tests\Dummy;

class DummyUserWithFallback extends DummyUser
{
public function permalinkSeoMetaTitle()
{
return 'foo';
}

public function permalinkSeoMetaDescription()
{
return 'bar';
}
}
30 changes: 27 additions & 3 deletions tests/PermalinkCreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Devio\Permalink\Permalink;
use Devio\Permalink\Tests\Dummy\DummyUser;
use Devio\Permalink\Tests\Dummy\DummyUserWithFallback;
use Illuminate\Database\Eloquent\Relations\Relation;

class PermalinkCreationTest extends TestCase
Expand Down Expand Up @@ -131,11 +132,34 @@ public function permalink_is_nested_with_morphed_model_name_with_full_class_name
/** @test */
public function permalink_creation_accepts_dot_nested_arrays()
{
$user = factory(DummyUser::class)->create(['permalink' => [
'seo.meta' => ['title' => 'foo', 'description' => 'bar']
]]);
$user = factory(DummyUser::class)->create([
'permalink' => [
'seo.meta' => ['title' => 'foo', 'description' => 'bar']
]
]);

$this->assertEquals('foo', $user->permalink->seo['meta']['title']);
$this->assertEquals('bar', $user->permalink->seo['meta']['description']);
}

/** @test */
public function fallback_function_will_populate_seo_attributes()
{
$user = factory(DummyUserWithFallback::class)->create();

$this->assertEquals('foo', $user->permalink->seo['meta']['title']);
$this->assertEquals('bar', $user->permalink->seo['meta']['description']);
}

/** @test */
public function fallback_function_will_be_skiped_if_value_is_given()
{
$user = factory(DummyUserWithFallback::class)->create([
'permalink' => [
'seo.meta' => ['title' => 'custom']
]
]);

$this->assertEquals('custom', $user->permalink->seo['meta']['title']);
}
}
12 changes: 12 additions & 0 deletions tests/factories/DummyUserWithFallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Faker\Generator as Faker;
use Devio\Permalink\Tests\Dummy\DummyUserWithFallback;

$factory->define(DummyUserWithFallback::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => '123123123'
];
});

0 comments on commit e277412

Please sign in to comment.