fomvasss /
laravel-slugmaker
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Fomvasss\SlugMaker; |
||||
| 4 | |||||
| 5 | /** |
||||
| 6 | * Class SlugGenerator |
||||
| 7 | * |
||||
| 8 | * @package \Fomvasss\SlugGenerator |
||||
| 9 | */ |
||||
| 10 | class SlugHelper |
||||
| 11 | { |
||||
| 12 | use SlugGenerator; |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 13 | |||||
| 14 | protected $slugModelClass; |
||||
| 15 | |||||
| 16 | protected $currentSlugModel = null; |
||||
| 17 | |||||
| 18 | protected $slugConfig = [ |
||||
| 19 | 'separator' => '-', |
||||
| 20 | 'prefix' => '', |
||||
| 21 | 'suffix' => '', |
||||
| 22 | 'maximum_length' => 190, |
||||
| 23 | 'model' => \Fomvasss\SlugGenerator\Models\Slug::class, |
||||
|
0 ignored issues
–
show
The type
Fomvasss\SlugGenerator\Models\Slug was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 24 | ]; |
||||
| 25 | |||||
| 26 | /** |
||||
| 27 | * SlugHelper constructor. |
||||
| 28 | */ |
||||
| 29 | public function __construct() |
||||
| 30 | { |
||||
| 31 | //$this->slugConfig = array_merge($this->slugConfig, config('slugmaker', [])); |
||||
| 32 | $this->slugConfig = config('slugmaker'); |
||||
|
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 33 | $this->slugModelClass = app()->make($this->slugConfig['model']); |
||||
|
0 ignored issues
–
show
The function
app was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 34 | } |
||||
| 35 | |||||
| 36 | /** |
||||
| 37 | * @param $slug |
||||
| 38 | * @param null $modelClass |
||||
|
0 ignored issues
–
show
|
|||||
| 39 | * @return null |
||||
| 40 | */ |
||||
| 41 | public function getModel($slug, $modelClass = null) |
||||
| 42 | { |
||||
| 43 | $slug = $this->slugModelClass |
||||
| 44 | ->byNameByClass($slug, $modelClass) |
||||
| 45 | ->first(); |
||||
| 46 | |||||
| 47 | return $slug ? $slug->slugable : null; |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * @param array $slugs |
||||
| 52 | * @param null $modelClass |
||||
|
0 ignored issues
–
show
|
|||||
| 53 | * @return mixed |
||||
| 54 | */ |
||||
| 55 | public function getModels(array $slugs, $modelClass = null) |
||||
| 56 | { |
||||
| 57 | $slugs = $this->slugModelClass |
||||
| 58 | ->with('slugable') |
||||
| 59 | ->byNamesByClass($slugs, $modelClass) |
||||
| 60 | ->get(); |
||||
| 61 | |||||
| 62 | return $slugs->map(function ($item) { |
||||
| 63 | return $item->slugable; |
||||
| 64 | }); |
||||
| 65 | } |
||||
| 66 | |||||
| 67 | /** |
||||
| 68 | * @param $slug |
||||
| 69 | * @param null $modelClass |
||||
|
0 ignored issues
–
show
|
|||||
| 70 | * @return null |
||||
| 71 | */ |
||||
| 72 | public function getId($slug, $modelClass = null) |
||||
| 73 | { |
||||
| 74 | $model = $this->getModel($slug, $modelClass); |
||||
|
0 ignored issues
–
show
Are you sure the assignment to
$model is correct as $this->getModel($slug, $modelClass) targeting Fomvasss\SlugMaker\SlugHelper::getModel() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 75 | return $model ? $model->id : null; |
||||
|
0 ignored issues
–
show
|
|||||
| 76 | } |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * @param array $slugs |
||||
| 80 | * @param null $modelClass |
||||
|
0 ignored issues
–
show
|
|||||
| 81 | * @return mixed |
||||
| 82 | */ |
||||
| 83 | public function getIds(array $slugs, $modelClass = null) |
||||
| 84 | { |
||||
| 85 | return $this->getModels($slugs, $modelClass) |
||||
| 86 | ->pluck('id') |
||||
| 87 | ->toArray(); |
||||
| 88 | } |
||||
| 89 | |||||
| 90 | /** |
||||
| 91 | * @param array $attributes |
||||
| 92 | * @param bool $useId |
||||
| 93 | * @return array |
||||
| 94 | */ |
||||
| 95 | public function getIdsGroupedByClass(array $attributes, $useId = false) |
||||
| 96 | { |
||||
| 97 | $key = $useId ? 'slugable_id' : 'name'; |
||||
| 98 | |||||
| 99 | $slugs = $this->slugModelClass |
||||
| 100 | ->byClassesByNames($attributes, $key) |
||||
| 101 | ->get(); |
||||
| 102 | |||||
| 103 | return $this->groupedByClass($slugs |
||||
| 104 | ->pluck('slugable_type', 'slugable_id') |
||||
| 105 | ->toArray()); |
||||
| 106 | } |
||||
| 107 | |||||
| 108 | /** |
||||
| 109 | * @param $model |
||||
| 110 | * @return mixed |
||||
| 111 | */ |
||||
| 112 | public function getSlugName($model) |
||||
| 113 | { |
||||
| 114 | return $model->getSlugName(); |
||||
| 115 | } |
||||
| 116 | |||||
| 117 | /** |
||||
| 118 | * @param $model |
||||
| 119 | * @param $slug |
||||
| 120 | * @return mixed |
||||
| 121 | */ |
||||
| 122 | public function makeForModel($model, $slug = '') |
||||
| 123 | { |
||||
| 124 | $this->currentSlugModel = $model->slug; |
||||
| 125 | $slug = empty($slug) ? $this->getStrSlugByModelFields($model) : $slug; |
||||
| 126 | |||||
| 127 | $newSlug = $this->getSlug($slug); |
||||
| 128 | |||||
| 129 | if ($model->slug) { |
||||
| 130 | if ($this->slugConfig['generate_on_update']) { |
||||
| 131 | $model->slug()->update(['name' => $newSlug]); |
||||
| 132 | } |
||||
| 133 | return $newSlug; |
||||
| 134 | } |
||||
| 135 | |||||
| 136 | if ($this->slugConfig['generate_on_create']) { |
||||
| 137 | $model->slug()->create(['name' => $newSlug]); |
||||
| 138 | } |
||||
| 139 | |||||
| 140 | return $newSlug; |
||||
| 141 | } |
||||
| 142 | |||||
| 143 | /** |
||||
| 144 | * @param $model |
||||
| 145 | * @return string |
||||
| 146 | */ |
||||
| 147 | protected function getStrSlugByModelFields($model) |
||||
| 148 | { |
||||
| 149 | $str = ''; |
||||
| 150 | foreach ($model->getSlugSourceFields() as $field) { |
||||
| 151 | $str .= $model->{$field}.'-'; |
||||
| 152 | } |
||||
| 153 | |||||
| 154 | return trim($str, '-'); |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | /** |
||||
| 158 | * @param $model |
||||
| 159 | */ |
||||
| 160 | public function deleteByModel($model) |
||||
| 161 | { |
||||
| 162 | if ($model->slug) { |
||||
| 163 | return $model->slug()->delete(); |
||||
| 164 | } |
||||
| 165 | |||||
| 166 | return; |
||||
| 167 | } |
||||
| 168 | |||||
| 169 | /** |
||||
| 170 | * @param $attributes |
||||
| 171 | * @return array |
||||
| 172 | */ |
||||
| 173 | private function groupedByClass($attributes): array |
||||
| 174 | { |
||||
| 175 | $res = []; |
||||
| 176 | foreach ($attributes as $id => $type) { |
||||
| 177 | $res[$type][] = $id; |
||||
| 178 | } |
||||
| 179 | |||||
| 180 | return $res; |
||||
| 181 | } |
||||
| 182 | } |
||||
| 183 |