Skip to content

Commit

Permalink
Improved Breadcrumb composition. Get rid of array helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
neochief committed Mar 1, 2019
1 parent 5178700 commit 3692fef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/Builders/Parsers/BreadcrumbParser.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace ShvetsGroup\JetPages\Builders\Parsers;

use Illuminate\Support\Arr;
use ShvetsGroup\JetPages\Page\Page;
use ShvetsGroup\JetPages\Page\PageRegistry;

Expand All @@ -23,14 +24,22 @@ public function parse(Page $page, PageRegistry $registry)
}

$breadcrumb = [];
foreach ($breadcrumbPaths as $slug) {
foreach ($breadcrumbPaths as $path) {
if (is_array($path)) {
$title = Arr::get($path, 'title');
$slug = Arr::get($path, 'slug');
}
else {
$slug = $path;
}

$p = $registry->findBySlug($locale, Page::uriToSlug($slug));
if (!$p) {
throw new \RuntimeException("Can not find page with id '$locale/$slug'.");
}
$breadcrumb[] = [
'href' => $p->uri(true, true),
'title' => $p->getAttribute('title_short') ?: $p->getAttribute('title'),
'title' => $title ?? ($p->getAttribute('title_short') ?: $p->getAttribute('title')),
];
}
$page->setAttribute('breadcrumb', $breadcrumb);
Expand Down
5 changes: 3 additions & 2 deletions src/Page/Page.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace ShvetsGroup\JetPages\Page;

use Illuminate\Support\Arr;
use Illuminate\Contracts\Support\Arrayable;

class Page implements Arrayable
Expand Down Expand Up @@ -90,7 +91,7 @@ public function setAttributes(array $attributes = [], $force = false)
*/
public function getAttribute($key, $default = null)
{
return array_get($this->attributes, $key, $default);
return Arr::get($this->attributes, $key, $default);
}

/**
Expand All @@ -109,7 +110,7 @@ public function setAttribute($key, $value, $force = false)
if ($key == 'slug') {
$value = static::uriToSlug($value);
if (!$force) {
array_set($this->attributes, 'oldSlug', array_get($this->attributes, 'slug'));
Arr::set($this->attributes, 'oldSlug', array_get($this->attributes, 'slug'));
}
}
if ($key == 'slug' || $key == 'locale') {
Expand Down

0 comments on commit 3692fef

Please sign in to comment.