Skip to content

Commit 8762694

Browse files
Allow using mutators and other features for boolean dates
1 parent 3831632 commit 8762694

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
66

77
## Unreleased
88

9+
### Changed
10+
11+
- Allow using mutators and other features for boolean dates
12+
913
### Fixed
1014

1115
- Allow multiple traits with the same methods (e.g. `getAttribute`)

src/BooleanDates.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function getBooleanDates() : array
8585
protected function getBooleanDateAttributes() : array
8686
{
8787
return array_intersect_key(
88-
$this->attributes,
88+
$this->getAttributes(),
8989
array_flip($this->getBooleanDates())
9090
);
9191
}
@@ -97,7 +97,7 @@ protected function getBooleanDateAttributes() : array
9797
*/
9898
protected function getBooleanDate($key) : bool
9999
{
100-
return $this->attributes[$this->getBooleanDateField($key)] !== null;
100+
return parent::getAttribute($this->getBooleanDateField($key)) !== null;
101101
}
102102

103103
/**
@@ -109,6 +109,10 @@ protected function setBooleanDate(string $key, $value) : void
109109
// Only update the timestamp if the value is true and if it's not yet set
110110
// or if the value is false and we need to unset the field.
111111
if (! $value || ($value && $this->currentBooleanDateFieldValueIsNotYetSet($key))) {
112+
// Set the value directly on the attributes array, don't use
113+
// setAttribute, and don't receive $200. (\) (°,,,°) (/)
114+
// This allows us to format and set the datetime ourselves,
115+
// and makes using the $dates field optional.
112116
$this->attributes[$this->getBooleanDateField($key)] = $this->getNewBooleanDateValue($value);
113117
}
114118
}
@@ -130,11 +134,11 @@ protected function hasBooleanDate(string $key) : bool
130134
*/
131135
protected function currentBooleanDateFieldValueIsNotYetSet($key) : bool
132136
{
133-
if (! array_key_exists($this->getBooleanDateField($key), $this->attributes)) {
137+
if (! array_key_exists($this->getBooleanDateField($key), $this->getAttributes())) {
134138
return true;
135139
}
136140

137-
return $this->attributes[$this->getBooleanDateField($key)] === null;
141+
return parent::getAttribute($this->getBooleanDateField($key)) === null;
138142
}
139143

140144
/**

0 commit comments

Comments
 (0)