1: <?php
2: namespace Pharborist\Objects;
3:
4: use Pharborist\ExpressionNode;
5: use Pharborist\Node;
6: use Pharborist\ParentNode;
7: use Pharborist\Parser;
8: use Pharborist\TokenNode;
9:
10: /**
11: * A single class member in a ClassMemberListNode.
12: *
13: * The relationship between class members and class member lists can be
14: * a bit confusing. Both of these are considered class member lists:
15: * ```
16: * protected $foo; // A member list with one member.
17: * private $bar, $baz;
18: * ```
19: * The individual members in those lists are $foo, $bar, and $baz. Each of
20: * them is a ClassMemberNode, which will render as `$foo`, `$bar`, and `$baz`,
21: * respectively. And each is a child of a parent ClassMemberListNode, which
22: * has a visibility and static-ness.
23: *
24: * ClassMemberNode's getVisibility(), setVisibility(), and is/get/setStatic()
25: * methods are actually convenience methods which call the same method on the
26: * parent member list. But the visibility and static keywords are still
27: * attributes of the *list*, not the individual member.
28: *
29: * @see ClassMemberListNode
30: */
31: class ClassMemberNode extends ParentNode {
32: /**
33: * @var Node
34: */
35: protected $name;
36:
37: /**
38: * @var Node
39: */
40: protected $value;
41:
42: /**
43: * Creates a new class member.
44: *
45: * @param string $name
46: * The name of the member, with or without the leading $.
47: * @param \Pharborist\ExpressionNode $value
48: * The default value of the member, if any.
49: * @param string $visibility
50: * The member's visibility. Can be public, private, or protected. Defaults to
51: * public.
52: *
53: * @return ClassMemberListNode
54: *
55: * @todo Not all expressions can be default values, but I forget what sorts of
56: * expressions are valid for this. Will need better sanity checking here.
57: */
58: public static function create($name, ExpressionNode $value = NULL, $visibility = 'public') {
59: $code = $visibility . ' $' . ltrim($name, '$');
60: if ($value instanceof ExpressionNode) {
61: $code .= ' = ' . $value->getText();
62: }
63: /** @var ClassNode $class_node */
64: $class_node = Parser::parseSnippet('class Foo { ' . $code . '; }');
65: return $class_node->getStatements()[0]->remove();
66: }
67:
68: /**
69: * @return Node
70: */
71: public function getName() {
72: return $this->name;
73: }
74:
75: /**
76: * @return Node
77: */
78: public function getValue() {
79: return $this->value;
80: }
81:
82: /**
83: * @return ClassMemberListNode
84: */
85: protected function getClassMemberListNode() {
86: return $this->parent()->parent();
87: }
88:
89: /**
90: * @see \Pharborist\ClassMemberListNode::isStatic()
91: *
92: * @return boolean
93: */
94: public function isStatic() {
95: return $this->getClassMemberListNode()->isStatic();
96: }
97:
98: /**
99: * @see \Pharborist\ClassMemberListNode::getStatic()
100: *
101: * @return \Pharborist\TokenNode
102: */
103: public function getStatic() {
104: return $this->getClassMemberListNode()->getStatic();
105: }
106:
107: /**
108: * @see \Pharborist\ClassMemberListNode::setStatic()
109: *
110: * @param boolean $is_static
111: *
112: * @return $this
113: */
114: public function setStatic($is_static) {
115: $this->getClassMemberListNode()->setStatic($is_static);
116: return $this;
117: }
118:
119: /**
120: * @see \Pharborist\VisibilityTrait::getVisibility()
121: *
122: * @return \Pharborist\TokenNode
123: */
124: public function getVisibility() {
125: return $this->getClassMemberListNode()->getVisibility();
126: }
127:
128: /**
129: * @see \Pharborist\VisibilityTrait::setVisibility()
130: *
131: * @param string|integer|TokenNode|NULL $visibility
132: *
133: * @return $this
134: */
135: public function setVisibility($visibility) {
136: $this->getClassMemberListNode()->setVisibility($visibility);
137: return $this;
138: }
139: }
140: