1: <?php
2: namespace Pharborist\Functions;
3:
4: use Pharborist\ExpressionNode;
5: use Pharborist\CommaListNode;
6: use Pharborist\Node;
7: use Pharborist\NodeCollection;
8:
9: /**
10: * Trait for nodes that have arguments. For example, function calls.
11: */
12: trait ArgumentTrait {
13: /**
14: * @var CommaListNode
15: */
16: protected $arguments;
17:
18: /**
19: * @return CommaListNode
20: */
21: public function getArgumentList() {
22: return $this->arguments;
23: }
24:
25: /**
26: * @return NodeCollection|ExpressionNode[]
27: */
28: public function getArguments() {
29: return $this->arguments->getItems();
30: }
31:
32: /**
33: * @param mixed $argument
34: * The argument to prepend. Can be an ExpressionNode or a scalar value,
35: * which will be converted to an expression.
36: *
37: * @return $this
38: *
39: * @throws \InvalidArgumentException
40: */
41: public function appendArgument($argument) {
42: if (is_scalar($argument)) {
43: $argument = Node::fromValue($argument);
44: }
45:
46: if ($argument instanceof ExpressionNode) {
47: /** @var Node $argument */
48: $this->arguments->appendItem($argument);
49: }
50: else {
51: throw new \InvalidArgumentException();
52: }
53:
54: return $this;
55: }
56:
57: /**
58: * @param mixed $argument
59: * The argument to prepend. Can be an ExpressionNode or a scalar value,
60: * which will be converted to an expression.
61: *
62: * @return $this
63: *
64: * @throws \InvalidArgumentException
65: */
66: public function prependArgument($argument) {
67: if (is_scalar($argument)) {
68: $argument = Node::fromValue($argument);
69: }
70:
71: if ($argument instanceof ExpressionNode) {
72: /** @var Node $argument */
73: $this->arguments->prependItem($argument);
74: }
75: else {
76: throw new \InvalidArgumentException();
77: }
78:
79: return $this;
80: }
81:
82: /**
83: * Insert argument before argument at index.
84: *
85: * @param mixed $argument
86: * The argument to insert. Can be an ExpressionNode or a scalar value,
87: * which will be converted to an expression.
88: * @param int $index
89: * Position to insert argument at.
90: * @throws \OutOfBoundsException
91: * Index out of bounds.
92: * @throws \InvalidArgumentException
93: *
94: * @return $this
95: */
96: public function insertArgument($argument, $index) {
97: if (is_scalar($argument)) {
98: $argument = Node::fromValue($argument);
99: }
100:
101: if ($argument instanceof ExpressionNode) {
102: /** @var Node $argument */
103: $this->arguments->insertItem($argument, $index);
104: }
105: else {
106: throw new \InvalidArgumentException();
107: }
108:
109: return $this;
110: }
111:
112: /**
113: * Remove all arguments.
114: *
115: * @return $this
116: */
117: public function clearArguments() {
118: $this->arguments->clear();
119: return $this;
120: }
121: }
122: