Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class Attribute
/** @var array */
protected $keyIndexes = [];

/**
* @var array
*/
private $attributesRef = [];

/**
* @var string|int|null
*/
private $selfKey = null;

/**
* Constructor
*
Expand Down Expand Up @@ -84,6 +94,19 @@ public function getPrimaryAttribute()
return $this->primaryAttribute;
}

/**
* Provide a reference to all attributes, so we can dynamically exclude self
*
* @param array $allAttributes
* @param string|int $selfKey
* @return void
*/
public function setAttributesReference(array &$allAttributes, $selfKey)
{
$this->attributesRef = &$allAttributes;
$this->selfKey = $selfKey;
}

/**
* Set other attributes
*
Expand Down Expand Up @@ -116,7 +139,18 @@ public function addOtherAttribute(Attribute $otherAttribute)
*/
public function getOtherAttributes(): array
{
return $this->otherAttributes;
if (!empty($this->attributesRef) && $this->selfKey !== null) {
return array_filter(
$this->attributesRef,
function ($_, $key) {
return $key !== $this->selfKey;
},
ARRAY_FILTER_USE_BOTH
);
}

// fallback to legacy if not initialized
return $this->otherAttributes ?? [];
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,8 @@ protected function parseArrayAttribute(Attribute $attribute): array
}

// set other attributes to each attributes
foreach ($attributes as $i => $attr) {
$otherAttributes = $attributes;
unset($otherAttributes[$i]);
$attr->setOtherAttributes($otherAttributes);
foreach ($this->attributes as $i => $attr) {
$attr->setAttributesReference($this->attributes, $i);
}

return $attributes;
Expand Down