ExecuteQueries.php
3 weeks ago
GuardAttributes.php
3 weeks ago
HasAttributes.php
3 weeks ago
HasDictionary.php
3 weeks ago
HasRelationships.php
3 weeks ago
HasTimestamps.php
3 weeks ago
RelationshipQueries.php
3 weeks ago
HasDictionary.php
43 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Trait providing dictionary key normalization for model collections. |
| 5 | * Converts attribute keys to strings via __toString or casting, throwing on unsupported types. |
| 6 | * Ensures collection merge and lookup use consistent primary-key indexing. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Database\Concerns |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Database\Concerns; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use InvalidArgumentException; |
| 16 | trait HasDictionary |
| 17 | { |
| 18 | /** |
| 19 | * Get the dictionary key for the attribute. |
| 20 | * |
| 21 | * @param mixed $attribute The attribute to get the key for |
| 22 | * |
| 23 | * @return string The dictionary key |
| 24 | * |
| 25 | * @throws \InvalidArgumentException |
| 26 | * |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | protected function get_dictionary_key($attribute) |
| 30 | { |
| 31 | if (\is_null($attribute) || \is_string($attribute) || \is_int($attribute)) { |
| 32 | return $attribute; |
| 33 | } |
| 34 | if (\is_object($attribute)) { |
| 35 | if (\method_exists($attribute, '__toString')) { |
| 36 | return $attribute->__toString(); |
| 37 | } |
| 38 | throw new InvalidArgumentException('Attribute must be a string, integer, or object with a __toString method.'); |
| 39 | } |
| 40 | return (string) $attribute; |
| 41 | } |
| 42 | } |
| 43 |