Current File : /home/jvzmxxx/wiki/extensions/Wikibase/lib/includes/Formatters/EscapingValueFormatter.php
<?php

namespace Wikibase\Lib;

use InvalidArgumentException;
use ValueFormatters\ValueFormatter;

/**
 * EscapingValueFormatter wraps another ValueFormatter and
 * applies a transformation (escaping) to that formatter's output.
 *
 * @since 0.5
 *
 * @license GPL-2.0+
 * @author Daniel Kinzler
 */
class EscapingValueFormatter implements ValueFormatter {

	/**
	 * @var ValueFormatter
	 */
	private $formatter;

	/**
	 * @var callable
	 */
	private $escapeCallback;

	/**
	 * @param ValueFormatter $formatter A formatter returning plain text.
	 * @param callable $escapeCallback A callable taking plain text and returning escaped text.
	 *
	 * @throws InvalidArgumentException
	 */
	public function __construct( ValueFormatter $formatter, $escapeCallback ) {
		if ( !is_callable( $escapeCallback ) ) {
			throw new InvalidArgumentException( '$escapeCallback must be callable' );
		}

		$this->formatter = $formatter;
		$this->escapeCallback = $escapeCallback;
	}

	/**
	 * @see ValueFormatter::format
	 *
	 * @param mixed $value
	 *
	 * @return string Typically wikitext or HTML, depending on the $escapeCallback provided.
	 */
	public function format( $value ) {
		$text = $this->formatter->format( $value );
		$escaped = call_user_func( $this->escapeCallback, $text );
		return $escaped;
	}

}