Overview

Namespaces

  • Pharborist
    • Constants
    • ControlStructures
    • Exceptions
    • Functions
    • Generators
    • Namespaces
    • Objects
    • Operators
    • Types
    • Variables

Classes

  • Pharborist\Variables\CompoundVariableNode
  • Pharborist\Variables\GlobalStatementNode
  • Pharborist\Variables\ReferenceVariableNode
  • Pharborist\Variables\StaticVariableNode
  • Pharborist\Variables\StaticVariableStatementNode
  • Pharborist\Variables\VariableNode
  • Pharborist\Variables\VariableVariableNode

Interfaces

  • Pharborist\Variables\VariableExpressionNode
  • Overview
  • Namespace
  • Class
  1: <?php
  2: namespace Pharborist\Types;
  3: 
  4: use Pharborist\ParentNode;
  5: use Pharborist\ParenTrait;
  6: use Pharborist\Token;
  7: use Pharborist\ExpressionNode;
  8: use Pharborist\Filter;
  9: use Pharborist\CommaListNode;
 10: use Pharborist\NodeCollection;
 11: use Pharborist\Parser;
 12: 
 13: /**
 14:  * A PHP array, e.g. `array(1, 3, 'banana', 'apple')`
 15:  */
 16: class ArrayNode extends ParentNode implements ExpressionNode {
 17:   use ParenTrait;
 18: 
 19:   /**
 20:    * @var CommaListNode
 21:    */
 22:   protected $elements;
 23: 
 24:   /**
 25:    * @return CommaListNode
 26:    */
 27:   public function getElementList() {
 28:     return $this->elements;
 29:   }
 30: 
 31:   /**
 32:    * @return NodeCollection|ArrayElementNode[]
 33:    */
 34:   public function getElements() {
 35:     return $this->elements->getItems();
 36:   }
 37: 
 38:   /**
 39:    * Tests if the array contains another array.
 40:    *
 41:    * @return boolean
 42:    */
 43:   public function isMultidimensional() {
 44:     foreach ($this->elements->getItems() as $element) {
 45:       if ($element instanceof ArrayPairNode) {
 46:         if ($element->getValue() instanceof ArrayNode) {
 47:           return TRUE;
 48:         }
 49:       }
 50:       elseif ($element instanceof ArrayNode) {
 51:         return TRUE;
 52:       }
 53:     }
 54:     return FALSE;
 55:   }
 56: 
 57:   /**
 58:    * Convert to PHP array.
 59:    *
 60:    * @return array
 61:    *   Array of scalars.
 62:    *
 63:    * @throws \BadMethodCallException
 64:    *   Thrown if array contains non scalar elements.
 65:    */
 66:   public function toValue() {
 67:     $ret = array();
 68:     foreach ($this->elements->getItems() as $element) {
 69:       if ($element instanceof ArrayNode) {
 70:         $ref[] = $element->toValue();
 71:       }
 72:       elseif ($element instanceof ArrayPairNode) {
 73:         $key = $element->getKey();
 74:         $value = $element->getValue();
 75:         $value_convertable = $value instanceof ScalarNode || $value instanceof ArrayNode;
 76:         if (!($key instanceof ScalarNode && $value_convertable)) {
 77:           throw new \BadMethodCallException('Can only convert scalar arrays.');
 78:         }
 79:         $ret[$key->toValue()] = $value->toValue();
 80:       }
 81:       elseif ($element instanceof ScalarNode || $element instanceof ArrayNode) {
 82:         /** @var ScalarNode|ArrayNode $element */
 83:         $ret[] = $element->toValue();
 84:       }
 85:       else {
 86:         throw new \BadMethodCallException('Can only convert scalar arrays.');
 87:       }
 88:     }
 89:     return $ret;
 90:   }
 91: 
 92:   /**
 93:    * Returns if the array has a specific key.
 94:    *
 95:    * @param mixed $key
 96:    *  Either a scalar value ('foo') or an ExpressionNode representing the key.
 97:    *  If $key is an ExpressionNode, the key's string representation is compared
 98:    *  with the string representations of the array keys. Otherwise, the actual
 99:    *  value is compared.
100:    * @param boolean $recursive
101:    *  Whether or not to check every level of the array.
102:    *
103:    * @return boolean
104:    *
105:    * @throws \InvalidArgumentException
106:    */
107:   public function hasKey($key, $recursive = TRUE) {
108:     if (!($key instanceof ExpressionNode) && !is_scalar($key)) {
109:       throw new \InvalidArgumentException();
110:     }
111: 
112:     $keys = $this->getKeys($recursive);
113:     if (is_scalar($key)) {
114:       return $keys
115:         ->filter(Filter::isInstanceOf('\Pharborist\Types\ScalarNode'))
116:         ->is(function(ScalarNode $node) use ($key) {
117:           return $node->toValue() === $key;
118:         });
119:     }
120:     else {
121:       return $keys
122:         ->is(function(ExpressionNode $expr) use ($key) {
123:           return $expr->getText() === $key->getText();
124:         });
125:     }
126:   }
127: 
128:   /**
129:    * Get the keys of the array.
130:    *
131:    * @param boolean $recursive
132:    *   (optional) TRUE to get keys of array elements that are also arrays.
133:    *
134:    * @return NodeCollection
135:    */
136:   public function getKeys($recursive = TRUE) {
137:     $keys = new NodeCollection();
138:     $index = 0;
139:     foreach ($this->elements->getItems() as $element) {
140:       if ($element instanceof ArrayPairNode) {
141:         $keys->add($element->getKey());
142:         $value = $element->getValue();
143:       }
144:       else {
145:         $keys->add(Token::integer($index++));
146:         $value = $element;
147:       }
148: 
149:       if ($recursive && $value instanceof ArrayNode) {
150:         $keys->add($value->getKeys($recursive));
151:       }
152:     }
153:     return $keys;
154:   }
155: 
156:   /**
157:    * Get the values of the array.
158:    *
159:    * @param boolean $recursive
160:    *   (optional) TRUE to get values of array elements that are also arrays.
161:    *
162:    * @return NodeCollection
163:    */
164:   public function getValues($recursive = TRUE) {
165:     $values = new NodeCollection();
166:     foreach ($this->elements->getItems() as $element) {
167:       if ($element instanceof ArrayPairNode) {
168:         $value = $element->getValue();
169:         if ($recursive && $value instanceof ArrayNode) {
170:           $values->add($value->getValues($recursive));
171:         }
172:         else {
173:           $values->add($value);
174:         }
175:       }
176:       else {
177:         $values->add($element);
178:       }
179:     }
180:     return $values;
181:   }
182: 
183:   /**
184:    * @param ArrayElementNode[] $elements
185:    *   Array elements.
186:    *
187:    * @return ArrayNode
188:    */
189:   public static function create($elements) {
190:     /** @var ArrayNode $node */
191:     $node = Parser::parseExpression('[]');
192:     /** @var \Pharborist\Node $element */
193:     foreach ($elements as $element) {
194:       $node->getElementList()->appendItem($element);
195:     }
196:     return $node;
197:   }
198: }
199: 
Pharborist API documentation generated by ApiGen