Debian Codegraph @

vendor/mck89/peast/lib/Peast/Syntax/Node/TemplateLiteral.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 /**
13 * A node that represents a template literal.
14 * For example: `this is a ${test()} template`
15 *
16 * @author Marco Marchiò <[email protected]>
17 */
18 class TemplateLiteral extends Node implements Expression
19 {
20 /**
21 * Map of node properties
22 *
23 * @var array
24 */
25 protected $propertiesMap = array(
26 "parts" => true,
27 "quasis" => false,
28 "expressions" => false
29 );
30
31 /**
32 * Array of quasis that are the literal parts of the template
33 *
34 * @var TemplateElement[]
35 */
36 protected $quasis = array();
37
38 /**
39 * Array of expressions inside the template
40 *
41 * @var Expression[]
42 */
43 protected $expressions = array();
44
45 /**
46 * Returns the array of quasis that are the literal parts of the template
47 *
48 * @return TemplateElement[]
49 */
50 public function getQuasis()
51 {
52 return $this->quasis;
53 }
54
55 /**
56 * Sets the array of quasis that are the literal parts of the template
57 *
58 * @param TemplateElement[] $quasis Quasis
59 *
60 * @return $this
61 */
62 public function setQuasis($quasis)
63 {
64 $this->assertArrayOf($quasis, "TemplateElement");
65 $this->quasis = $quasis;
66 return $this;
67 }
68
69 /**
70 * Returns the array of expressions inside the template
71 *
72 * @return Expression[]
73 */
74 public function getExpressions()
75 {
76 return $this->expressions;
77 }
78
79 /**
80 * Sets the array of expressions inside the template
81 *
82 * @param Expression[] $expressions Expressions
83 *
84 * @return $this
85 */
86 public function setExpressions($expressions)
87 {
88 $this->assertArrayOf($expressions, "Expression");
89 $this->expressions = $expressions;
90 return $this;
91 }
92
93 /**
94 * Returns an array of the template parts (quasis and expressions)
95 *
96 * @return array
97 */
98 public function getParts()
99 {
100 // It must be a list of quasis and expressions alternated
101 $parts = array();
102 foreach ($this->quasis as $k => $val) {
103 $parts[] = $val;
104 if (isset($this->expressions[$k])) {
105 $parts[] = $this->expressions[$k];
106 }
107 }
108 return $parts;
109 }
110
111 /**
112 * Sets the array of the template parts (quasis and expressions)
113 *
114 * @param array Template parts
115 *
116 * @return $this
117 */
118 public function setParts($parts)
119 {
120 $this->assertArrayOf($parts, array("Expression", "TemplateElement"));
121 $quasis = $expressions = array();
122 foreach ($parts as $part) {
123 if ($part instanceof TemplateElement) {
124 $quasis[] = $part;
125 } else {
126 $expressions[] = $part;
127 }
128 }
129 return $this->setQuasis($quasis)->setExpressions($expressions);
130 }
131
132 /**
133 * Returns a serializable version of the node
134 *
135 * @return array
136 */
137 #[\ReturnTypeWillChange]
138 public function jsonSerialize()
139 {
140 $ret = parent::jsonSerialize();
141 unset($ret["parts"]);
142 return $ret;
143 }
144 }