-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix casting issues (issue: #2703) #2705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
38b58b8
Add Encryption cast test
stubbo 4d12cba
Add Encryption casts tests for array, object, collection
stubbo 36dc1a8
Clean up asDateTime method on model
stubbo e2d2931
Removed janky fromJson override
stubbo 3d11dd2
Reduce asDecimal override by using parent::asDecimal
stubbo b76f575
Cast to native mongo Decimal128
stubbo 5a7f718
Add more cast tests
stubbo 9acd088
apply phpcbf formatting
stubbo 2a9d836
Cleanup casting in Model::setAttribute
stubbo c55b2af
Use $now insead of a now()
stubbo b0fdb1f
Improve casting from BSON to decimal
stubbo 0dd845a
Fix queue tests which have delay
stubbo fe9ed89
Merge branch '4.1' into 4.1
GromNaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,14 @@ | |
|
||
namespace MongoDB\Laravel\Eloquent; | ||
|
||
use Brick\Math\BigDecimal; | ||
use Brick\Math\Exception\MathException as BrickMathException; | ||
use Brick\Math\RoundingMode; | ||
use Carbon\CarbonInterface; | ||
use DateTimeInterface; | ||
use Illuminate\Contracts\Queue\QueueableCollection; | ||
use Illuminate\Contracts\Queue\QueueableEntity; | ||
use Illuminate\Contracts\Support\Arrayable; | ||
use Illuminate\Database\Eloquent\Casts\Json; | ||
use Illuminate\Database\Eloquent\Model as BaseModel; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Exceptions\MathException; | ||
use Illuminate\Support\Facades\Date; | ||
use Illuminate\Support\Str; | ||
use MongoDB\BSON\Binary; | ||
|
@@ -25,7 +20,6 @@ | |
use MongoDB\BSON\UTCDateTime; | ||
use MongoDB\Laravel\Query\Builder as QueryBuilder; | ||
|
||
use function abs; | ||
use function array_key_exists; | ||
use function array_keys; | ||
use function array_merge; | ||
|
@@ -41,7 +35,6 @@ | |
use function is_string; | ||
use function ltrim; | ||
use function method_exists; | ||
use function sprintf; | ||
use function str_contains; | ||
use function str_starts_with; | ||
use function strcmp; | ||
|
@@ -139,15 +132,9 @@ public function fromDateTime($value) | |
/** @inheritdoc */ | ||
protected function asDateTime($value) | ||
{ | ||
// Convert UTCDateTime instances. | ||
// Convert UTCDateTime instances to Carbon. | ||
if ($value instanceof UTCDateTime) { | ||
$date = $value->toDateTime(); | ||
|
||
$seconds = $date->format('U'); | ||
$milliseconds = abs((int) $date->format('v')); | ||
$timestampMs = sprintf('%d%03d', $seconds, $milliseconds); | ||
|
||
return Date::createFromTimestampMs($timestampMs); | ||
return Date::instance($value->toDateTime()); | ||
} | ||
|
||
return parent::asDateTime($value); | ||
|
@@ -250,9 +237,16 @@ public function setAttribute($key, $value) | |
{ | ||
$key = (string) $key; | ||
|
||
// Add casts | ||
if ($this->hasCast($key)) { | ||
$value = $this->castAttribute($key, $value); | ||
$casts = $this->getCasts(); | ||
if (array_key_exists($key, $casts)) { | ||
$castType = $this->getCastType($key); | ||
$castOptions = Str::after($casts[$key], ':'); | ||
|
||
// Can add more native mongo type casts here. | ||
$value = match (true) { | ||
$castType === 'decimal' => $this->fromDecimal($value, $castOptions), | ||
default => $value, | ||
}; | ||
} | ||
|
||
// Convert _id to ObjectID. | ||
|
@@ -284,23 +278,25 @@ public function setAttribute($key, $value) | |
/** @inheritdoc */ | ||
protected function asDecimal($value, $decimals) | ||
{ | ||
try { | ||
$value = (string) BigDecimal::of((string) $value)->toScale((int) $decimals, RoundingMode::HALF_UP); | ||
|
||
return new Decimal128($value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Good catch, the method |
||
} catch (BrickMathException $e) { | ||
throw new MathException('Unable to cast value to a decimal.', previous: $e); | ||
if ($value instanceof Decimal128) { | ||
stubbo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Convert it to a string to round, want to make it act exactly like we expect. | ||
$value = (string) $value; | ||
} | ||
|
||
return parent::asDecimal($value, $decimals); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public function fromJson($value, $asObject = false) | ||
/** | ||
* Change to mongo native for decimal cast. | ||
* | ||
* @param mixed $value | ||
* @param int $decimals | ||
* | ||
* @return Decimal128 | ||
*/ | ||
protected function fromDecimal($value, $decimals) | ||
{ | ||
if (! is_string($value)) { | ||
$value = Json::encode($value); | ||
} | ||
|
||
return Json::decode($value, ! $asObject); | ||
return new Decimal128($this->asDecimal($value, $decimals)); | ||
} | ||
|
||
/** @inheritdoc */ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.