*/ class DecoratorHeadings extends Decorator { const FRONT_MATTER = 900; const MAIN_MATTER = 901; const APPENDIX = 902; /** * Internal state of this decorator. */ private $state; /** * Class constructor. * @param decorator The next decorator. */ function __construct($decorator) { parent::__construct($decorator); $this->state = DecoratorHeadings::FRONT_MATTER; } /** * Headers are transformed in part, chapter, section, subsection and subsubsection. */ function header($text, $level, $pos) { switch($level) { case 1: $this->h1($text, $pos); break; case 2: $this->h2($text, $pos); break; case 3: $this->h3($text, $pos); break; case 4: $this->h4($text, $pos); break; default: $this->h5($text, $pos); break; } } /** * Handles H1 level headers. * */ private function h1($text, $pos) { switch($this->state) { case DecoratorHeadings::FRONT_MATTER: $this->state = DecoratorHeadings::MAIN_MATTER; $this->decorator->header($text, 1, $pos); break; case DecoratorHeadings::MAIN_MATTER: $this->state = DecoratorHeadings::APPENDIX; $this->decorator->header($text, 1, $pos); break; case DecoratorHeadings::APPENDIX: $this->decorator->header($text, 3, $pos); break; default: trigger_error("h1 unexpected $this->state"); } } /** * Handles H2 level headers. * */ private function h2($text, $pos) { switch($this->state) { case DecoratorHeadings::FRONT_MATTER: $this->decorator->header($text, 3, $pos); break; case DecoratorHeadings::MAIN_MATTER: $this->decorator->header($text, 2, $pos); break; case DecoratorHeadings::APPENDIX: $this->decorator->header($text, 3, $pos); break; default: trigger_error("h2 unexpected $this->state"); } } private function h3($text, $pos) { $this->decorator->header($text, 3, $pos); } private function h4($text, $pos) { $this->decorator->header($text, 4, $pos); } private function h5($text, $pos) { $this->decorator->header($text, 5, $pos); } }