Skip to content

Commit 2ab9504

Browse files
committed
Refactoring of casting
1 parent e589d0f commit 2ab9504

1 file changed

Lines changed: 90 additions & 75 deletions

File tree

src/Database/Barry/Model.php

Lines changed: 90 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public static function retrieveAndDelete(
337337
}
338338

339339
if ($model instanceof Collection) {
340-
$model->dropAll();
340+
$model->delete();
341341
return $model;
342342
}
343343

@@ -904,56 +904,7 @@ public function __get(string $name): mixed
904904
return null;
905905
}
906906

907-
if (in_array($name, $this->mutableDateAttributes())) {
908-
return new Carbon($this->attributes[$name]);
909-
}
910-
911-
if (array_key_exists($name, $this->casts)) {
912-
$type = $this->casts[$name];
913-
$value = $this->attributes[$name];
914-
if ($type === "date") {
915-
return new Carbon($value);
916-
}
917-
if ($type === "int") {
918-
return (int)$value;
919-
}
920-
if ($type === "float") {
921-
return (float)$value;
922-
}
923-
if ($type === "double") {
924-
return (double)$value;
925-
}
926-
if ($type === "json") {
927-
if (is_array($value)) {
928-
return (object)$value;
929-
}
930-
if (is_object($value)) {
931-
return (object)$value;
932-
}
933-
return json_decode(
934-
$value,
935-
false,
936-
512,
937-
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_IGNORE
938-
);
939-
}
940-
if ($type === "array") {
941-
if (is_array($value)) {
942-
return (array)$value;
943-
}
944-
if (is_object($value)) {
945-
return (array)$value;
946-
}
947-
return json_decode(
948-
$value,
949-
true,
950-
512,
951-
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_IGNORE
952-
);
953-
}
954-
}
955-
956-
return $this->attributes[$name];
907+
return $this->executeDataCasting($name);
957908
}
958909

959910
/**
@@ -968,28 +919,29 @@ public function __set(string $name, mixed $value)
968919
}
969920

970921
/**
971-
* Lists of mutable properties
922+
* __toString
972923
*
973-
* @return array
924+
* @return string
974925
*/
975-
private function mutableDateAttributes(): array
926+
public function __toString(): string
976927
{
977-
return array_merge(
978-
$this->dates,
979-
[
980-
$this->created_at, $this->updated_at, 'expired_at', 'logged_at', 'signed_at'
981-
]
982-
);
928+
foreach ($this->attributes as $name => $value) {
929+
$this->attributes[$name] = $this->executeDataCasting($name);
930+
}
931+
932+
return $this->toJson();
983933
}
984934

985935
/**
986-
* __toString
936+
* Lists of mutable properties
987937
*
988-
* @return string
938+
* @return array
989939
*/
990-
public function __toString(): string
940+
private function mutableDateAttributes(): array
991941
{
992-
return $this->toJson();
942+
return array_merge($this->dates, [
943+
$this->created_at, $this->updated_at, 'expired_at', 'logged_at', 'signed_at'
944+
]);
993945
}
994946

995947
/**
@@ -999,13 +951,11 @@ public function __toString(): string
999951
*/
1000952
public function toJson(): string
1001953
{
1002-
$data = array_filter(
1003-
$this->attributes,
1004-
function ($key) {
1005-
return !in_array($key, $this->hidden);
1006-
},
1007-
ARRAY_FILTER_USE_KEY
1008-
);
954+
foreach ($this->attributes as $name => $value) {
955+
$this->attributes[$name] = $this->executeDataCasting($name);
956+
}
957+
958+
$data = array_filter($this->attributes, fn ($key) => !in_array($key, $this->hidden), ARRAY_FILTER_USE_KEY);
1009959

1010960
return json_encode($data);
1011961
}
@@ -1025,9 +975,74 @@ public function __call(string $name, array $arguments = [])
1025975
return call_user_func_array([$model, $name], $arguments);
1026976
}
1027977

1028-
throw new BadMethodCallException(
1029-
'method ' . $name . ' is not defined.',
1030-
E_ERROR
1031-
);
978+
throw new BadMethodCallException('Method ' . $name . ' is not defined.', E_ERROR);
979+
}
980+
981+
/**
982+
* Executes data casting for a given attribute name
983+
*
984+
* @param string $name
985+
* @return mixed
986+
*/
987+
private function executeDataCasting(string $name): mixed
988+
{
989+
if (in_array($name, $this->mutableDateAttributes())) {
990+
return new Carbon($this->attributes[$name]);
991+
}
992+
993+
if (!array_key_exists($name, $this->casts)) {
994+
return $this->attributes[$name];
995+
}
996+
997+
$type = $this->casts[$name];
998+
$value = $this->attributes[$name];
999+
1000+
if ($type === "date") {
1001+
return new Carbon($value);
1002+
}
1003+
1004+
if ($type === "int") {
1005+
return (int)$value;
1006+
}
1007+
1008+
if ($type === "float") {
1009+
return (float)$value;
1010+
}
1011+
1012+
if ($type === "double") {
1013+
return (float)$value;
1014+
}
1015+
1016+
if ($type === "json") {
1017+
if (is_array($value)) {
1018+
return (object)$value;
1019+
}
1020+
if (is_object($value)) {
1021+
return (object)$value;
1022+
}
1023+
return json_decode(
1024+
$value,
1025+
false,
1026+
512,
1027+
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_IGNORE
1028+
);
1029+
}
1030+
1031+
if ($type === "array") {
1032+
if (is_array($value)) {
1033+
return (array) $value;
1034+
}
1035+
if (is_object($value)) {
1036+
return (array) $value;
1037+
}
1038+
return json_decode(
1039+
$value,
1040+
true,
1041+
512,
1042+
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_IGNORE
1043+
);
1044+
}
1045+
1046+
return $this->attributes[$name];
10321047
}
10331048
}

0 commit comments

Comments
 (0)