Skip to content

Commit

Permalink
Fast Attribute Access (#2921)
Browse files Browse the repository at this point in the history
Improve performance for simple model attribute
  • Loading branch information
tabuna authored Nov 8, 2024
1 parent 603b70c commit df2e842
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Screen/AsSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}

0 comments on commit df2e842

Please sign in to comment.