From e13b5d31e6463ae98f78e4cd3e78a68235de83bd Mon Sep 17 00:00:00 2001 From: Alexandr Chernyaev Date: Sat, 9 Nov 2024 01:28:16 +0300 Subject: [PATCH 1/2] Improve perfomance for simple model attribute --- src/Screen/AsSource.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Screen/AsSource.php b/src/Screen/AsSource.php index a2a1e2a278..ae4aad9fe7 100644 --- a/src/Screen/AsSource.php +++ b/src/Screen/AsSource.php @@ -22,10 +22,16 @@ trait AsSource * * @return mixed|null The value of the field, or null if the field is not found. */ - public function getContent(string $field) + public function getContent(string $field): mixed { - return Arr::get($this->toArray(), $field) // Try to get the field value from the object's array representation. + // When field does not contain a dot, it is a simple field name. + // And we not need to use cast model to array for this case. + if (!str_contains($field, '.')) { + return $this->getAttribute($field); + } + + return $this->getAttribute($field) // Try to get the field value from the object's attributes. ?? Arr::get($this->getRelations(), $field) // Try to get the field value from the object's relations. - ?? $this->getAttribute($field); // Try to get the field value from the object's attributes. + ?? Arr::get($this, $field); // Try to get the field value from the object's array representation. } } From c4da19c3e8fa2d48e9ab70c6e440ac2449ae627d Mon Sep 17 00:00:00 2001 From: tabuna Date: Fri, 8 Nov 2024 22:29:13 +0000 Subject: [PATCH 2/2] Fixed code style --- src/Screen/AsSource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Screen/AsSource.php b/src/Screen/AsSource.php index ae4aad9fe7..1f3fcd1970 100644 --- a/src/Screen/AsSource.php +++ b/src/Screen/AsSource.php @@ -26,7 +26,7 @@ public function getContent(string $field): mixed { // When field does not contain a dot, it is a simple field name. // And we not need to use cast model to array for this case. - if (!str_contains($field, '.')) { + if (! str_contains($field, '.')) { return $this->getAttribute($field); }