label = self::normalizeRequiredString( 'label', $label ); $this->providerLabel = self::normalizeRequiredString( 'providerLabel', $providerLabel ); $this->presetClassName = self::normalizeOptionalString( 'presetClassName', $presetClassName ); $this->prefersTrackedUrl = $prefersTrackedUrl; } /** * Pre-formatted button label. */ public function label(): string { return $this->label; } /** * Human-readable provider name. */ public function providerLabel(): string { return $this->providerLabel; } /** * Caller-supplied extra CSS class string, or null. */ public function presetClassName(): ?string { return $this->presetClassName; } /** * Whether the link-attribute builder should prefer the * affiliate-tracked URL when tracking was applied. */ public function prefersTrackedUrl(): bool { return $this->prefersTrackedUrl; } /** * Report whether this input equals the supplied one. * * Equality is structural: every field must match with strict * comparison. * * @param self $other Other input to compare against. */ public function equals( self $other ): bool { return $this->label === $other->label && $this->providerLabel === $other->providerLabel && $this->presetClassName === $other->presetClassName && $this->prefersTrackedUrl === $other->prefersTrackedUrl; } /** * Validate and normalize a required string field at * construction. * * @param string $field Field name for the error message. * @param string $value Raw value as supplied by the caller. * * @throws InvalidArgumentException When `$value` is empty * after trimming. */ private static function normalizeRequiredString( string $field, string $value ): string { $trimmed = trim( $value ); if ( $trimmed === '' ) { $message = sprintf( 'ButtonConfigInput %s must be a non-empty string.', $field ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Developer-facing message, never rendered to HTML. throw new InvalidArgumentException( $message ); } return $trimmed; } /** * Validate and normalize a nullable string field that, when * non-null, must carry non-empty content. * * @param string $field Field name for the error message. * @param string|null $value Raw value, or null. * * @throws InvalidArgumentException When `$value` is the empty * string after trimming. */ private static function normalizeOptionalString( string $field, ?string $value ): ?string { if ( $value === null ) { return null; } $trimmed = trim( $value ); if ( $trimmed === '' ) { $message = sprintf( 'ButtonConfigInput %s must be null or a non-empty string.', $field ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Developer-facing message, never rendered to HTML. throw new InvalidArgumentException( $message ); } return $trimmed; } }