kind = $kind; $this->value = $value; } public static function last(): self { return new self( self::KIND_LAST ); } public static function first(): self { return new self( self::KIND_FIRST ); } public static function at_index( int $index ): self { if ( $index < 0 ) { throw new \InvalidArgumentException( 'Element_Position: index must be >= 0.' ); } return new self( self::KIND_INDEX, $index ); } public static function after_type( string $element_type ): self { self::assert_non_empty_element_type( $element_type, 'after_type' ); return new self( self::KIND_AFTER_TYPE, $element_type ); } public static function before_type( string $element_type ): self { self::assert_non_empty_element_type( $element_type, 'before_type' ); return new self( self::KIND_BEFORE_TYPE, $element_type ); } private static function assert_non_empty_element_type( string $element_type, string $method ): void { if ( '' === trim( $element_type ) ) { throw new \InvalidArgumentException( esc_html( "Element_Position::{$method}: element_type must be a non-empty string." ) ); } } public function to_array(): array { return [ 'kind' => $this->kind, 'value' => $this->value, ]; } }