public function withLabel(int $key, $label): self
    {
        $nb_labels = count($this->labels);
        if ($key < - $nb_labels - 1 || $key > $nb_labels) {
            throw new OffsetOutOfBounds(sprintf('no label can be added with the submitted key : `%s`', $key));
        }

        if (0 > $key) {
            $key += $nb_labels;
        }

        if ($nb_labels === $key) {
            return $this->append($label);
        }

        if (-1 === $key) {
            return $this->prepend($label);
        }

        if (!$label instanceof HostInterface) {
            $label = new Host($label);
        }

        $label = $label->getContent();
        if ($label === $this->labels[$key]) {
            return $this;
        }

        $labels = $this->labels;
        $labels[$key] = $label;

        return new self(implode(self::SEPARATOR, array_reverse($labels)));
    }
public function withLabel(int $key, $label): self
    {
        $nb_labels = count($this->labels);
        if ($key < - $nb_labels - 1 || $key > $nb_labels) {
            throw new OffsetOutOfBounds(sprintf('no label can be added with the submitted key : `%s`', $key));
        }

        if (0 > $key) {
            $key += $nb_labels;
        }

        if ($nb_labels === $key) {
            return $this->append($label);
        }

        if (-1 === $key) {
            return $this->prepend($label);
        }

        if (!$label instanceof HostInterface) {
            $label = new Host($label);
        }

        $label = $label->getContent();
        if ($label === $this->labels[$key]) {
            return $this;
        }

        $labels = $this->labels;
        $labels[$key] = $label;

        return new self(implode(self::SEPARATOR, array_reverse($labels)));
    }
public function withLabel ( int $ key , $ label ) : self { $ nb_labels = count ( $ this -> labels ) ; if ( $ key < - $ nb_labels - 1 || $ key > $ nb_labels ) { throw new OffsetOutOfBounds ( sprintf ( 'no label can be added with the submitted key : `%s`' , $ key ) ) ; } if ( 0 > $ key ) { $ key += $ nb_labels ; } if ( $ nb_labels === $ key ) { return $ this -> append ( $ label ) ; } if ( - 1 === $ key ) { return $ this -> prepend ( $ label ) ; } if ( ! $ label instanceof HostInterface ) { $ label = new Host ( $ label ) ; } $ label = $ label -> getContent ( ) ; if ( $ label === $ this -> labels [ $ key ] ) { return $ this ; } $ labels = $ this -> labels ; $ labels [ $ key ] = $ label ; return new self ( implode ( self :: SEPARATOR , array_reverse ( $ labels ) ) ) ; }
Returns an instance with the modified label.

This method MUST retain the state of the current instance, and return
an instance that contains the new label

If $key is non-negative, the added label will be the label at $key position from the start.
If $key is negative, the added label will be the label at $key position from the end.

@throws OffsetOutOfBounds If the key is invalid
Returns an instance with the modified label .