Debian Codegraph @

vendor/mck89/peast/lib/Peast/Syntax/Node/StringLiteral.php

raw
1 <?php
2 /**
3 * This file is part of the Peast package
4 *
5 * (c) Marco Marchiò <[email protected]>
6 *
7 * For the full copyright and license information refer to the LICENSE file
8 * distributed with this source code
9 */
10 namespace Peast\Syntax\Node;
11
12 use Peast\Syntax\Utils;
13
14 /**
15 * A node that represents a string literal.
16 *
17 * @author Marco Marchiò <[email protected]>
18 */
19 class StringLiteral extends Literal
20 {
21 /**
22 * Map of node properties
23 *
24 * @var array
25 */
26 protected $propertiesMap = array(
27 "format" => false
28 );
29
30 //Format constants
31 /**
32 * Double quoted string
33 */
34 const DOUBLE_QUOTED = "double";
35
36 /**
37 * Single quoted string
38 */
39 const SINGLE_QUOTED = "single";
40
41 /**
42 * String format
43 *
44 * @var string
45 */
46 protected $format = self::DOUBLE_QUOTED;
47
48 /**
49 * Sets node's value
50 *
51 * @param string $value Value
52 *
53 * @return $this
54 */
55 public function setValue($value)
56 {
57 $this->value = (string) $value;
58 //Force recalculation of the raw value
59 return $this->setFormat($this->format);
60 }
61
62 /**
63 * Sets node's raw value
64 *
65 * @param mixed $raw Raw value
66 *
67 * @return $this
68 *
69 * @throws \Exception
70 */
71 public function setRaw($raw)
72 {
73 if (!is_string($raw) || strlen($raw) < 2) {
74 throw new \Exception("Invalid string");
75 }
76 $startQuote = $raw[0];
77 $endQuote = substr($raw, -1);
78 if (($startQuote !== "'" && $startQuote !== '"') ||
79 $startQuote !== $endQuote
80 ) {
81 throw new \Exception("Invalid string");
82 }
83 $this->value = Utils::unquoteLiteralString($raw);
84 $this->setFormat($raw[0] === "'" ?
85 self::SINGLE_QUOTED :
86 self::DOUBLE_QUOTED
87 );
88 $this->raw = $raw;
89 return $this;
90 }
91
92 /**
93 * Returns string format
94 *
95 * @return string
96 */
97 public function getFormat()
98 {
99 return $this->format;
100 }
101
102 /**
103 * Sets string format
104 *
105 * @param string $format Format, one of the format constants
106 *
107 * @return $this
108 */
109 public function setFormat($format)
110 {
111 $this->format = $format;
112 $quote = $format === self::SINGLE_QUOTED ? "'" : '"';
113 $this->raw = Utils::quoteLiteralString($this->value, $quote);
114 return $this;
115 }
116 }