$link_ids */ public function encode_link_ids( array $link_ids ): string { return json_encode( $link_ids, JSON_THROW_ON_ERROR ); } /** @return list */ public function decode_link_ids( string $json ): array { $decoded = $this->decoded_list( $json ); $link_ids = array(); foreach ( $decoded as $link_id ) { if ( ! is_int( $link_id ) ) { throw new UnexpectedValueException( esc_html( self::DAMAGED_MESSAGE ) ); } $link_ids[] = $link_id; } return $link_ids; } /** @param list $journal */ public function encode_journal( array $journal ): string { $rows = array(); foreach ( $journal as $entry ) { $rows[] = array( self::LINK_ID => $entry->link_id, self::POST_ID => $entry->post_id, self::CONTENT_BEFORE => $entry->content_before_fix, ); } return json_encode( $rows, JSON_THROW_ON_ERROR ); } /** @return list */ public function decode_journal( string $json ): array { $entries = array(); foreach ( $this->decoded_list( $json ) as $fields ) { $entries[] = $this->entry_from( is_array( $fields ) ? $fields : array() ); } return $entries; } /** @return list */ private function decoded_list( string $json ): array { $decoded = json_decode( $json, true ); if ( ! is_array( $decoded ) || ! array_is_list( $decoded ) ) { throw new UnexpectedValueException( esc_html( self::DAMAGED_MESSAGE ) ); } return $decoded; } /** @param array $fields */ private function entry_from( array $fields ): JournalEntry { $link_id = $fields[ self::LINK_ID ] ?? null; $post_id = $fields[ self::POST_ID ] ?? null; $content = $fields[ self::CONTENT_BEFORE ] ?? null; $is_valid = is_int( $link_id ) && is_int( $post_id ) && ( null === $content || is_string( $content ) ); if ( ! $is_valid ) { throw new UnexpectedValueException( esc_html( self::DAMAGED_MESSAGE ) ); } return new JournalEntry( $link_id, $post_id, $content ); } }