prism-x /
laravel-schemaless
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace PrismX\Schemaless\Traits; |
||||||
| 4 | |||||||
| 5 | use Illuminate\Support\Str; |
||||||
| 6 | |||||||
| 7 | trait HasSchemalessAttributes |
||||||
| 8 | { |
||||||
| 9 | public $schema = []; |
||||||
| 10 | |||||||
| 11 | protected function getSchemaColumnName() |
||||||
| 12 | { |
||||||
| 13 | return '_attributes'; |
||||||
| 14 | } |
||||||
| 15 | |||||||
| 16 | public function initializeHasSchemalessAttributes() |
||||||
| 17 | { |
||||||
| 18 | $this->fillable( |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 19 | array_merge( |
||||||
| 20 | $this->getFillable(), |
||||||
|
0 ignored issues
–
show
It seems like
getFillable() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 21 | array_keys($this->schema ?? []) |
||||||
| 22 | ) |
||||||
| 23 | ); |
||||||
| 24 | |||||||
| 25 | $this->addHidden([$this->getSchemaColumnName()]); |
||||||
|
0 ignored issues
–
show
It seems like
addHidden() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 26 | } |
||||||
| 27 | |||||||
| 28 | public function get_attributesAttribute($value): array |
||||||
| 29 | { |
||||||
| 30 | $value = $value ?? []; |
||||||
| 31 | $value = ! is_array($value) ? json_decode($value, true) : $value; |
||||||
| 32 | |||||||
| 33 | return array_merge( |
||||||
| 34 | $this->schema ?? [], |
||||||
| 35 | $value ?? [] |
||||||
| 36 | ); |
||||||
| 37 | } |
||||||
| 38 | |||||||
| 39 | public function set_attributesAttribute($value) |
||||||
| 40 | { |
||||||
| 41 | $this->attributes[$this->getSchemaColumnName()] = json_encode($value); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 42 | } |
||||||
| 43 | |||||||
| 44 | public function getAttribute($key) |
||||||
| 45 | { |
||||||
| 46 | if (array_key_exists($key, $this->schema)) { |
||||||
| 47 | $value = $this->{$this->getSchemaColumnName()}[$key] ?? null; |
||||||
| 48 | |||||||
| 49 | return isset($this->getCasts()[$key]) ? $this->castAttribute($key, $value) : $value; |
||||||
|
0 ignored issues
–
show
It seems like
getCasts() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
castAttribute() does not exist on PrismX\Schemaless\Traits\HasSchemalessAttributes. Did you maybe mean setAttribute()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 50 | } |
||||||
| 51 | |||||||
| 52 | return parent::getAttribute($key); |
||||||
| 53 | } |
||||||
| 54 | |||||||
| 55 | public function setAttribute($key, $value) |
||||||
| 56 | { |
||||||
| 57 | if (array_key_exists($key, $this->schema)) { |
||||||
| 58 | $this->{$this->getSchemaColumnName()} = array_merge($this->{$this->getSchemaColumnName()}, [$key => $value]); |
||||||
| 59 | |||||||
| 60 | return; |
||||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | parent::setAttribute($key, $value); |
||||||
| 64 | } |
||||||
| 65 | |||||||
| 66 | public function getSchema() |
||||||
| 67 | { |
||||||
| 68 | return collect($this->schema) |
||||||
| 69 | ->mapWithKeys(function ($default, $key) { |
||||||
| 70 | return [$key => $this->getAttribute($key)]; |
||||||
| 71 | }) |
||||||
| 72 | ->toArray(); |
||||||
| 73 | } |
||||||
| 74 | |||||||
| 75 | public function setSchema(array $attributes = []) |
||||||
| 76 | { |
||||||
| 77 | $this->schema = $attributes; |
||||||
| 78 | } |
||||||
| 79 | |||||||
| 80 | public function toArray() |
||||||
| 81 | { |
||||||
| 82 | return array_merge( |
||||||
| 83 | parent::toArray(), |
||||||
| 84 | $this->getSchema() |
||||||
| 85 | ); |
||||||
| 86 | } |
||||||
| 87 | |||||||
| 88 | protected function mutateAttribute($key, $value) |
||||||
| 89 | { |
||||||
| 90 | $attribute = Str::startsWith($key, '_') ? $key : Str::studly($key); |
||||||
| 91 | |||||||
| 92 | return $this->{"get{$attribute}Attribute"}($value); |
||||||
| 93 | } |
||||||
| 94 | |||||||
| 95 | public function hasGetMutator($key) |
||||||
| 96 | { |
||||||
| 97 | $attribute = Str::startsWith($key, '_') ? $key : Str::studly($key); |
||||||
| 98 | |||||||
| 99 | return method_exists($this, "get{$attribute}Attribute"); |
||||||
| 100 | } |
||||||
| 101 | |||||||
| 102 | public function hasSetMutator($key) |
||||||
| 103 | { |
||||||
| 104 | $attribute = Str::startsWith($key, '_') ? $key : Str::studly($key); |
||||||
| 105 | |||||||
| 106 | return method_exists($this, "set{$attribute}Attribute"); |
||||||
| 107 | } |
||||||
| 108 | |||||||
| 109 | protected function setMutatedAttributeValue($key, $value) |
||||||
| 110 | { |
||||||
| 111 | $attribute = Str::startsWith($key, '_') ? $key : Str::studly($key); |
||||||
| 112 | |||||||
| 113 | return $this->{"set{$attribute}Attribute"}($value); |
||||||
| 114 | } |
||||||
| 115 | } |
||||||
| 116 |