8000 [6.x] The `hasAttributes` trait is divided into two: `hasAttributes` … · laravel/framework@4d4c098 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d4c098

Browse files
author
Andrey Helldar
committed
[6.x] The hasAttributes trait is divided into two: hasAttributes and hasCasts
1 parent a631793 commit 4d4c098

File tree

3 files changed

+277
-257
lines changed

3 files changed

+277
-257
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 0 additions & 257 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
use Carbon\CarbonInterface;
66
use DateTimeInterface;
77
use Illuminate\Contracts\Support\Arrayable;
8-
use Illuminate\Database\Eloquent\JsonEncodingException;
98
use Illuminate\Database\Eloquent\Relations\Relation;
109
use Illuminate\Support\Arr;
1110
use Illuminate\Support\Carbon;
12-
use Illuminate\Support\Collection as BaseCollection;
1311
use Illuminate\Support\Facades\Date;
1412
use Illuminate\Support\Str;
1513
use LogicException;
@@ -37,13 +35,6 @@ trait HasAttributes
3735
*/
3836
protected $changes = [];
3937

40-
/**
41-
* The attributes that should be cast to native types.
42-
*
43-
* @var array
44-
*/
45-
protected $casts = [];
46-
4738
/**
4839
* The attributes that should be mutated to dates.
4940
*
@@ -163,47 +154,6 @@ protected function addMutatedAttributesToArray(array $attributes, array $mutated
163154
return $attributes;
164155
}
165156

166-
/**
167-
* Add the casted attributes to the attributes array.
168-
*
169-
* @param array $attributes
170-
* @param array $mutatedAttributes
171-
* @return array
172-
*/
173-
protected function addCastAttributesToArray(array $attributes, array $mutatedAttributes)
174-
{
175-
foreach ($this->getCasts() as $key => $value) {
176-
if (! array_key_exists($key, $attributes) || in_array($key, $mutatedAttributes)) {
177-
continue;
178-
}
179-
180-
// Here we will cast the attribute. Then, if the cast is a date or datetime cast
181-
// then we will serialize the date for the array. This will convert the dates
182-
// to strings based on the date format specified for these Eloquent models.
183-
$attributes[$key] = $this->castAttribute(
184-
$key, $attributes[$key]
185-
);
186-
187-
// If the attribute cast was a date or a datetime, we will serialize the date as
188-
// a string. This allows the developers to customize how dates are serialized
189-
// into an array without affecting how they are persisted into the storage.
190-
if ($attributes[$key] &&
191-
($value === 'date' || $value === 'datetime')) {
192-
$attributes[$key] = $this->serializeDate($attributes[$key]);
193-
}
194-
195-
if ($attributes[$key] && $this->isCustomDateTimeCast($value)) {
196-
$attributes[$key] = $attributes[$key]->format(explode(':', $value, 2)[1]);
197-
}
198-
199-
if ($attributes[$key] instanceof Arrayable) {
200-
$attributes[$key] = $attributes[$key]->toArray();
201-
}
202-
}
203-
204-
return $attributes;
205-
}
206-
207157
/**
208158
* Get an attribute array of all arrayable attributes.
209159
*
@@ -468,95 +418,6 @@ protected function mutateAttributeForArray($key, $value)
468418
return $value instanceof Arrayable ? $value->toArray() : $value;
469419
}
470420

471-
/**
472-
* Cast an attribute to a native PHP type.
473-
*
474-
* @param string $key
475-
* @param mixed $value
476-
* @return mixed
477-
*/
478-
protected function castAttribute($key, $value)
479-
{
480-
if (is_null($value)) {
481-
return $value;
482-
}
483-
484-
switch ($this->getCastType($key)) {
485-
case 'int':
486-
case 'integer':
487-
return (int) $value;
488-
case 'real':
489-
case 'float':
490-
case 'double':
491-
return $this->fromFloat($value);
492-
case 'decimal':
493-
return $this->asDecimal($value, explode(':', $this->getCasts()[$key], 2)[1]);
494-
case 'string':
495-
return (string) $value;
496-
case 'bool':
497-
case 'boolean':
498-
return (bool) $value;
499-
case 'object':
500-
return $this->fromJson($value, true);
501-
case 'array':
502-
case 'json':
503-
return $this->fromJson($value);
504-
case 'collection':
505-
return new BaseCollection($this->fromJson($value));
506-
case 'date':
507-
return $this->asDate($value);
508-
case 'datetime':
509-
case 'custom_datetime':
510-
return $this->asDateTime($value);
511-
case 'timestamp':
512-
return $this->asTimestamp($value);
513-
default:
514-
return $value;
515-
}
516-
}
517-
518-
/**
519-
* Get the type of cast for a model attribute.
520-
*
521-
* @param string $key
522-
* @return string
523-
*/
524-
protected function getCastType($key)
525-
{
526-
if ($this->isCustomDateTimeCast($this->getCasts()[$key])) {
527-
return 'custom_datetime';
528-
}
529-
530-
if ($this->isDecimalCast($this->getCasts()[$key])) {
531-
return 'decimal';
532-
}
533-
534-
return trim(strtolower($this->getCasts()[$key]));
535-
}
536-
537-
/**
538-
* Determine if the cast type is a custom date time cast.
539-
*
540-
* @param string $cast
541-
* @return bool
542-
*/
543-
protected function isCustomDateTimeCast($cast)
544-
{
545-
return strncmp($cast, 'date:', 5) === 0 ||
546-
strncmp($cast, 'datetime:', 9) === 0;
547-
}
548-
549-
/**
550-
* Determine if the cast type is a decimal cast.
551-
*
552-
* @param string $cast
553-
* @return bool
554-
*/
555-
protected function isDecimalCast($cast)
556-
{
557-
return strncmp($cast, 'decimal:', 8) === 0;
558-
}
559-
560421
/**
561422
* Set a given attribute on the model.
562423
*
@@ -619,18 +480,6 @@ protected function setMutatedAttributeValue($key, $value)
619480
return $this->{'set'.Str::studly($key).'Attribute'}($value);
620481
}
621482

622-
/**
623-
* Determine if the given attribute is a date or date castable.
624-
*
625-
* @param string $key
626-
* @return bool
627-
*/
628-
protected function isDateAttribute($key)
629-
{
630-
return in_array($key, $this->getDates(), true) ||
631-
$this->isDateCastable($key);
632-
}
633-
634483
/**
635484
* Set a given JSON attribute on the model.
636485
*
@@ -676,26 +525,6 @@ protected function getArrayAttributeByKey($key)
676525
$this->fromJson($this->attributes[$key]) : [];
677526
}
678527

679-
/**
680-
* Cast the given attribute to JSON.
681-
*
682-
* @param string $key
683-
* @param mixed $value
684-
* @return string
685-
*/
686-
protected function castAttributeAsJson($key, $value)
687-
{
688-
$value = $this->asJson($value);
689-
690-
if ($value === false) {
691-
throw JsonEncodingException::forAttribute(
692-
$this, $key, json_last_error_msg()
693-
);
694-
}
695-
696-
return $value;
697-
}
698-
699528
/**
700529
* Encode the given value as JSON.
701530
*
@@ -899,58 +728,6 @@ public function setDateFormat($format)
899728
return $this;
900729
}
901730

902-
/**
903-
* Determine whether an attribute should be cast to a native type.
904-
*
905-
* @param string $key
906-
* @param array|string|null $types
907-
* @return bool
908-
*/
909-
public function hasCast($key, $types = null)
910-
{
911-
if (array_key_exists($key, $this->getCasts())) {
912-
return $types ? in_array($this->getCastType($key), (array) $types, true) : true;
913-
}
914-
915-
return false;
916-
}
917-
918-
/**
919-
* Get the casts array.
920-
*
921-
* @return array
922-
*/
923-
public function getCasts()
924-
{
925-
if ($this->getIncrementing()) {
926-
return array_merge([$this->getKeyName() => $this->getKeyType()], $this->casts);
927-
}
928-
929-
return $this->casts;
930-
}
931-
932-
/**
933-
* Determine whether a value is Date / DateTime castable for inbound manipulation.
934-
*
935-
* @param string $key
936-
* @return bool
937-
*/
938-
protected function isDateCastable($key)
939-
{
940-
return $this->hasCast($key, ['date', 'datetime']);
941-
}
942-
943-
/**
944-
* Determine whether a value is JSON castable for inbound manipulation.
945-
*
946-
* @param string $key
947-
* @return bool
948-
*/
949-
protected function isJsonCastable($key)
950-
{
951-
return $this->hasCast($key, ['array', 'json', 'object', 'collection']);
952-
}
953-
954731
/**
955732
* Get all of the current attributes on the model.
956733
*
@@ -1153,40 +930,6 @@ public function getChanges()
1153930
return $this->changes;
1154931
}
1155932

1156-
/**
1157-
* Determine if the new and old values for a given key are equivalent.
1158-
*
1159-
* @param string $key
1160-
* @param mixed $current
1161-
* @return bool
1162-
*/
1163-
public function originalIsEquivalent($key, $current)
1164-
{
1165-
if (! array_key_exists($key, $this->original)) {
1166-
return false;
1167-
}
1168-
1169-
$original = $this->getOriginal($key);
1170-
1171-
if ($current === $original) {
1172-
return true;
1173-
} elseif (is_null($current)) {
1174-
return false;
1175-
} elseif ($this->isDateAttribute($key)) {
1176-
return $this->fromDateTime($current) ===
1177-
$this->fromDateTime($original);
1178-
} elseif ($this->hasCast($key, ['object', 'collection'])) {
1179-
return $this->castAttribute($key, $current) ==
1180-
$this->castAttribute($key, $original);
1181-
} elseif ($this->hasCast($key)) {
1182-
return $this->castAttribute($key, $current) ===
1183-
$this->castAttribute($key, $original);
1184-
}
1185-
1186-
return is_numeric($current) && is_numeric($original)
1187-
&& strcmp((string) $current, (string) $original) === 0;
1188-
}
1189-
1190933
/**
1191934
* Append attributes to query when building a query.
1192935
*

0 commit comments

Comments
 (0)
0