1<?php
2
3/**
4 * Exception thrown if a scanner status finds does not find a
5 * valid character.
6 *
7 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
8 * @copyright Copyright 2010-2014 PhpCss Team
9 */
10
11namespace PhpCss\Exception {
12
13  use PhpCss;
14  use PhpCss\Scanner\Status;
15  use UnexpectedValueException;
16
17  /**
18   * Exception thrown if a scanner status finds does not
19   * find a valid character.
20   */
21  class InvalidCharacterException
22    extends UnexpectedValueException
23    implements PhpCssException {
24
25    /** @var int $offset byte offset */
26    private $_offset;
27    /** @var string $buffer string buffer */
28    private $_buffer;
29    /** @var Status $status scanner status */
30    private $_status;
31
32    /**
33     * @param string $buffer
34     * @param int $offset
35     * @param Status $status
36     */
37    public function __construct(string $buffer, int $offset, Status $status) {
38      $this->_buffer = $buffer;
39      $this->_offset = $offset;
40      $this->_status = $status;
41      parent::__construct(
42        sprintf(
43          'Invalid char "%s" for status "%s" at offset #%d in "%s"',
44          $this->getChar(),
45          get_class($this->_status),
46          $this->_offset,
47          $this->_buffer
48        )
49      );
50    }
51
52    /**
53     * Match the utf-8 character at the byte offset position.
54     *
55     * @return string
56     */
57    public function getChar(): string {
58      if (preg_match('(.)suS', $this->_buffer, $match, 0, $this->_offset)) {
59        return $match[0];
60      }
61      return '';
62    }
63
64    public function getOffset(): int {
65      return $this->_offset;
66    }
67
68    public function getBuffer(): string {
69      return $this->_buffer;
70    }
71
72    public function getStatus(): Status {
73      return $this->_status;
74    }
75  }
76}
77