swoole-framwork/vendor/illuminate/database/Eloquent/Relations/Concerns/InteractsWithDictionary.php

36 lines
912 B
PHP
Raw Normal View History

2024-05-20 11:51:07 +08:00
<?php
namespace Illuminate\Database\Eloquent\Relations\Concerns;
use BackedEnum;
use InvalidArgumentException;
use UnitEnum;
trait InteractsWithDictionary
{
/**
* Get a dictionary key attribute - casting it to a string if necessary.
*
* @param mixed $attribute
* @return mixed
*
* @throws \InvalidArgumentException
*/
protected function getDictionaryKey($attribute)
{
if (is_object($attribute)) {
if (method_exists($attribute, '__toString')) {
return $attribute->__toString();
}
if ($attribute instanceof UnitEnum) {
return $attribute instanceof BackedEnum ? $attribute->value : $attribute->name;
}
throw new InvalidArgumentException('Model attribute value is an object but does not have a __toString method.');
}
return $attribute;
}
}