Created
February 26, 2021 01:27
-
-
Save bayareawebpro/5b8bf076e20407431bb03f121d4f58c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace App\Repositories; | |
use Illuminate\Contracts\Config\Repository as Config; | |
use Illuminate\Contracts\Cache\Repository as Cache; | |
use Illuminate\Contracts\Pagination\Paginator; | |
use Illuminate\Database\Eloquent\Collection as EloquentCollection; | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Support\Collection; | |
use Illuminate\Support\Arr; | |
use Illuminate\Support\Str; | |
abstract class Repository | |
{ | |
protected Config $config; | |
protected Cache $cache; | |
protected int $cacheTtl; | |
public function __construct(Config $config, Cache $cache) | |
{ | |
$this->cacheTtl = 60 * 60; | |
$this->config = $config; | |
$this->cache = $cache; | |
} | |
/** | |
* @return static | |
*/ | |
public static function make(): self | |
{ | |
return app(static::class); | |
} | |
/** | |
* Get All International Countries | |
* @return EloquentCollection | |
*/ | |
public function countries(): EloquentCollection | |
{ | |
return $this->cache->rememberForever('countries', function () { | |
return Location::query() | |
->has('pages') | |
->where('capital', 1) | |
->distinct() | |
->orderBy('country') | |
->get([ | |
'iso', | |
'country', | |
'flag', | |
]); | |
}); | |
} | |
/** | |
* Filter Keyword Values. | |
* @param string|array $keywords | |
* @param bool $extractWords | |
* @return Collection | |
*/ | |
public function filterKeywords($keywords, bool $extractWords = false) | |
{ | |
return Collection::make(Arr::wrap($keywords)) | |
->map(function ($value) use ($extractWords) { | |
$value = Str::lower($value); | |
if ($extractWords) { | |
return $this->extractWords($value); | |
} | |
return $value; | |
}) | |
->flatten(0) | |
->reject(function ($value) { | |
return (empty($value) || strlen($value) < 2); | |
}); | |
} | |
/** | |
* Extract Word Values. | |
* @param string $value | |
* @return array | |
*/ | |
public function extractWords(string $value): array | |
{ | |
$words = preg_replace('/[^a-zA-Z0-9]/', ' ', (string)$value); | |
return Collection::make(explode(" ", $words)) | |
->reject(function ($word) { | |
return empty($word); | |
}) | |
->map(function ($word) { | |
return trim($word); | |
}) | |
->toArray(); | |
} | |
/** | |
* Make Cache Key | |
* @param string ...$attributes | |
* @return string | |
*/ | |
public function cacheKey(...$attributes): string | |
{ | |
return Str::slug(Collection::make($attributes)->flatten(0)->implode('-')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment