dep: package update
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Eloquent\Casts;
|
||||
|
||||
use Illuminate\Contracts\Database\Eloquent\Castable;
|
||||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class AsStringable implements Castable
|
||||
{
|
||||
/**
|
||||
* Get the caster class to use when casting from / to this cast target.
|
||||
*
|
||||
* @param array $arguments
|
||||
* @return object|string
|
||||
*/
|
||||
public static function castUsing(array $arguments)
|
||||
{
|
||||
return new class implements CastsAttributes
|
||||
{
|
||||
public function get($model, $key, $value, $attributes)
|
||||
{
|
||||
return isset($value) ? Str::of($value) : null;
|
||||
}
|
||||
|
||||
public function set($model, $key, $value, $attributes)
|
||||
{
|
||||
return isset($value) ? (string) $value : null;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@ use DateTimeInterface;
|
||||
use Illuminate\Contracts\Database\Eloquent\Castable;
|
||||
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
|
||||
use Illuminate\Database\Eloquent\Casts\AsCollection;
|
||||
use Illuminate\Database\Eloquent\InvalidCastException;
|
||||
use Illuminate\Database\Eloquent\JsonEncodingException;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
@@ -262,6 +264,10 @@ trait HasAttributes
|
||||
$attributes[$key] = $this->serializeClassCastableAttribute($key, $attributes[$key]);
|
||||
}
|
||||
|
||||
if ($this->isEnumCastable($key)) {
|
||||
$attributes[$key] = isset($attributes[$key]) ? $attributes[$key]->value : null;
|
||||
}
|
||||
|
||||
if ($attributes[$key] instanceof Arrayable) {
|
||||
$attributes[$key] = $attributes[$key]->toArray();
|
||||
}
|
||||
@@ -620,6 +626,10 @@ trait HasAttributes
|
||||
return $this->asTimestamp($value);
|
||||
}
|
||||
|
||||
if ($this->isEnumCastable($key)) {
|
||||
return $this->getEnumCastableAttributeValue($key, $value);
|
||||
}
|
||||
|
||||
if ($this->isClassCastable($key)) {
|
||||
return $this->getClassCastableAttributeValue($key, $value);
|
||||
}
|
||||
@@ -655,6 +665,24 @@ trait HasAttributes
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast the given attribute to an enum.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getEnumCastableAttributeValue($key, $value)
|
||||
{
|
||||
if (is_null($value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$castType = $this->getCasts()[$key];
|
||||
|
||||
return $castType::from($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type of cast for a model attribute.
|
||||
*
|
||||
@@ -765,6 +793,12 @@ trait HasAttributes
|
||||
$value = $this->fromDateTime($value);
|
||||
}
|
||||
|
||||
if ($this->isEnumCastable($key)) {
|
||||
$this->setEnumCastableAttribute($key, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if ($this->isClassCastable($key)) {
|
||||
$this->setClassCastableAttribute($key, $value);
|
||||
|
||||
@@ -883,6 +917,18 @@ trait HasAttributes
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of an enum castable attribute.
|
||||
*
|
||||
* @param string $key
|
||||
* @param \BackedEnum $value
|
||||
* @return void
|
||||
*/
|
||||
protected function setEnumCastableAttribute($key, $value)
|
||||
{
|
||||
$this->attributes[$key] = isset($value) ? $value->value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array attribute with the given key and value set.
|
||||
*
|
||||
@@ -1217,7 +1263,18 @@ trait HasAttributes
|
||||
*/
|
||||
protected function isDateCastable($key)
|
||||
{
|
||||
return $this->hasCast($key, ['date', 'datetime', 'custom_datetime', 'immutable_date', 'immutable_datetime', 'immutable_custom_datetime']);
|
||||
return $this->hasCast($key, ['date', 'datetime', 'immutable_date', 'immutable_datetime']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a value is Date / DateTime custom-castable for inbound manipulation.
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
protected function isDateCastableWithCustomFormat($key)
|
||||
{
|
||||
return $this->hasCast($key, ['custom_datetime', 'immutable_custom_datetime']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1269,6 +1326,29 @@ trait HasAttributes
|
||||
throw new InvalidCastException($this->getModel(), $key, $castType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given key is cast using an enum.
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
protected function isEnumCastable($key)
|
||||
{
|
||||
if (! array_key_exists($key, $this->getCasts())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$castType = $this->getCasts()[$key];
|
||||
|
||||
if (in_array($castType, static::$primitiveCastTypes)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (function_exists('enum_exists') && enum_exists($castType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the key is deviable using a custom class.
|
||||
*
|
||||
@@ -1294,8 +1374,9 @@ trait HasAttributes
|
||||
*/
|
||||
protected function isClassSerializable($key)
|
||||
{
|
||||
return $this->isClassCastable($key) &&
|
||||
method_exists($this->parseCasterClass($this->getCasts()[$key]), 'serialize');
|
||||
return ! $this->isEnumCastable($key) &&
|
||||
$this->isClassCastable($key) &&
|
||||
method_exists($this->resolveCasterClass($key), 'serialize');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1643,7 +1724,7 @@ trait HasAttributes
|
||||
return true;
|
||||
} elseif (is_null($attribute)) {
|
||||
return false;
|
||||
} elseif ($this->isDateAttribute($key) || $this->isDateCastable($key)) {
|
||||
} elseif ($this->isDateAttribute($key) || $this->isDateCastableWithCustomFormat($key)) {
|
||||
return $this->fromDateTime($attribute) ===
|
||||
$this->fromDateTime($original);
|
||||
} elseif ($this->hasCast($key, ['object', 'collection'])) {
|
||||
@@ -1658,6 +1739,8 @@ trait HasAttributes
|
||||
} elseif ($this->hasCast($key, static::$primitiveCastTypes)) {
|
||||
return $this->castAttribute($key, $attribute) ===
|
||||
$this->castAttribute($key, $original);
|
||||
} elseif ($this->isClassCastable($key) && in_array($this->getCasts()[$key], [AsArrayObject::class, AsCollection::class])) {
|
||||
return $this->fromJson($attribute) === $this->fromJson($original);
|
||||
}
|
||||
|
||||
return is_numeric($attribute) && is_numeric($original)
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
|
||||
namespace Illuminate\Database\Eloquent\Concerns;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Closure;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\RelationNotFoundException;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||
@@ -455,6 +458,56 @@ trait QueriesRelationships
|
||||
return $this->whereMorphedTo($relation, $model, 'or');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a "belongs to" relationship where clause to the query.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $related
|
||||
* @param string $relationship
|
||||
* @param string $boolean
|
||||
* @return $this
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function whereBelongsTo($related, $relationshipName = null, $boolean = 'and')
|
||||
{
|
||||
if ($relationshipName === null) {
|
||||
$relationshipName = Str::camel(class_basename($related));
|
||||
}
|
||||
|
||||
try {
|
||||
$relationship = $this->model->{$relationshipName}();
|
||||
} catch (BadMethodCallException $exception) {
|
||||
throw RelationNotFoundException::make($this->model, $relationshipName);
|
||||
}
|
||||
|
||||
if (! $relationship instanceof BelongsTo) {
|
||||
throw RelationNotFoundException::make($this->model, $relationshipName, BelongsTo::class);
|
||||
}
|
||||
|
||||
$this->where(
|
||||
$relationship->getQualifiedForeignKeyName(),
|
||||
'=',
|
||||
$related->getAttributeValue($relationship->getOwnerKeyName()),
|
||||
$boolean,
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an "BelongsTo" relationship with an "or where" clause to the query.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $related
|
||||
* @param string $relationship
|
||||
* @return $this
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function orWhereBelongsTo($related, $relationshipName = null)
|
||||
{
|
||||
return $this->whereBelongsTo($related, $relationshipName, 'or');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add subselect queries to include an aggregate value for a relationship.
|
||||
*
|
||||
|
||||
@@ -10,20 +10,21 @@ use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Traits\Conditionable;
|
||||
use Illuminate\Support\Traits\ForwardsCalls;
|
||||
use Illuminate\Support\Traits\Macroable;
|
||||
use Throwable;
|
||||
|
||||
abstract class Factory
|
||||
{
|
||||
use ForwardsCalls, Macroable {
|
||||
use Conditionable, ForwardsCalls, Macroable {
|
||||
__call as macroCall;
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of the factory's corresponding model.
|
||||
*
|
||||
* @var string
|
||||
* @var string|null
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
|
||||
+25
-2
@@ -8,6 +8,7 @@ use Illuminate\Contracts\Queue\QueueableCollection;
|
||||
use Illuminate\Contracts\Queue\QueueableEntity;
|
||||
use Illuminate\Contracts\Routing\UrlRoutable;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString;
|
||||
use Illuminate\Contracts\Support\Jsonable;
|
||||
use Illuminate\Database\ConnectionResolverInterface as Resolver;
|
||||
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
||||
@@ -22,7 +23,7 @@ use Illuminate\Support\Traits\ForwardsCalls;
|
||||
use JsonSerializable;
|
||||
use LogicException;
|
||||
|
||||
abstract class Model implements Arrayable, ArrayAccess, HasBroadcastChannel, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
|
||||
abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToString, HasBroadcastChannel, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
|
||||
{
|
||||
use Concerns\HasAttributes,
|
||||
Concerns\HasEvents,
|
||||
@@ -110,6 +111,13 @@ abstract class Model implements Arrayable, ArrayAccess, HasBroadcastChannel, Jso
|
||||
*/
|
||||
public $wasRecentlyCreated = false;
|
||||
|
||||
/**
|
||||
* Indicates that the object's string representation should be escaped when __toString is invoked.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $escapeWhenCastingToString = false;
|
||||
|
||||
/**
|
||||
* The connection resolver instance.
|
||||
*
|
||||
@@ -2128,7 +2136,22 @@ abstract class Model implements Arrayable, ArrayAccess, HasBroadcastChannel, Jso
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toJson();
|
||||
return $this->escapeWhenCastingToString
|
||||
? e($this->toJson())
|
||||
: $this->toJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the object's string representation should be escaped when __toString is invoked.
|
||||
*
|
||||
* @param bool $escape
|
||||
* @return $this
|
||||
*/
|
||||
public function escapeWhenCastingToString($escape = true)
|
||||
{
|
||||
$this->escapeWhenCastingToString = $escape;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,13 +25,18 @@ class RelationNotFoundException extends RuntimeException
|
||||
*
|
||||
* @param object $model
|
||||
* @param string $relation
|
||||
* @param string|null $type
|
||||
* @return static
|
||||
*/
|
||||
public static function make($model, $relation)
|
||||
public static function make($model, $relation, $type = null)
|
||||
{
|
||||
$class = get_class($model);
|
||||
|
||||
$instance = new static("Call to undefined relationship [{$relation}] on model [{$class}].");
|
||||
$instance = new static(
|
||||
is_null($type)
|
||||
? "Call to undefined relationship [{$relation}] on model [{$class}]."
|
||||
: "Call to undefined relationship [{$relation}] on model [{$class}] of type [{$type}].",
|
||||
);
|
||||
|
||||
$instance->model = $class;
|
||||
$instance->relation = $relation;
|
||||
|
||||
@@ -277,7 +277,7 @@ class BelongsToMany extends Relation
|
||||
|
||||
// Once we have an array dictionary of child objects we can easily match the
|
||||
// children back to their parent using the dictionary and the keys on the
|
||||
// the parent models. Then we will return the hydrated models back out.
|
||||
// parent models. Then we should return these hydrated models back out.
|
||||
foreach ($models as $model) {
|
||||
$key = $this->getDictionaryKey($model->{$this->parentKey});
|
||||
|
||||
|
||||
@@ -102,7 +102,9 @@ trait CanBeOneOfMany
|
||||
|
||||
if (isset($previous)) {
|
||||
$this->addOneOfManyJoinSubQuery($subQuery, $previous['subQuery'], $previous['column']);
|
||||
} elseif (isset($closure)) {
|
||||
}
|
||||
|
||||
if (isset($closure)) {
|
||||
$closure($subQuery);
|
||||
}
|
||||
|
||||
@@ -122,6 +124,12 @@ trait CanBeOneOfMany
|
||||
|
||||
$this->addConstraints();
|
||||
|
||||
$columns = $this->query->getQuery()->columns;
|
||||
|
||||
if (is_null($columns) || $columns === ['*']) {
|
||||
$this->select([$this->qualifyColumn('*')]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -137,7 +145,7 @@ trait CanBeOneOfMany
|
||||
{
|
||||
return $this->ofMany(collect(Arr::wrap($column))->mapWithKeys(function ($column) {
|
||||
return [$column => 'MAX'];
|
||||
})->all(), 'MAX', $relation ?: $this->guessRelationship());
|
||||
})->all(), 'MAX', $relation);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,7 +160,7 @@ trait CanBeOneOfMany
|
||||
{
|
||||
return $this->ofMany(collect(Arr::wrap($column))->mapWithKeys(function ($column) {
|
||||
return [$column => 'MIN'];
|
||||
})->all(), 'MIN', $relation ?: $this->guessRelationship());
|
||||
})->all(), 'MIN', $relation);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,14 +187,15 @@ trait CanBeOneOfMany
|
||||
protected function newOneOfManySubQuery($groupBy, $column = null, $aggregate = null)
|
||||
{
|
||||
$subQuery = $this->query->getModel()
|
||||
->newQuery();
|
||||
->newQuery()
|
||||
->withoutGlobalScopes($this->removedScopes());
|
||||
|
||||
foreach (Arr::wrap($groupBy) as $group) {
|
||||
$subQuery->groupBy($this->qualifyRelatedColumn($group));
|
||||
}
|
||||
|
||||
if (! is_null($column)) {
|
||||
$subQuery->selectRaw($aggregate.'('.$subQuery->getQuery()->grammar->wrap($column).') as '.$subQuery->getQuery()->grammar->wrap($column));
|
||||
$subQuery->selectRaw($aggregate.'('.$subQuery->getQuery()->grammar->wrap($subQuery->qualifyColumn($column)).') as '.$subQuery->getQuery()->grammar->wrap($column.'_aggregate'));
|
||||
}
|
||||
|
||||
$this->addOneOfManySubQueryConstraints($subQuery, $groupBy, $column, $aggregate);
|
||||
@@ -208,7 +217,7 @@ trait CanBeOneOfMany
|
||||
$subQuery->applyBeforeQueryCallbacks();
|
||||
|
||||
$parent->joinSub($subQuery, $this->relationName, function ($join) use ($on) {
|
||||
$join->on($this->qualifySubSelectColumn($on), '=', $this->qualifyRelatedColumn($on));
|
||||
$join->on($this->qualifySubSelectColumn($on.'_aggregate'), '=', $this->qualifyRelatedColumn($on));
|
||||
|
||||
$this->addOneOfManyJoinSubQueryConstraints($join, $on);
|
||||
});
|
||||
|
||||
@@ -407,7 +407,7 @@ abstract class Relation
|
||||
/**
|
||||
* Define the morph map for polymorphic relations and require all morphed models to be explicitly mapped.
|
||||
*
|
||||
* @param array|null $map
|
||||
* @param array $map
|
||||
* @param bool $merge
|
||||
* @return array
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user