Skip to content

Commit 32b968d

Browse files
Merge branch 'release/0.2.0'
2 parents c82ab1e + c89b95e commit 32b968d

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

CHANGELOG.md

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

77
## Unreleased
88

9+
## 0.2.0 (2018-07-26)
10+
11+
### Changed
12+
13+
- Allow using mutators and other features for boolean dates
14+
15+
### Fixed
16+
17+
- Allow multiple traits with the same methods (e.g. `getAttribute`)
18+
919
## 0.1.1 (2018-07-26)
1020

1121
### Fixed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Laravel Boolean Dates
1+
# Easily convert Eloquent model booleans to dates and back
22

33
[![Latest stable release][version-badge]][link-packagist]
44
[![Software license][license-badge]](LICENSE.md)
@@ -10,9 +10,7 @@
1010
[![Follow @sebastiaanluca on Twitter][twitter-profile-badge]][link-twitter]
1111
[![Share this package on Twitter][twitter-share-badge]][link-twitter-share]
1212

13-
__Easily convert Eloquent model booleans to dates and back with Laravel Boolean Dates.__
14-
15-
Sets the date of fields to the current date and time if an inputted counterpart boolean field is true-ish. Ideal for those terms and conditions checkboxes on user registration pages! Now you know _when_ they were accepted and not just _if_ (GDPR anyone?).
13+
__Sets the date of fields to the current date and time if an inputted counterpart boolean field is true-ish.__ Ideal for those terms and conditions checkboxes on user registration pages! Now you know _when_ they were accepted and not just _if_ (GDPR anyone?).
1614

1715
## Table of contents
1816

src/BooleanDates.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getAttribute($key)
4646
}
4747

4848
if ($this->hasBooleanDate($key)) {
49-
return $this->attributes[$this->getBooleanDateField($key)] !== null;
49+
return $this->getBooleanDate($key);
5050
}
5151

5252
return parent::getAttribute($key);
@@ -82,33 +82,37 @@ public function getBooleanDates() : array
8282
/**
8383
* @return array
8484
*/
85-
public function getBooleanDateAttributes() : array
85+
protected function getBooleanDateAttributes() : array
8686
{
8787
return array_intersect_key(
88-
$this->attributes,
88+
$this->getAttributes(),
8989
array_flip($this->getBooleanDates())
9090
);
9191
}
9292

9393
/**
94-
* @param string $key
94+
* @param mixed $key
9595
*
9696
* @return bool
9797
*/
98-
public function hasBooleanDate(string $key) : bool
98+
protected function getBooleanDate($key) : bool
9999
{
100-
return array_key_exists($key, $this->getBooleanDates());
100+
return parent::getAttribute($this->getBooleanDateField($key)) !== null;
101101
}
102102

103103
/**
104104
* @param string $key
105105
* @param mixed $value
106106
*/
107-
public function setBooleanDate(string $key, $value) : void
107+
protected function setBooleanDate(string $key, $value) : void
108108
{
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
}
@@ -118,21 +122,31 @@ public function setBooleanDate(string $key, $value) : void
118122
*
119123
* @return bool
120124
*/
121-
public function currentBooleanDateFieldValueIsNotYetSet(string $key) : bool
125+
protected function hasBooleanDate(string $key) : bool
122126
{
123-
if (! array_key_exists($this->getBooleanDateField($key), $this->attributes)) {
127+
return array_key_exists($key, $this->getBooleanDates());
128+
}
129+
130+
/**
131+
* @param mixed $key
132+
*
133+
* @return bool
134+
*/
135+
protected function currentBooleanDateFieldValueIsNotYetSet($key) : bool
136+
{
137+
if (! array_key_exists($this->getBooleanDateField($key), $this->getAttributes())) {
124138
return true;
125139
}
126140

127-
return $this->attributes[$this->getBooleanDateField($key)] === null;
141+
return parent::getAttribute($this->getBooleanDateField($key)) === null;
128142
}
129143

130144
/**
131-
* @param string $key
145+
* @param mixed $key
132146
*
133147
* @return string
134148
*/
135-
public function getBooleanDateField(string $key) : string
149+
protected function getBooleanDateField($key) : string
136150
{
137151
return $this->booleanDates[$key];
138152
}
@@ -142,7 +156,7 @@ public function getBooleanDateField(string $key) : string
142156
*
143157
* @return string|null
144158
*/
145-
public function getNewBooleanDateValue($value) : ?string
159+
protected function getNewBooleanDateValue($value) : ?string
146160
{
147161
return $value ? $this->fromDateTime(Carbon::now()) : null;
148162
}

0 commit comments

Comments
 (0)