setArguments(...$arguments); } /** * Sets the arguments. * * @param mixed $arguments The arguments to set. * * @return static * * @internal */ public function setArguments(...$arguments) { $arguments = ArrayUtils::flatten($arguments, true); if ($arguments instanceof static) { $this->arguments = clone $arguments->arguments; return $this; } if (static::MIN_ARGUMENTS > count($arguments)) { throw new InvalidArgumentException( 'Qualifier "' . static::getName() . '" must have at least ' . static::MIN_ARGUMENTS . ' arguments(s)' ); } if (static::MAX_ARGUMENTS !== 0 && count($arguments) > static::MAX_ARGUMENTS) { throw new InvalidArgumentException( 'Qualifier "' . static::getName() . '" can have no more than ' . static::MAX_ARGUMENTS . ' arguments(s)' ); } foreach ($this->validators as $validator) { $validator->validate(...$arguments); } $this->arguments = $arguments; return $this; } /** * Adds values. * * @param mixed $values The values to add. * * @return static * * @internal */ public function addValues(...$values) { $this->arguments = array_merge($this->arguments, $values); return $this; } /** * Sets a value. * * @param BaseComponent|null $value The value to set. * * @return $this * * @internal */ public function setValue(BaseComponent $value = null) { return $this->setSimpleValue($value ? $value->getName() : null, $value); } /** * Sets a simple unnamed value specified by name(for uniqueness) and the actual value. * * @param string $name The name of the argument. * @param BaseComponent|mixed $value The value of the argument. * * @return $this * * @internal */ public function setSimpleValue($name, $value = null) { ArrayUtils::addNonEmpty($this->arguments, $name, $value); return $this; } /** * Gets an argument value by name. * * @param string $name The name of the argument. * * @return mixed * * @internal */ public function getSimpleValue($name) { return ArrayUtils::get($this->arguments, $name); } /** * Sets a named value. * * @param BaseComponent|mixed $value The value to set. * * @return $this * * @internal */ public function setNamedValue(BaseComponent $value = null) { return $this->setSimpleNamedValue($value ? $value->getName() : null, $value); } /** * Sets named values. * * @param BaseComponent ...$values The values to set. * * @return $this * * @internal */ public function setNamedValues(BaseComponent ...$values) { foreach ($values as $index => $value) { if ($index === 0) { $this->setSimpleNamedValue($value ? $value::getName() : null, $value); } else { $this->addValues($value); } } return $this; } /** * Sets a simple named value specified by name (for uniqueness) and the actual value. * * @param string $name The name of the argument. * @param BaseComponent|mixed $value The value of the argument. * * @return $this * * @internal */ public function setSimpleNamedValue($name, $value = null) { ArrayUtils::addNonEmpty($this->namedArguments, $name, $value); return $this; } /** * Sets in the inner order of the arguments. * * @param array $argumentOrder A list of argument keys in the correct order. * * @return $this * * @internal */ public function setArgumentOrder($argumentOrder) { if (! empty($argumentOrder)) { $this->argumentOrder = $argumentOrder; } return $this; } /** * Serializes to string. * * @return string */ public function __toString() { foreach ($this->arguments as $argumentKey => $argumentValue) { if ($argumentValue instanceof EffectName) { $this->argumentOrder[] = $argumentKey; } } $values = ArrayUtils::implodeFiltered( static::VALUE_DELIMITER, ArrayUtils::sortByArray($this->arguments, $this->argumentOrder) ); $namedValues = ArrayUtils::implodeAssoc( ArrayUtils::sortByArray($this->namedArguments), static::VALUE_DELIMITER, static::KEY_VALUE_DELIMITER ); return ArrayUtils::implodeFiltered(static::VALUE_DELIMITER, [$values, $namedValues]); } /** * Serializes to json. * * @return mixed */ public function jsonSerialize() { return ArrayUtils::flatten( ArrayUtils::safeFilter(ArrayUtils::mergeNonEmpty($this->arguments, $this->namedArguments)) ); } }