code
stringlengths 31
1.39M
| docstring
stringlengths 23
16.8k
| func_name
stringlengths 1
126
| language
stringclasses 1
value | repo
stringlengths 7
63
| path
stringlengths 7
166
| url
stringlengths 50
220
| license
stringclasses 7
values |
---|---|---|---|---|---|---|---|
public static function reguard()
{
static::$unguarded = false;
}
|
Enable the mass assignment restrictions.
@return void
|
reguard
|
php
|
illuminate/database
|
Eloquent/Concerns/GuardsAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/GuardsAttributes.php
|
MIT
|
public static function isUnguarded()
{
return static::$unguarded;
}
|
Determine if the current state is "unguarded".
@return bool
|
isUnguarded
|
php
|
illuminate/database
|
Eloquent/Concerns/GuardsAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/GuardsAttributes.php
|
MIT
|
public static function unguarded(callable $callback)
{
if (static::$unguarded) {
return $callback();
}
static::unguard();
try {
return $callback();
} finally {
static::reguard();
}
}
|
Run the given callable while being unguarded.
@template TReturn
@param callable(): TReturn $callback
@return TReturn
|
unguarded
|
php
|
illuminate/database
|
Eloquent/Concerns/GuardsAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/GuardsAttributes.php
|
MIT
|
public function isFillable($key)
{
if (static::$unguarded) {
return true;
}
// If the key is in the "fillable" array, we can of course assume that it's
// a fillable attribute. Otherwise, we will check the guarded array when
// we need to determine if the attribute is black-listed on the model.
if (in_array($key, $this->getFillable())) {
return true;
}
// If the attribute is explicitly listed in the "guarded" array then we can
// return false immediately. This means this attribute is definitely not
// fillable and there is no point in going any further in this method.
if ($this->isGuarded($key)) {
return false;
}
return empty($this->getFillable()) &&
! str_contains($key, '.') &&
! str_starts_with($key, '_');
}
|
Determine if the given attribute may be mass assigned.
@param string $key
@return bool
|
isFillable
|
php
|
illuminate/database
|
Eloquent/Concerns/GuardsAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/GuardsAttributes.php
|
MIT
|
public function isGuarded($key)
{
if (empty($this->getGuarded())) {
return false;
}
return $this->getGuarded() == ['*'] ||
! empty(preg_grep('/^'.preg_quote($key, '/').'$/i', $this->getGuarded())) ||
! $this->isGuardableColumn($key);
}
|
Determine if the given key is guarded.
@param string $key
@return bool
|
isGuarded
|
php
|
illuminate/database
|
Eloquent/Concerns/GuardsAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/GuardsAttributes.php
|
MIT
|
protected function isGuardableColumn($key)
{
if ($this->hasSetMutator($key) || $this->hasAttributeSetMutator($key)) {
return true;
}
if (! isset(static::$guardableColumns[get_class($this)])) {
$columns = $this->getConnection()
->getSchemaBuilder()
->getColumnListing($this->getTable());
if (empty($columns)) {
return true;
}
static::$guardableColumns[get_class($this)] = $columns;
}
return in_array($key, static::$guardableColumns[get_class($this)]);
}
|
Determine if the given column is a valid, guardable column.
@param string $key
@return bool
|
isGuardableColumn
|
php
|
illuminate/database
|
Eloquent/Concerns/GuardsAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/GuardsAttributes.php
|
MIT
|
public function totallyGuarded()
{
return count($this->getFillable()) === 0 && $this->getGuarded() == ['*'];
}
|
Determine if the model is totally guarded.
@return bool
|
totallyGuarded
|
php
|
illuminate/database
|
Eloquent/Concerns/GuardsAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/GuardsAttributes.php
|
MIT
|
protected function fillableFromArray(array $attributes)
{
if (count($this->getFillable()) > 0 && ! static::$unguarded) {
return array_intersect_key($attributes, array_flip($this->getFillable()));
}
return $attributes;
}
|
Get the fillable attributes of a given array.
@param array<string, mixed> $attributes
@return array<string, mixed>
|
fillableFromArray
|
php
|
illuminate/database
|
Eloquent/Concerns/GuardsAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/GuardsAttributes.php
|
MIT
|
public function attributesToArray()
{
// If an attribute is a date, we will cast it to a string after converting it
// to a DateTime / Carbon instance. This is so we will get some consistent
// formatting while accessing attributes vs. arraying / JSONing a model.
$attributes = $this->addDateAttributesToArray(
$attributes = $this->getArrayableAttributes()
);
$attributes = $this->addMutatedAttributesToArray(
$attributes, $mutatedAttributes = $this->getMutatedAttributes()
);
// Next we will handle any casts that have been setup for this model and cast
// the values to their appropriate type. If the attribute has a mutator we
// will not perform the cast on those attributes to avoid any confusion.
$attributes = $this->addCastAttributesToArray(
$attributes, $mutatedAttributes
);
// Here we will grab all of the appended, calculated attributes to this model
// as these attributes are not really in the attributes array, but are run
// when we need to array or JSON the model for convenience to the coder.
foreach ($this->getArrayableAppends() as $key) {
$attributes[$key] = $this->mutateAttributeForArray($key, null);
}
return $attributes;
}
|
Convert the model's attributes to an array.
@return array<string, mixed>
|
attributesToArray
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function addDateAttributesToArray(array $attributes)
{
foreach ($this->getDates() as $key) {
if (! isset($attributes[$key])) {
continue;
}
$attributes[$key] = $this->serializeDate(
$this->asDateTime($attributes[$key])
);
}
return $attributes;
}
|
Add the date attributes to the attributes array.
@param array<string, mixed> $attributes
@return array<string, mixed>
|
addDateAttributesToArray
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function addMutatedAttributesToArray(array $attributes, array $mutatedAttributes)
{
foreach ($mutatedAttributes as $key) {
// We want to spin through all the mutated attributes for this model and call
// the mutator for the attribute. We cache off every mutated attributes so
// we don't have to constantly check on attributes that actually change.
if (! array_key_exists($key, $attributes)) {
continue;
}
// Next, we will call the mutator for this attribute so that we can get these
// mutated attribute's actual values. After we finish mutating each of the
// attributes we will return this final array of the mutated attributes.
$attributes[$key] = $this->mutateAttributeForArray(
$key, $attributes[$key]
);
}
return $attributes;
}
|
Add the mutated attributes to the attributes array.
@param array<string, mixed> $attributes
@param array<string, mixed> $mutatedAttributes
@return array<string, mixed>
|
addMutatedAttributesToArray
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function addCastAttributesToArray(array $attributes, array $mutatedAttributes)
{
foreach ($this->getCasts() as $key => $value) {
if (! array_key_exists($key, $attributes) ||
in_array($key, $mutatedAttributes)) {
continue;
}
// Here we will cast the attribute. Then, if the cast is a date or datetime cast
// then we will serialize the date for the array. This will convert the dates
// to strings based on the date format specified for these Eloquent models.
$attributes[$key] = $this->castAttribute(
$key, $attributes[$key]
);
// If the attribute cast was a date or a datetime, we will serialize the date as
// a string. This allows the developers to customize how dates are serialized
// into an array without affecting how they are persisted into the storage.
if (isset($attributes[$key]) && in_array($value, ['date', 'datetime', 'immutable_date', 'immutable_datetime'])) {
$attributes[$key] = $this->serializeDate($attributes[$key]);
}
if (isset($attributes[$key]) && ($this->isCustomDateTimeCast($value) ||
$this->isImmutableCustomDateTimeCast($value))) {
$attributes[$key] = $attributes[$key]->format(explode(':', $value, 2)[1]);
}
if ($attributes[$key] instanceof DateTimeInterface &&
$this->isClassCastable($key)) {
$attributes[$key] = $this->serializeDate($attributes[$key]);
}
if (isset($attributes[$key]) && $this->isClassSerializable($key)) {
$attributes[$key] = $this->serializeClassCastableAttribute($key, $attributes[$key]);
}
if ($this->isEnumCastable($key) && (! ($attributes[$key] ?? null) instanceof Arrayable)) {
$attributes[$key] = isset($attributes[$key]) ? $this->getStorableEnumValue($this->getCasts()[$key], $attributes[$key]) : null;
}
if ($attributes[$key] instanceof Arrayable) {
$attributes[$key] = $attributes[$key]->toArray();
}
}
return $attributes;
}
|
Add the casted attributes to the attributes array.
@param array<string, mixed> $attributes
@param array<string, mixed> $mutatedAttributes
@return array<string, mixed>
|
addCastAttributesToArray
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getArrayableAttributes()
{
return $this->getArrayableItems($this->getAttributes());
}
|
Get an attribute array of all arrayable attributes.
@return array<string, mixed>
|
getArrayableAttributes
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getArrayableAppends()
{
if (! count($this->appends)) {
return [];
}
return $this->getArrayableItems(
array_combine($this->appends, $this->appends)
);
}
|
Get all of the appendable values that are arrayable.
@return array
|
getArrayableAppends
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function relationsToArray()
{
$attributes = [];
foreach ($this->getArrayableRelations() as $key => $value) {
// If the values implement the Arrayable interface we can just call this
// toArray method on the instances which will convert both models and
// collections to their proper array form and we'll set the values.
if ($value instanceof Arrayable) {
$relation = $value->toArray();
}
// If the value is null, we'll still go ahead and set it in this list of
// attributes, since null is used to represent empty relationships if
// it has a has one or belongs to type relationships on the models.
elseif (is_null($value)) {
$relation = $value;
}
// If the relationships snake-casing is enabled, we will snake case this
// key so that the relation attribute is snake cased in this returned
// array to the developers, making this consistent with attributes.
if (static::$snakeAttributes) {
$key = Str::snake($key);
}
// If the relation value has been set, we will set it on this attributes
// list for returning. If it was not arrayable or null, we'll not set
// the value on the array because it is some type of invalid value.
if (array_key_exists('relation', get_defined_vars())) { // check if $relation is in scope (could be null)
$attributes[$key] = $relation ?? null;
}
unset($relation);
}
return $attributes;
}
|
Get the model's relationships in array form.
@return array
|
relationsToArray
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getArrayableRelations()
{
return $this->getArrayableItems($this->relations);
}
|
Get an attribute array of all arrayable relations.
@return array
|
getArrayableRelations
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getArrayableItems(array $values)
{
if (count($this->getVisible()) > 0) {
$values = array_intersect_key($values, array_flip($this->getVisible()));
}
if (count($this->getHidden()) > 0) {
$values = array_diff_key($values, array_flip($this->getHidden()));
}
return $values;
}
|
Get an attribute array of all arrayable values.
@param array $values
@return array
|
getArrayableItems
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function hasAttribute($key)
{
if (! $key) {
return false;
}
return array_key_exists($key, $this->attributes) ||
array_key_exists($key, $this->casts) ||
$this->hasGetMutator($key) ||
$this->hasAttributeMutator($key) ||
$this->isClassCastable($key);
}
|
Determine whether an attribute exists on the model.
@param string $key
@return bool
|
hasAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function getAttribute($key)
{
if (! $key) {
return;
}
// If the attribute exists in the attribute array or has a "get" mutator we will
// get the attribute's value. Otherwise, we will proceed as if the developers
// are asking for a relationship's value. This covers both types of values.
if ($this->hasAttribute($key)) {
return $this->getAttributeValue($key);
}
// Here we will determine if the model base class itself contains this given key
// since we don't want to treat any of those methods as relationships because
// they are all intended as helper methods and none of these are relations.
if (method_exists(self::class, $key)) {
return $this->throwMissingAttributeExceptionIfApplicable($key);
}
return $this->isRelation($key) || $this->relationLoaded($key)
? $this->getRelationValue($key)
: $this->throwMissingAttributeExceptionIfApplicable($key);
}
|
Get an attribute from the model.
@param string $key
@return mixed
|
getAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function throwMissingAttributeExceptionIfApplicable($key)
{
if ($this->exists &&
! $this->wasRecentlyCreated &&
static::preventsAccessingMissingAttributes()) {
if (isset(static::$missingAttributeViolationCallback)) {
return call_user_func(static::$missingAttributeViolationCallback, $this, $key);
}
throw new MissingAttributeException($this, $key);
}
return null;
}
|
Either throw a missing attribute exception or return null depending on Eloquent's configuration.
@param string $key
@return null
@throws \Illuminate\Database\Eloquent\MissingAttributeException
|
throwMissingAttributeExceptionIfApplicable
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function getAttributeValue($key)
{
return $this->transformModelValue($key, $this->getAttributeFromArray($key));
}
|
Get a plain attribute (not a relationship).
@param string $key
@return mixed
|
getAttributeValue
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getAttributeFromArray($key)
{
return $this->getAttributes()[$key] ?? null;
}
|
Get an attribute from the $attributes array.
@param string $key
@return mixed
|
getAttributeFromArray
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function getRelationValue($key)
{
// If the key already exists in the relationships array, it just means the
// relationship has already been loaded, so we'll just return it out of
// here because there is no need to query within the relations twice.
if ($this->relationLoaded($key)) {
return $this->relations[$key];
}
if (! $this->isRelation($key)) {
return;
}
if ($this->attemptToAutoloadRelation($key)) {
return $this->relations[$key];
}
if ($this->preventsLazyLoading) {
$this->handleLazyLoadingViolation($key);
}
// If the "attribute" exists as a method on the model, we will just assume
// it is a relationship and will load and return results from the query
// and hydrate the relationship's value on the "relationships" array.
return $this->getRelationshipFromMethod($key);
}
|
Get a relationship.
@param string $key
@return mixed
|
getRelationValue
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function isRelation($key)
{
if ($this->hasAttributeMutator($key)) {
return false;
}
return method_exists($this, $key) ||
$this->relationResolver(static::class, $key);
}
|
Determine if the given key is a relationship method on the model.
@param string $key
@return bool
|
isRelation
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function handleLazyLoadingViolation($key)
{
if (isset(static::$lazyLoadingViolationCallback)) {
return call_user_func(static::$lazyLoadingViolationCallback, $this, $key);
}
if (! $this->exists || $this->wasRecentlyCreated) {
return;
}
throw new LazyLoadingViolationException($this, $key);
}
|
Handle a lazy loading violation.
@param string $key
@return mixed
|
handleLazyLoadingViolation
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getRelationshipFromMethod($method)
{
$relation = $this->$method();
if (! $relation instanceof Relation) {
if (is_null($relation)) {
throw new LogicException(sprintf(
'%s::%s must return a relationship instance, but "null" was returned. Was the "return" keyword used?', static::class, $method
));
}
throw new LogicException(sprintf(
'%s::%s must return a relationship instance.', static::class, $method
));
}
return tap($relation->getResults(), function ($results) use ($method) {
$this->setRelation($method, $results);
});
}
|
Get a relationship value from a method.
@param string $method
@return mixed
@throws \LogicException
|
getRelationshipFromMethod
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function hasGetMutator($key)
{
return method_exists($this, 'get'.Str::studly($key).'Attribute');
}
|
Determine if a get mutator exists for an attribute.
@param string $key
@return bool
|
hasGetMutator
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function hasAttributeMutator($key)
{
if (isset(static::$attributeMutatorCache[get_class($this)][$key])) {
return static::$attributeMutatorCache[get_class($this)][$key];
}
if (! method_exists($this, $method = Str::camel($key))) {
return static::$attributeMutatorCache[get_class($this)][$key] = false;
}
$returnType = (new ReflectionMethod($this, $method))->getReturnType();
return static::$attributeMutatorCache[get_class($this)][$key] =
$returnType instanceof ReflectionNamedType &&
$returnType->getName() === Attribute::class;
}
|
Determine if a "Attribute" return type marked mutator exists for an attribute.
@param string $key
@return bool
|
hasAttributeMutator
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function hasAttributeGetMutator($key)
{
if (isset(static::$getAttributeMutatorCache[get_class($this)][$key])) {
return static::$getAttributeMutatorCache[get_class($this)][$key];
}
if (! $this->hasAttributeMutator($key)) {
return static::$getAttributeMutatorCache[get_class($this)][$key] = false;
}
return static::$getAttributeMutatorCache[get_class($this)][$key] = is_callable($this->{Str::camel($key)}()->get);
}
|
Determine if a "Attribute" return type marked get mutator exists for an attribute.
@param string $key
@return bool
|
hasAttributeGetMutator
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function hasAnyGetMutator($key)
{
return $this->hasGetMutator($key) || $this->hasAttributeGetMutator($key);
}
|
Determine if any get mutator exists for an attribute.
@param string $key
@return bool
|
hasAnyGetMutator
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function mutateAttribute($key, $value)
{
return $this->{'get'.Str::studly($key).'Attribute'}($value);
}
|
Get the value of an attribute using its mutator.
@param string $key
@param mixed $value
@return mixed
|
mutateAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function mutateAttributeMarkedAttribute($key, $value)
{
if (array_key_exists($key, $this->attributeCastCache)) {
return $this->attributeCastCache[$key];
}
$attribute = $this->{Str::camel($key)}();
$value = call_user_func($attribute->get ?: function ($value) {
return $value;
}, $value, $this->attributes);
if ($attribute->withCaching || (is_object($value) && $attribute->withObjectCaching)) {
$this->attributeCastCache[$key] = $value;
} else {
unset($this->attributeCastCache[$key]);
}
return $value;
}
|
Get the value of an "Attribute" return type marked attribute using its mutator.
@param string $key
@param mixed $value
@return mixed
|
mutateAttributeMarkedAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function mutateAttributeForArray($key, $value)
{
if ($this->isClassCastable($key)) {
$value = $this->getClassCastableAttributeValue($key, $value);
} elseif (isset(static::$getAttributeMutatorCache[get_class($this)][$key]) &&
static::$getAttributeMutatorCache[get_class($this)][$key] === true) {
$value = $this->mutateAttributeMarkedAttribute($key, $value);
$value = $value instanceof DateTimeInterface
? $this->serializeDate($value)
: $value;
} else {
$value = $this->mutateAttribute($key, $value);
}
return $value instanceof Arrayable ? $value->toArray() : $value;
}
|
Get the value of an attribute using its mutator for array conversion.
@param string $key
@param mixed $value
@return mixed
|
mutateAttributeForArray
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function mergeCasts($casts)
{
$casts = $this->ensureCastsAreStringValues($casts);
$this->casts = array_merge($this->casts, $casts);
return $this;
}
|
Merge new casts with existing casts on the model.
@param array $casts
@return $this
|
mergeCasts
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function ensureCastsAreStringValues($casts)
{
foreach ($casts as $attribute => $cast) {
$casts[$attribute] = match (true) {
is_array($cast) => value(function () use ($cast) {
if (count($cast) === 1) {
return $cast[0];
}
[$cast, $arguments] = [array_shift($cast), $cast];
return $cast.':'.implode(',', $arguments);
}),
default => $cast,
};
}
return $casts;
}
|
Ensure that the given casts are strings.
@param array $casts
@return array
|
ensureCastsAreStringValues
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function castAttribute($key, $value)
{
$castType = $this->getCastType($key);
if (is_null($value) && in_array($castType, static::$primitiveCastTypes)) {
return $value;
}
// If the key is one of the encrypted castable types, we'll first decrypt
// the value and update the cast type so we may leverage the following
// logic for casting this value to any additionally specified types.
if ($this->isEncryptedCastable($key)) {
$value = $this->fromEncryptedString($value);
$castType = Str::after($castType, 'encrypted:');
}
switch ($castType) {
case 'int':
case 'integer':
return (int) $value;
case 'real':
case 'float':
case 'double':
return $this->fromFloat($value);
case 'decimal':
return $this->asDecimal($value, explode(':', $this->getCasts()[$key], 2)[1]);
case 'string':
return (string) $value;
case 'bool':
case 'boolean':
return (bool) $value;
case 'object':
return $this->fromJson($value, true);
case 'array':
case 'json':
case 'json:unicode':
return $this->fromJson($value);
case 'collection':
return new BaseCollection($this->fromJson($value));
case 'date':
return $this->asDate($value);
case 'datetime':
case 'custom_datetime':
return $this->asDateTime($value);
case 'immutable_date':
return $this->asDate($value)->toImmutable();
case 'immutable_custom_datetime':
case 'immutable_datetime':
return $this->asDateTime($value)->toImmutable();
case 'timestamp':
return $this->asTimestamp($value);
}
if ($this->isEnumCastable($key)) {
return $this->getEnumCastableAttributeValue($key, $value);
}
if ($this->isClassCastable($key)) {
return $this->getClassCastableAttributeValue($key, $value);
}
return $value;
}
|
Cast an attribute to a native PHP type.
@param string $key
@param mixed $value
@return mixed
|
castAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getClassCastableAttributeValue($key, $value)
{
$caster = $this->resolveCasterClass($key);
$objectCachingDisabled = $caster->withoutObjectCaching ?? false;
if (isset($this->classCastCache[$key]) && ! $objectCachingDisabled) {
return $this->classCastCache[$key];
} else {
$value = $caster instanceof CastsInboundAttributes
? $value
: $caster->get($this, $key, $value, $this->attributes);
if ($caster instanceof CastsInboundAttributes ||
! is_object($value) ||
$objectCachingDisabled) {
unset($this->classCastCache[$key]);
} else {
$this->classCastCache[$key] = $value;
}
return $value;
}
}
|
Cast the given attribute using a custom cast class.
@param string $key
@param mixed $value
@return mixed
|
getClassCastableAttributeValue
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getEnumCastableAttributeValue($key, $value)
{
if (is_null($value)) {
return;
}
$castType = $this->getCasts()[$key];
if ($value instanceof $castType) {
return $value;
}
return $this->getEnumCaseFromValue($castType, $value);
}
|
Cast the given attribute to an enum.
@param string $key
@param mixed $value
@return mixed
|
getEnumCastableAttributeValue
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getCastType($key)
{
$castType = $this->getCasts()[$key];
if (isset(static::$castTypeCache[$castType])) {
return static::$castTypeCache[$castType];
}
if ($this->isCustomDateTimeCast($castType)) {
$convertedCastType = 'custom_datetime';
} elseif ($this->isImmutableCustomDateTimeCast($castType)) {
$convertedCastType = 'immutable_custom_datetime';
} elseif ($this->isDecimalCast($castType)) {
$convertedCastType = 'decimal';
} elseif (class_exists($castType)) {
$convertedCastType = $castType;
} else {
$convertedCastType = trim(strtolower($castType));
}
return static::$castTypeCache[$castType] = $convertedCastType;
}
|
Get the type of cast for a model attribute.
@param string $key
@return string
|
getCastType
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function deviateClassCastableAttribute($method, $key, $value)
{
return $this->resolveCasterClass($key)->{$method}(
$this, $key, $value, $this->attributes
);
}
|
Increment or decrement the given attribute using the custom cast class.
@param string $method
@param string $key
@param mixed $value
@return mixed
|
deviateClassCastableAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function serializeClassCastableAttribute($key, $value)
{
return $this->resolveCasterClass($key)->serialize(
$this, $key, $value, $this->attributes
);
}
|
Serialize the given attribute using the custom cast class.
@param string $key
@param mixed $value
@return mixed
|
serializeClassCastableAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function compareClassCastableAttribute($key, $original, $value)
{
return $this->resolveCasterClass($key)->compare(
$this, $key, $original, $value
);
}
|
Compare two values for the given attribute using the custom cast class.
@param string $key
@param mixed $original
@param mixed $value
@return bool
|
compareClassCastableAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isCustomDateTimeCast($cast)
{
return str_starts_with($cast, 'date:') ||
str_starts_with($cast, 'datetime:');
}
|
Determine if the cast type is a custom date time cast.
@param string $cast
@return bool
|
isCustomDateTimeCast
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isImmutableCustomDateTimeCast($cast)
{
return str_starts_with($cast, 'immutable_date:') ||
str_starts_with($cast, 'immutable_datetime:');
}
|
Determine if the cast type is an immutable custom date time cast.
@param string $cast
@return bool
|
isImmutableCustomDateTimeCast
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isDecimalCast($cast)
{
return str_starts_with($cast, 'decimal:');
}
|
Determine if the cast type is a decimal cast.
@param string $cast
@return bool
|
isDecimalCast
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function setAttribute($key, $value)
{
// First we will check for the presence of a mutator for the set operation
// which simply lets the developers tweak the attribute as it is set on
// this model, such as "json_encoding" a listing of data for storage.
if ($this->hasSetMutator($key)) {
return $this->setMutatedAttributeValue($key, $value);
} elseif ($this->hasAttributeSetMutator($key)) {
return $this->setAttributeMarkedMutatedAttributeValue($key, $value);
}
// If an attribute is listed as a "date", we'll convert it from a DateTime
// instance into a form proper for storage on the database tables using
// the connection grammar's date format. We will auto set the values.
elseif (! is_null($value) && $this->isDateAttribute($key)) {
$value = $this->fromDateTime($value);
}
if ($this->isEnumCastable($key)) {
$this->setEnumCastableAttribute($key, $value);
return $this;
}
if ($this->isClassCastable($key)) {
$this->setClassCastableAttribute($key, $value);
return $this;
}
if (! is_null($value) && $this->isJsonCastable($key)) {
$value = $this->castAttributeAsJson($key, $value);
}
// If this attribute contains a JSON ->, we'll set the proper value in the
// attribute's underlying array. This takes care of properly nesting an
// attribute in the array's value in the case of deeply nested items.
if (str_contains($key, '->')) {
return $this->fillJsonAttribute($key, $value);
}
if (! is_null($value) && $this->isEncryptedCastable($key)) {
$value = $this->castAttributeAsEncryptedString($key, $value);
}
if (! is_null($value) && $this->hasCast($key, 'hashed')) {
$value = $this->castAttributeAsHashedString($key, $value);
}
$this->attributes[$key] = $value;
return $this;
}
|
Set a given attribute on the model.
@param string $key
@param mixed $value
@return mixed
|
setAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function hasSetMutator($key)
{
return method_exists($this, 'set'.Str::studly($key).'Attribute');
}
|
Determine if a set mutator exists for an attribute.
@param string $key
@return bool
|
hasSetMutator
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function hasAttributeSetMutator($key)
{
$class = get_class($this);
if (isset(static::$setAttributeMutatorCache[$class][$key])) {
return static::$setAttributeMutatorCache[$class][$key];
}
if (! method_exists($this, $method = Str::camel($key))) {
return static::$setAttributeMutatorCache[$class][$key] = false;
}
$returnType = (new ReflectionMethod($this, $method))->getReturnType();
return static::$setAttributeMutatorCache[$class][$key] =
$returnType instanceof ReflectionNamedType &&
$returnType->getName() === Attribute::class &&
is_callable($this->{$method}()->set);
}
|
Determine if an "Attribute" return type marked set mutator exists for an attribute.
@param string $key
@return bool
|
hasAttributeSetMutator
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function setMutatedAttributeValue($key, $value)
{
return $this->{'set'.Str::studly($key).'Attribute'}($value);
}
|
Set the value of an attribute using its mutator.
@param string $key
@param mixed $value
@return mixed
|
setMutatedAttributeValue
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function setAttributeMarkedMutatedAttributeValue($key, $value)
{
$attribute = $this->{Str::camel($key)}();
$callback = $attribute->set ?: function ($value) use ($key) {
$this->attributes[$key] = $value;
};
$this->attributes = array_merge(
$this->attributes,
$this->normalizeCastClassResponse(
$key, $callback($value, $this->attributes)
)
);
if ($attribute->withCaching || (is_object($value) && $attribute->withObjectCaching)) {
$this->attributeCastCache[$key] = $value;
} else {
unset($this->attributeCastCache[$key]);
}
return $this;
}
|
Set the value of a "Attribute" return type marked attribute using its mutator.
@param string $key
@param mixed $value
@return mixed
|
setAttributeMarkedMutatedAttributeValue
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isDateAttribute($key)
{
return in_array($key, $this->getDates(), true) ||
$this->isDateCastable($key);
}
|
Determine if the given attribute is a date or date castable.
@param string $key
@return bool
|
isDateAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function fillJsonAttribute($key, $value)
{
[$key, $path] = explode('->', $key, 2);
$value = $this->asJson($this->getArrayAttributeWithValue(
$path, $key, $value
), $this->getJsonCastFlags($key));
$this->attributes[$key] = $this->isEncryptedCastable($key)
? $this->castAttributeAsEncryptedString($key, $value)
: $value;
if ($this->isClassCastable($key)) {
unset($this->classCastCache[$key]);
}
return $this;
}
|
Set a given JSON attribute on the model.
@param string $key
@param mixed $value
@return $this
|
fillJsonAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function setClassCastableAttribute($key, $value)
{
$caster = $this->resolveCasterClass($key);
$this->attributes = array_replace(
$this->attributes,
$this->normalizeCastClassResponse($key, $caster->set(
$this, $key, $value, $this->attributes
))
);
if ($caster instanceof CastsInboundAttributes ||
! is_object($value) ||
($caster->withoutObjectCaching ?? false)) {
unset($this->classCastCache[$key]);
} else {
$this->classCastCache[$key] = $value;
}
}
|
Set the value of a class castable attribute.
@param string $key
@param mixed $value
@return void
|
setClassCastableAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function setEnumCastableAttribute($key, $value)
{
$enumClass = $this->getCasts()[$key];
if (! isset($value)) {
$this->attributes[$key] = null;
} elseif (is_object($value)) {
$this->attributes[$key] = $this->getStorableEnumValue($enumClass, $value);
} else {
$this->attributes[$key] = $this->getStorableEnumValue(
$enumClass, $this->getEnumCaseFromValue($enumClass, $value)
);
}
}
|
Set the value of an enum castable attribute.
@param string $key
@param \UnitEnum|string|int|null $value
@return void
|
setEnumCastableAttribute
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getEnumCaseFromValue($enumClass, $value)
{
return is_subclass_of($enumClass, BackedEnum::class)
? $enumClass::from($value)
: constant($enumClass.'::'.$value);
}
|
Get an enum case instance from a given class and value.
@param string $enumClass
@param string|int $value
@return \UnitEnum|\BackedEnum
|
getEnumCaseFromValue
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getStorableEnumValue($expectedEnum, $value)
{
if (! $value instanceof $expectedEnum) {
throw new ValueError(sprintf('Value [%s] is not of the expected enum type [%s].', var_export($value, true), $expectedEnum));
}
return enum_value($value);
}
|
Get the storable value from the given enum.
@param string $expectedEnum
@param \UnitEnum|\BackedEnum $value
@return string|int
|
getStorableEnumValue
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getArrayAttributeWithValue($path, $key, $value)
{
return tap($this->getArrayAttributeByKey($key), function (&$array) use ($path, $value) {
Arr::set($array, str_replace('->', '.', $path), $value);
});
}
|
Get an array attribute with the given key and value set.
@param string $path
@param string $key
@param mixed $value
@return $this
|
getArrayAttributeWithValue
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getArrayAttributeByKey($key)
{
if (! isset($this->attributes[$key])) {
return [];
}
return $this->fromJson(
$this->isEncryptedCastable($key)
? $this->fromEncryptedString($this->attributes[$key])
: $this->attributes[$key]
);
}
|
Get an array attribute or return an empty array if it is not set.
@param string $key
@return array
|
getArrayAttributeByKey
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function castAttributeAsJson($key, $value)
{
$value = $this->asJson($value, $this->getJsonCastFlags($key));
if ($value === false) {
throw JsonEncodingException::forAttribute(
$this, $key, json_last_error_msg()
);
}
return $value;
}
|
Cast the given attribute to JSON.
@param string $key
@param mixed $value
@return string
|
castAttributeAsJson
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getJsonCastFlags($key)
{
$flags = 0;
if ($this->hasCast($key, ['json:unicode'])) {
$flags |= JSON_UNESCAPED_UNICODE;
}
return $flags;
}
|
Get the JSON casting flags for the given attribute.
@param string $key
@return int
|
getJsonCastFlags
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function asJson($value, $flags = 0)
{
return Json::encode($value, $flags);
}
|
Encode the given value as JSON.
@param mixed $value
@param int $flags
@return string
|
asJson
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function fromJson($value, $asObject = false)
{
if ($value === null || $value === '') {
return null;
}
return Json::decode($value, ! $asObject);
}
|
Decode the given JSON back into an array or object.
@param string|null $value
@param bool $asObject
@return mixed
|
fromJson
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function fromEncryptedString($value)
{
return static::currentEncrypter()->decrypt($value, false);
}
|
Decrypt the given encrypted string.
@param string $value
@return mixed
|
fromEncryptedString
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function castAttributeAsEncryptedString($key, #[\SensitiveParameter] $value)
{
return static::currentEncrypter()->encrypt($value, false);
}
|
Cast the given attribute to an encrypted string.
@param string $key
@param mixed $value
@return string
|
castAttributeAsEncryptedString
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public static function encryptUsing($encrypter)
{
static::$encrypter = $encrypter;
}
|
Set the encrypter instance that will be used to encrypt attributes.
@param \Illuminate\Contracts\Encryption\Encrypter|null $encrypter
@return void
|
encryptUsing
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected static function currentEncrypter()
{
return static::$encrypter ?? Crypt::getFacadeRoot();
}
|
Get the current encrypter being used by the model.
@return \Illuminate\Contracts\Encryption\Encrypter
|
currentEncrypter
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function castAttributeAsHashedString($key, #[\SensitiveParameter] $value)
{
if ($value === null) {
return null;
}
if (! Hash::isHashed($value)) {
return Hash::make($value);
}
/** @phpstan-ignore staticMethod.notFound */
if (! Hash::verifyConfiguration($value)) {
throw new RuntimeException("Could not verify the hashed value's configuration.");
}
return $value;
}
|
Cast the given attribute to a hashed string.
@param string $key
@param mixed $value
@return string
|
castAttributeAsHashedString
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function fromFloat($value)
{
return match ((string) $value) {
'Infinity' => INF,
'-Infinity' => -INF,
'NaN' => NAN,
default => (float) $value,
};
}
|
Decode the given float.
@param mixed $value
@return mixed
|
fromFloat
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function asDecimal($value, $decimals)
{
try {
return (string) BigDecimal::of($value)->toScale($decimals, RoundingMode::HALF_UP);
} catch (BrickMathException $e) {
throw new MathException('Unable to cast value to a decimal.', previous: $e);
}
}
|
Return a decimal as string.
@param float|string $value
@param int $decimals
@return string
|
asDecimal
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function asDate($value)
{
return $this->asDateTime($value)->startOfDay();
}
|
Return a timestamp as DateTime object with time set to 00:00:00.
@param mixed $value
@return \Illuminate\Support\Carbon
|
asDate
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function asDateTime($value)
{
// If this value is already a Carbon instance, we shall just return it as is.
// This prevents us having to re-instantiate a Carbon instance when we know
// it already is one, which wouldn't be fulfilled by the DateTime check.
if ($value instanceof CarbonInterface) {
return Date::instance($value);
}
// If the value is already a DateTime instance, we will just skip the rest of
// these checks since they will be a waste of time, and hinder performance
// when checking the field. We will just return the DateTime right away.
if ($value instanceof DateTimeInterface) {
return Date::parse(
$value->format('Y-m-d H:i:s.u'), $value->getTimezone()
);
}
// If this value is an integer, we will assume it is a UNIX timestamp's value
// and format a Carbon object from this timestamp. This allows flexibility
// when defining your date fields as they might be UNIX timestamps here.
if (is_numeric($value)) {
return Date::createFromTimestamp($value, date_default_timezone_get());
}
// If the value is in simply year, month, day format, we will instantiate the
// Carbon instances from that format. Again, this provides for simple date
// fields on the database, while still supporting Carbonized conversion.
if ($this->isStandardDateFormat($value)) {
return Date::instance(Carbon::createFromFormat('Y-m-d', $value)->startOfDay());
}
$format = $this->getDateFormat();
// Finally, we will just assume this date is in the format used by default on
// the database connection and use that format to create the Carbon object
// that is returned back out to the developers after we convert it here.
try {
$date = Date::createFromFormat($format, $value);
} catch (InvalidArgumentException) {
$date = false;
}
return $date ?: Date::parse($value);
}
|
Return a timestamp as DateTime object.
@param mixed $value
@return \Illuminate\Support\Carbon
|
asDateTime
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isStandardDateFormat($value)
{
return preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value);
}
|
Determine if the given value is a standard date format.
@param string $value
@return bool
|
isStandardDateFormat
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function fromDateTime($value)
{
return empty($value) ? $value : $this->asDateTime($value)->format(
$this->getDateFormat()
);
}
|
Convert a DateTime to a storable string.
@param mixed $value
@return string|null
|
fromDateTime
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function asTimestamp($value)
{
return $this->asDateTime($value)->getTimestamp();
}
|
Return a timestamp as unix timestamp.
@param mixed $value
@return int
|
asTimestamp
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function serializeDate(DateTimeInterface $date)
{
return $date instanceof DateTimeImmutable ?
CarbonImmutable::instance($date)->toJSON() :
Carbon::instance($date)->toJSON();
}
|
Prepare a date for array / JSON serialization.
@param \DateTimeInterface $date
@return string
|
serializeDate
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function getDates()
{
return $this->usesTimestamps() ? [
$this->getCreatedAtColumn(),
$this->getUpdatedAtColumn(),
] : [];
}
|
Get the attributes that should be converted to dates.
@return array
|
getDates
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function getDateFormat()
{
return $this->dateFormat ?: $this->getConnection()->getQueryGrammar()->getDateFormat();
}
|
Get the format for database stored dates.
@return string
|
getDateFormat
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function setDateFormat($format)
{
$this->dateFormat = $format;
return $this;
}
|
Set the date format used by the model.
@param string $format
@return $this
|
setDateFormat
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function hasCast($key, $types = null)
{
if (array_key_exists($key, $this->getCasts())) {
return $types ? in_array($this->getCastType($key), (array) $types, true) : true;
}
return false;
}
|
Determine whether an attribute should be cast to a native type.
@param string $key
@param array|string|null $types
@return bool
|
hasCast
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function getCasts()
{
if ($this->getIncrementing()) {
return array_merge([$this->getKeyName() => $this->getKeyType()], $this->casts);
}
return $this->casts;
}
|
Get the attributes that should be cast.
@return array
|
getCasts
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function casts()
{
return [];
}
|
Get the attributes that should be cast.
@return array<string, string>
|
casts
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isDateCastable($key)
{
return $this->hasCast($key, ['date', 'datetime', 'immutable_date', 'immutable_datetime']);
}
|
Determine whether a value is Date / DateTime castable for inbound manipulation.
@param string $key
@return bool
|
isDateCastable
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isDateCastableWithCustomFormat($key)
{
return $this->hasCast($key, ['custom_datetime', 'immutable_custom_datetime']);
}
|
Determine whether a value is Date / DateTime custom-castable for inbound manipulation.
@param string $key
@return bool
|
isDateCastableWithCustomFormat
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isJsonCastable($key)
{
return $this->hasCast($key, ['array', 'json', 'json:unicode', 'object', 'collection', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object']);
}
|
Determine whether a value is JSON castable for inbound manipulation.
@param string $key
@return bool
|
isJsonCastable
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isEncryptedCastable($key)
{
return $this->hasCast($key, ['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object']);
}
|
Determine whether a value is an encrypted castable for inbound manipulation.
@param string $key
@return bool
|
isEncryptedCastable
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isClassCastable($key)
{
$casts = $this->getCasts();
if (! array_key_exists($key, $casts)) {
return false;
}
$castType = $this->parseCasterClass($casts[$key]);
if (in_array($castType, static::$primitiveCastTypes)) {
return false;
}
if (class_exists($castType)) {
return true;
}
throw new InvalidCastException($this->getModel(), $key, $castType);
}
|
Determine if the given key is cast using a custom class.
@param string $key
@return bool
@throws \Illuminate\Database\Eloquent\InvalidCastException
|
isClassCastable
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isEnumCastable($key)
{
$casts = $this->getCasts();
if (! array_key_exists($key, $casts)) {
return false;
}
$castType = $casts[$key];
if (in_array($castType, static::$primitiveCastTypes)) {
return false;
}
return enum_exists($castType);
}
|
Determine if the given key is cast using an enum.
@param string $key
@return bool
|
isEnumCastable
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isClassDeviable($key)
{
if (! $this->isClassCastable($key)) {
return false;
}
$castType = $this->resolveCasterClass($key);
return method_exists($castType::class, 'increment') && method_exists($castType::class, 'decrement');
}
|
Determine if the key is deviable using a custom class.
@param string $key
@return bool
@throws \Illuminate\Database\Eloquent\InvalidCastException
|
isClassDeviable
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isClassSerializable($key)
{
return ! $this->isEnumCastable($key) &&
$this->isClassCastable($key) &&
method_exists($this->resolveCasterClass($key), 'serialize');
}
|
Determine if the key is serializable using a custom class.
@param string $key
@return bool
@throws \Illuminate\Database\Eloquent\InvalidCastException
|
isClassSerializable
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function isClassComparable($key)
{
return ! $this->isEnumCastable($key) &&
$this->isClassCastable($key) &&
method_exists($this->resolveCasterClass($key), 'compare');
}
|
Determine if the key is comparable using a custom class.
@param string $key
@return bool
|
isClassComparable
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function resolveCasterClass($key)
{
$castType = $this->getCasts()[$key];
$arguments = [];
if (is_string($castType) && str_contains($castType, ':')) {
$segments = explode(':', $castType, 2);
$castType = $segments[0];
$arguments = explode(',', $segments[1]);
}
if (is_subclass_of($castType, Castable::class)) {
$castType = $castType::castUsing($arguments);
}
if (is_object($castType)) {
return $castType;
}
return new $castType(...$arguments);
}
|
Resolve the custom caster class for a given key.
@param string $key
@return mixed
|
resolveCasterClass
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function parseCasterClass($class)
{
return ! str_contains($class, ':')
? $class
: explode(':', $class, 2)[0];
}
|
Parse the given caster class, removing any arguments.
@param string $class
@return string
|
parseCasterClass
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function mergeAttributesFromCachedCasts()
{
$this->mergeAttributesFromClassCasts();
$this->mergeAttributesFromAttributeCasts();
}
|
Merge the cast class and attribute cast attributes back into the model.
@return void
|
mergeAttributesFromCachedCasts
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function mergeAttributesFromClassCasts()
{
foreach ($this->classCastCache as $key => $value) {
$caster = $this->resolveCasterClass($key);
$this->attributes = array_merge(
$this->attributes,
$caster instanceof CastsInboundAttributes
? [$key => $value]
: $this->normalizeCastClassResponse($key, $caster->set($this, $key, $value, $this->attributes))
);
}
}
|
Merge the cast class attributes back into the model.
@return void
|
mergeAttributesFromClassCasts
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function mergeAttributesFromAttributeCasts()
{
foreach ($this->attributeCastCache as $key => $value) {
$attribute = $this->{Str::camel($key)}();
if ($attribute->get && ! $attribute->set) {
continue;
}
$callback = $attribute->set ?: function ($value) use ($key) {
$this->attributes[$key] = $value;
};
$this->attributes = array_merge(
$this->attributes,
$this->normalizeCastClassResponse(
$key, $callback($value, $this->attributes)
)
);
}
}
|
Merge the cast class attributes back into the model.
@return void
|
mergeAttributesFromAttributeCasts
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function normalizeCastClassResponse($key, $value)
{
return is_array($value) ? $value : [$key => $value];
}
|
Normalize the response from a custom class caster.
@param string $key
@param mixed $value
@return array
|
normalizeCastClassResponse
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function getAttributes()
{
$this->mergeAttributesFromCachedCasts();
return $this->attributes;
}
|
Get all of the current attributes on the model.
@return array<string, mixed>
|
getAttributes
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
protected function getAttributesForInsert()
{
return $this->getAttributes();
}
|
Get all of the current attributes on the model for an insert operation.
@return array
|
getAttributesForInsert
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function setRawAttributes(array $attributes, $sync = false)
{
$this->attributes = $attributes;
if ($sync) {
$this->syncOriginal();
}
$this->classCastCache = [];
$this->attributeCastCache = [];
return $this;
}
|
Set the array of model attributes. No checking is done.
@param array $attributes
@param bool $sync
@return $this
|
setRawAttributes
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
public function getOriginal($key = null, $default = null)
{
return (new static)->setRawAttributes(
$this->original, $sync = true
)->getOriginalWithoutRewindingModel($key, $default);
}
|
Get the model's original attribute values.
@param string|null $key
@param mixed $default
@return ($key is null ? array<string, mixed> : mixed)
|
getOriginal
|
php
|
illuminate/database
|
Eloquent/Concerns/HasAttributes.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php
|
MIT
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.