} */ public static function cleanOne( OutputValidator $validator, string $raw ): array { $clean = $validator->sanitizeUnit( array( 'heading' => '', 'items' => array( $raw ) ), 'bullets' ); $text = $clean['items'][0] ?? ''; return array( 'ok' => '' !== $text, 'text' => $text, 'findings' => $clean['findings'], ); } /** * Escape text for interpolation inside a ``-style prompt * wrapper, neutralizing any literal tag-like sequence (mirrors * PromptCompiler's own private escapeBlock()). * * @param string $text Raw text. * @return string */ public static function escapeBlock( string $text ): string { return htmlspecialchars( $text, ENT_QUOTES, 'UTF-8' ); } /** * Trim $text to at most $max_chars. The cut prefers the last clause or * sentence break before the limit (",", ";", ":", ".", "!" or "?" * followed by whitespace, so a decimal point never counts) as long as * that keeps at least half of the limit, so the result reads as a * complete phrase instead of ending on a dangling "and" or "with". * Otherwise it cuts at the last whitespace boundary so no word is cut * in half. Trailing punctuation left by either cut is trimmed. Returns * the original text unchanged when it already fits. * * @param string $text Text to trim. * @param int $max_chars Maximum character length. * @return array{text:string,trimmed:bool} */ public static function trimToWordBoundary( string $text, int $max_chars ): array { if ( $max_chars <= 0 || mb_strlen( $text, 'UTF-8' ) <= $max_chars ) { return array( 'text' => $text, 'trimmed' => false ); } $slice = mb_substr( $text, 0, $max_chars, 'UTF-8' ); $clause = self::beforeLastClauseBreak( $slice . mb_substr( $text, $max_chars, 1, 'UTF-8' ) ); if ( mb_strlen( $clause, 'UTF-8' ) * 2 >= $max_chars ) { return array( 'text' => $clause, 'trimmed' => true ); } $cut = mb_strrpos( $slice, ' ', 0, 'UTF-8' ); $result = ( false !== $cut && $cut > 0 ) ? mb_substr( $slice, 0, $cut, 'UTF-8' ) : $slice; $result = rtrim( $result, self::DANGLING_PUNCTUATION ); return array( 'text' => $result, 'trimmed' => true ); } /** * @param string $text Text to search. * @return string The text before its last clause or sentence break, trailing punctuation trimmed; '' when it has none. */ private static function beforeLastClauseBreak( string $text ): string { if ( ! preg_match_all( '/[,;:.!?](?=\s)/u', $text, $matches, PREG_OFFSET_CAPTURE ) ) { return ''; } $last = end( $matches[0] ); return rtrim( substr( $text, 0, $last[1] ), self::DANGLING_PUNCTUATION ); } /** * Remove repeated whitespace-delimited words across an ORDERED list of * texts, case-insensitive and Unicode-aware, keeping the first * occurrence and dropping only the duplicate word (not the whole text) * from every later occurrence (`Title.dedupe`; e.g. * "4K" repeated in both a product-type and a key-feature part). A * regular English plural counts as its singular ("Dresses" repeats * "Dress"), since a category name is usually the plural of the * product's own noun. * * @param string[] $texts Ordered plain texts (e.g. one per included title part). * @return string[] Same count and order, each with later duplicate words removed. */ public static function dedupeWordsAcross( array $texts ): array { $seen = array(); $result = array(); foreach ( $texts as $text ) { $tokens = preg_split( '/(\s+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); if ( ! is_array( $tokens ) ) { $result[] = $text; continue; } $kept = array(); foreach ( $tokens as $token ) { if ( '' === $token || 1 !== preg_match( '/[\p{L}\p{N}]/u', $token ) ) { $kept[] = $token; continue; } $key = self::wordKey( $token ); if ( isset( $seen[ $key ] ) ) { continue; } $seen[ $key ] = true; $kept[] = $token; } $joined = (string) preg_replace( '/\s+/u', ' ', implode( '', $kept ) ); $result[] = trim( $joined ); } return $result; } /** * dedupeWordsAcross()'s comparison key: the lowercase word without the * punctuation around it ("(2-Pack)" repeats "2-Pack"), with a regular * English plural ending ("-ies", "-ses"/"-xes"/"-zes"/"-ches"/"-shes", * "-s") reduced to its singular. Short words and anything outside a-z * keep their own lowercase form. * * @param string $token One whitespace-delimited word. * @return string */ private static function wordKey( string $token ): string { $key = mb_strtolower( $token, 'UTF-8' ); $bare = preg_replace( '/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/u', '', $key ); $key = ( is_string( $bare ) && '' !== $bare ) ? $bare : $key; if ( 1 !== preg_match( '/^[a-z]{4,}$/', $key ) ) { return $key; } if ( 'ies' === substr( $key, -3 ) ) { return substr( $key, 0, -3 ) . 'y'; } if ( 1 === preg_match( '/(ss|x|z|ch|sh)es$/', $key ) ) { return substr( $key, 0, -2 ); } if ( 1 === preg_match( '/[^s]s$/', $key ) ) { return substr( $key, 0, -1 ); } return $key; } /** * Resolve a FieldCatalog `{type,value,empty}` result to one display * string (lists joined with ", ", pairs joined as "label: value" * clauses). Mirrors the small private `scalarText()` helpers already * duplicated inside `SectionPlanner`/`ResultAssembler`; a third, shared * copy here is used by every formula generator that resolves a single * FieldRef to one value (Title/SeoMeta data parts, Faq conditions). * * @param array $resolved {type,value,empty}. * @return string */ public static function scalarText( array $resolved ): string { $type = (string) ( $resolved['type'] ?? 'text' ); if ( 'list' === $type ) { return implode( ', ', array_map( 'strval', (array) $resolved['value'] ) ); } if ( 'pairs' === $type ) { $parts = array(); foreach ( (array) $resolved['value'] as $pair ) { $parts[] = ( $pair['label'] ?? '' ) . ': ' . ( $pair['value'] ?? '' ); } return implode( '; ', $parts ); } return (string) ( $resolved['value'] ?? '' ); } }