Upload beautify
This commit is contained in:
@@ -113,4 +113,50 @@ class Category extends Model
|
||||
{
|
||||
return 'slug';
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a category by a content-type slug and a category path (e.g. "audio/winamp").
|
||||
* This will locate the category with the final slug and verify its parent chain
|
||||
* matches the provided path and that the category belongs to the given content type.
|
||||
*
|
||||
* @param string $contentTypeSlug
|
||||
* @param string|array $categoryPath
|
||||
* @return Category|null
|
||||
*/
|
||||
public static function findByPath(string $contentTypeSlug, $categoryPath): ?Category
|
||||
{
|
||||
$parts = is_array($categoryPath)
|
||||
? array_values(array_map('strtolower', array_filter($categoryPath)))
|
||||
: array_values(array_map('strtolower', array_filter(explode('/', (string) $categoryPath))));
|
||||
|
||||
if (empty($parts)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$last = end($parts);
|
||||
|
||||
$category = static::where('slug', $last)
|
||||
->whereHas('contentType', function ($q) use ($contentTypeSlug) {
|
||||
$q->where('slug', strtolower($contentTypeSlug));
|
||||
})
|
||||
->first();
|
||||
|
||||
if (! $category) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Verify parent chain matches the preceding parts in the path
|
||||
$idx = count($parts) - 2;
|
||||
$current = $category;
|
||||
while ($idx >= 0) {
|
||||
$parent = $current->parent;
|
||||
if (! $parent || $parent->slug !== $parts[$idx]) {
|
||||
return null;
|
||||
}
|
||||
$current = $parent;
|
||||
$idx--;
|
||||
}
|
||||
|
||||
return $category;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user