2021-08-27 06:46:27 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Illuminate\Database\Eloquent\Relations\Concerns;
|
|
|
|
|
2022-03-14 16:22:30 -04:00
|
|
|
use BackedEnum;
|
2021-08-27 06:46:27 -04:00
|
|
|
use Doctrine\Instantiator\Exception\InvalidArgumentException;
|
|
|
|
|
|
|
|
trait InteractsWithDictionary
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Get a dictionary key attribute - casting it to a string if necessary.
|
|
|
|
*
|
|
|
|
* @param mixed $attribute
|
|
|
|
* @return mixed
|
|
|
|
*
|
|
|
|
* @throws \Doctrine\Instantiator\Exception\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
protected function getDictionaryKey($attribute)
|
|
|
|
{
|
|
|
|
if (is_object($attribute)) {
|
|
|
|
if (method_exists($attribute, '__toString')) {
|
|
|
|
return $attribute->__toString();
|
|
|
|
}
|
|
|
|
|
2022-03-14 16:22:30 -04:00
|
|
|
if (function_exists('enum_exists') &&
|
|
|
|
$attribute instanceof BackedEnum) {
|
|
|
|
return $attribute->value;
|
|
|
|
}
|
|
|
|
|
2021-08-27 06:46:27 -04:00
|
|
|
throw new InvalidArgumentException('Model attribute value is an object but does not have a __toString method.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $attribute;
|
|
|
|
}
|
|
|
|
}
|