boomdraw /
laravel-canonicalizable
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Boomdraw\Canonicalizable; |
||
| 4 | |||
| 5 | use Boomdraw\Canonicalizer\Facades\Canonicalizer; |
||
| 6 | use Illuminate\Database\Eloquent\Model; |
||
| 7 | use Illuminate\Support\Str; |
||
| 8 | |||
| 9 | trait HasCanonical |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Get fields to generate canonical for. |
||
| 13 | * |
||
| 14 | * @return CanonicalFieldsCollection |
||
| 15 | */ |
||
| 16 | abstract public function getCanonicalFields(): CanonicalFieldsCollection; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get the model's original attribute values. |
||
| 20 | * |
||
| 21 | * @param string|null $key |
||
| 22 | * @param mixed $default |
||
| 23 | * @return mixed|array |
||
| 24 | */ |
||
| 25 | abstract public function getOriginal($key = null, $default = null); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Get the value of the model's primary key. |
||
| 29 | * |
||
| 30 | * @return mixed |
||
| 31 | */ |
||
| 32 | abstract public function getKey(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Get the primary key for the model. |
||
| 36 | * |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | abstract public function getKeyName(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Updates canonical field immediately. |
||
| 43 | * |
||
| 44 | * @param string $fieldName |
||
| 45 | * @return bool |
||
| 46 | */ |
||
| 47 | 1 | public function canonicalize(string $fieldName): bool |
|
| 48 | { |
||
| 49 | 1 | $field = $this->getCanonicalFields()->getFields()[$fieldName] ?? false; |
|
| 50 | 1 | if ($field) { |
|
| 51 | 1 | $this->addCanonical($field); |
|
| 52 | |||
| 53 | 1 | return true; |
|
| 54 | } |
||
| 55 | |||
| 56 | 1 | return false; |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Bootstrap trait. |
||
| 61 | * |
||
| 62 | * @return void |
||
| 63 | */ |
||
| 64 | 33 | protected static function bootHasCanonical(): void |
|
| 65 | { |
||
| 66 | static::creating(static function (Model $model) { |
||
| 67 | 33 | $model->generateOnCreate(); |
|
| 68 | 33 | }); |
|
| 69 | |||
| 70 | static::updating(static function (Model $model) { |
||
| 71 | 6 | $model->generateOnUpdate(); |
|
| 72 | 33 | }); |
|
| 73 | 33 | } |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Generate canonical fields on creating event. |
||
| 77 | * |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | 33 | protected function generateOnCreate(): void |
|
| 81 | { |
||
| 82 | $this->getCanonicalFields()->getFields()->each(function ($field) { |
||
| 83 | 33 | if ($field->generateOnCreate || $this->forceCanonicalization($field)) { |
|
| 84 | 32 | $this->addCanonical($field); |
|
| 85 | } |
||
| 86 | 33 | }); |
|
| 87 | 33 | } |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Generate canonical fields on updating event. |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | 6 | protected function generateOnUpdate(): void |
|
| 95 | { |
||
| 96 | $this->getCanonicalFields()->getFields()->each(function ($field) { |
||
| 97 | 6 | if ($field->generateOnUpdate || $this->forceCanonicalization($field)) { |
|
| 98 | 4 | $this->addCanonical($field); |
|
| 99 | } |
||
| 100 | 6 | }); |
|
| 101 | 6 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Determine should canonicalization be forced |
||
| 105 | * |
||
| 106 | * @param CanonicalField $field |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | 5 | protected function forceCanonicalization(CanonicalField $field): bool |
|
| 110 | { |
||
| 111 | 5 | $force = ($field->shouldBeUnique() || $field->forceCanonicalization); |
|
| 112 | 5 | return $force && $this->hasCustomCanonicalBeenUsed($field->to); |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Write canonicalized value to a field. |
||
| 117 | * |
||
| 118 | * @param CanonicalField $field |
||
| 119 | */ |
||
| 120 | 32 | protected function addCanonical(CanonicalField $field): void |
|
| 121 | { |
||
| 122 | 32 | $to = $field->to; |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 123 | 32 | if ($this->hasCustomCanonicalBeenUsed($to)) { |
|
| 124 | 10 | $canonical = data_get($this, $to, ''); |
|
| 125 | 10 | if ($field->forceCanonicalization) { |
|
| 126 | 10 | $canonical = $this->generateCanonical($field, $canonical); |
|
| 127 | } |
||
| 128 | } else { |
||
| 129 | 23 | $canonical = data_get($this, $field->from, ''); |
|
| 130 | 23 | $canonical = $this->generateCanonical($field, $canonical); |
|
| 131 | } |
||
| 132 | 32 | if ($field->shouldBeUnique()) { |
|
| 133 | 6 | $canonical = $this->makeCanonicalUnique($canonical, $to, $field->uniqueSeparator); |
|
| 134 | } |
||
| 135 | 32 | $this->$to = $canonical; |
|
| 136 | 32 | } |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Determine if the custom canonical value was used. |
||
| 140 | * |
||
| 141 | * @param string $to |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | 32 | protected function hasCustomCanonicalBeenUsed(string $to): bool |
|
| 145 | { |
||
| 146 | 32 | return $this->getOriginal($to) !== $this->$to && null !== $this->$to; |
|
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Generate canonical field value. |
||
| 151 | * |
||
| 152 | * @param CanonicalField $field |
||
| 153 | * @param string|null $string |
||
| 154 | * @return string|null |
||
| 155 | */ |
||
| 156 | 29 | protected function generateCanonical(CanonicalField $field, string $string): ?string |
|
| 157 | { |
||
| 158 | 29 | $args = $field->args; |
|
| 159 | 29 | array_unshift($args, $string); |
|
| 160 | 29 | if (null !== $callback = $field->callback) { |
|
|
0 ignored issues
–
show
The property
$callback is declared protected in Boomdraw\Canonicalizable\CanonicalField. Since you implement __get, consider adding a @property or @property-read.
Loading history...
|
|||
| 161 | 6 | return call_user_func($callback, ...$args); |
|
| 162 | } |
||
| 163 | 23 | $callable = $this->determineCallable($field->type); |
|
| 164 | |||
| 165 | 23 | return call_user_func($callable, ...$args); |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Make canonical value unique. |
||
| 170 | * |
||
| 171 | * @param string|null $canonical |
||
| 172 | * @param string $field |
||
| 173 | * @param string $separator |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | 6 | protected function makeCanonicalUnique(?string $canonical, string $field, string $separator): string |
|
| 177 | { |
||
| 178 | 6 | $originalCanonical = $canonical; |
|
| 179 | 6 | $i = 1; |
|
| 180 | 6 | while (empty($canonical) || $this->otherRecordExistsWithCanonical($canonical, $field)) { |
|
| 181 | 6 | $canonical = $originalCanonical . $separator . $i++; |
|
| 182 | } |
||
| 183 | |||
| 184 | 6 | return $canonical; |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Check another record with current canonical value existence. |
||
| 189 | * |
||
| 190 | * @param string $canonical |
||
| 191 | * @param string $field |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | 6 | protected function otherRecordExistsWithCanonical(string $canonical, string $field): bool |
|
| 195 | { |
||
| 196 | 6 | $key = $this->getKey(); |
|
| 197 | 6 | if ($this->incrementing) { |
|
| 198 | 6 | $key ??= '0'; |
|
| 199 | } |
||
| 200 | 6 | $query = static::where($field, $canonical) |
|
| 201 | 6 | ->where($this->getKeyName(), '!=', $key) |
|
| 202 | 6 | ->withoutGlobalScopes(); |
|
| 203 | 6 | if ($this->usesSoftDeletes()) { |
|
| 204 | 1 | $query->withTrashed(); |
|
| 205 | } |
||
| 206 | |||
| 207 | 6 | return $query->exists(); |
|
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Determine if the model uses SoftDeletes |
||
| 212 | * |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | 6 | protected function usesSoftDeletes(): bool |
|
| 216 | { |
||
| 217 | 6 | return (bool)in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this)); |
|
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Determine callable for specific type. |
||
| 222 | * |
||
| 223 | * @param string $type |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | 23 | protected function determineCallable(string $type): array |
|
| 227 | { |
||
| 228 | 23 | if ('default' !== $type) { |
|
| 229 | 6 | $method = 'canonicalize' . Str::studly($type); |
|
| 230 | 6 | if (method_exists($this, $method)) { |
|
| 231 | 1 | return [$this, $method]; |
|
| 232 | } |
||
| 233 | 5 | $method = Str::camel($type); |
|
| 234 | 5 | if (Canonicalizer::hasMacro($method) || method_exists(Canonicalizer::getFacadeRoot(), $method)) { |
|
| 235 | 4 | return [Canonicalizer::class, $method]; |
|
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | 18 | return [Canonicalizer::class, 'canonicalize']; |
|
| 240 | } |
||
| 241 | } |
||
| 242 |