independent-analytics
/
vendor
/
illuminate
/
database
/
Eloquent
/
Relations
/
Concerns
/
InteractsWithDictionary.php
independent-analytics
/
vendor
/
illuminate
/
database
/
Eloquent
/
Relations
/
Concerns
Last commit date
AsPivot.php
1 month ago
CanBeOneOfMany.php
1 month ago
ComparesRelatedModels.php
2 years ago
InteractsWithDictionary.php
1 month ago
InteractsWithPivotTable.php
1 month ago
SupportsDefaultModels.php
2 years ago
InteractsWithDictionary.php
33 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Eloquent\Relations\Concerns; |
| 4 | |
| 5 | use BackedEnum; |
| 6 | use IAWPSCOPED\Doctrine\Instantiator\Exception\InvalidArgumentException; |
| 7 | use UnitEnum; |
| 8 | /** @internal */ |
| 9 | trait InteractsWithDictionary |
| 10 | { |
| 11 | /** |
| 12 | * Get a dictionary key attribute - casting it to a string if necessary. |
| 13 | * |
| 14 | * @param mixed $attribute |
| 15 | * @return mixed |
| 16 | * |
| 17 | * @throws \Doctrine\Instantiator\Exception\InvalidArgumentException |
| 18 | */ |
| 19 | protected function getDictionaryKey($attribute) |
| 20 | { |
| 21 | if (\is_object($attribute)) { |
| 22 | if (\method_exists($attribute, '__toString')) { |
| 23 | return $attribute->__toString(); |
| 24 | } |
| 25 | if (\function_exists('enum_exists') && $attribute instanceof UnitEnum) { |
| 26 | return $attribute instanceof BackedEnum ? $attribute->value : $attribute->name; |
| 27 | } |
| 28 | throw new InvalidArgumentException('Model attribute value is an object but does not have a __toString method.'); |
| 29 | } |
| 30 | return $attribute; |
| 31 | } |
| 32 | } |
| 33 |