joskolenberg /
eloquent-empty-date-nullifier
| 1 | <?php |
||||||||
| 2 | |||||||||
| 3 | namespace JosKolenberg\EloquentEmptyDateNullifier; |
||||||||
| 4 | |||||||||
| 5 | trait NullifiesEmptyDates |
||||||||
| 6 | { |
||||||||
| 7 | /** |
||||||||
| 8 | * Add the date attributes to the attributes array. |
||||||||
| 9 | * |
||||||||
| 10 | * @param array $attributes |
||||||||
| 11 | * @return array |
||||||||
| 12 | */ |
||||||||
| 13 | 16 | protected function addDateAttributesToArray(array $attributes) |
|||||||
| 14 | { |
||||||||
| 15 | 16 | foreach ($this->getDates() as $key) { |
|||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||||
| 16 | 16 | if (! isset($attributes[$key])) { |
|||||||
| 17 | 16 | continue; |
|||||||
| 18 | } |
||||||||
| 19 | |||||||||
| 20 | /** |
||||||||
| 21 | * Additional check for valid date. |
||||||||
| 22 | */ |
||||||||
| 23 | 16 | if (! $this->isValidDate($attributes[$key])) { |
|||||||
| 24 | 16 | $attributes[$key] = null; |
|||||||
| 25 | |||||||||
| 26 | 16 | continue; |
|||||||
| 27 | } |
||||||||
| 28 | |||||||||
| 29 | 16 | $attributes[$key] = $this->serializeDate($this->asDateTime($attributes[$key])); |
|||||||
|
0 ignored issues
–
show
It seems like
asDateTime() 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...
It seems like
serializeDate() 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...
|
|||||||||
| 30 | } |
||||||||
| 31 | |||||||||
| 32 | 16 | return $attributes; |
|||||||
| 33 | } |
||||||||
| 34 | |||||||||
| 35 | /** |
||||||||
| 36 | * Cast an attribute to a native PHP type. |
||||||||
| 37 | * |
||||||||
| 38 | * @param string $key |
||||||||
| 39 | * @param mixed $value |
||||||||
| 40 | * @return mixed |
||||||||
| 41 | */ |
||||||||
| 42 | 20 | protected function castAttribute($key, $value) |
|||||||
| 43 | { |
||||||||
| 44 | /** |
||||||||
| 45 | * Additional check for valid date if it needs casting |
||||||||
| 46 | * to a date. Proceed to normal behaviour otherwise. |
||||||||
| 47 | */ |
||||||||
| 48 | 20 | if (in_array($this->getCastType($key), [ |
|||||||
|
0 ignored issues
–
show
It seems like
getCastType() 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...
|
|||||||||
| 49 | 20 | 'date', |
|||||||
| 50 | 'datetime', |
||||||||
| 51 | 'custom_datetime', |
||||||||
| 52 | 'timestamp', |
||||||||
| 53 | 20 | ]) && ! $this->isValidDate($value)) { |
|||||||
| 54 | 20 | return null; |
|||||||
| 55 | } |
||||||||
| 56 | |||||||||
| 57 | 16 | return parent::castAttribute($key, $value); |
|||||||
| 58 | } |
||||||||
| 59 | |||||||||
| 60 | /** |
||||||||
| 61 | * Check if the given value is not on the empty dates blacklist. |
||||||||
| 62 | * |
||||||||
| 63 | * @param $value |
||||||||
| 64 | * @return bool |
||||||||
| 65 | */ |
||||||||
| 66 | 20 | protected function isValidDate($value) |
|||||||
| 67 | { |
||||||||
| 68 | 20 | return ! in_array($value, [ |
|||||||
| 69 | 20 | '0000-00-00 00:00:00', |
|||||||
| 70 | '0000-00-00', |
||||||||
| 71 | ]); |
||||||||
| 72 | } |
||||||||
| 73 | |||||||||
| 74 | /** |
||||||||
| 75 | * Get a plain attribute (not a relationship). |
||||||||
| 76 | * |
||||||||
| 77 | * Additional check for valid date. Proceed to |
||||||||
| 78 | * normal behaviour when date is valid. |
||||||||
| 79 | * |
||||||||
| 80 | * @param string $key |
||||||||
| 81 | * @return mixed |
||||||||
| 82 | */ |
||||||||
| 83 | 12 | public function getAttributeValue($key) |
|||||||
| 84 | { |
||||||||
| 85 | 12 | $value = $this->getAttributeFromArray($key); |
|||||||
|
0 ignored issues
–
show
The method
getAttributeFromArray() does not exist on JosKolenberg\EloquentEmp...ier\NullifiesEmptyDates. Did you maybe mean getAttributeValue()?
(
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...
|
|||||||||
| 86 | |||||||||
| 87 | 12 | if (in_array($key, $this->getDates()) && ! $this->isValidDate($value)) { |
|||||||
| 88 | 4 | return null; |
|||||||
| 89 | } |
||||||||
| 90 | |||||||||
| 91 | 12 | return parent::getAttributeValue($key); |
|||||||
| 92 | } |
||||||||
| 93 | |||||||||
| 94 | /** |
||||||||
| 95 | * Convert a DateTime to a storable string. |
||||||||
| 96 | * |
||||||||
| 97 | * Additional check for valid date. Proceed to |
||||||||
| 98 | * normal behaviour when date is valid. |
||||||||
| 99 | * |
||||||||
| 100 | * @param mixed $value |
||||||||
| 101 | * @return string|null |
||||||||
| 102 | */ |
||||||||
| 103 | 8 | public function fromDateTime($value) |
|||||||
| 104 | { |
||||||||
| 105 | 8 | if (! $this->isValidDate($value)) { |
|||||||
| 106 | 8 | return null; |
|||||||
| 107 | } |
||||||||
| 108 | |||||||||
| 109 | 8 | return parent::fromDateTime($value); |
|||||||
| 110 | } |
||||||||
| 111 | } |