public function matchReflector(\ReflectionParameter $reflector) { $this->setReference($reflector->isPassedByReference()); // Match the reflector's type hint. if ($reflector->isArray()) { $this->setTypeHint('array'); } elseif ($reflector->isCallable()) { $this->setTypeHint('callable'); } elseif ($class = $reflector->getClass()) { $this->setTypeHint($class->getName()); } // Match the reflector's default value, if there is one. It will be a // scalar value, an array of scalar values, or a constant. if ($reflector->isDefaultValueAvailable()) { if ($reflector->isDefaultValueConstant()) { $this->setValue(ConstantNode::create($reflector->getDefaultValueConstantName())); } else { $this->setValue(Node::fromValue($reflector->getDefaultValue())); } } return $this; }
private function updateArray(ArrayNode $node, $data) { $i = 0; foreach ($node->getElements() as $el) { if ($el instanceof ArrayPairNode) { $k = (string) $el->getKey(); $k = trim($k, '"\''); $v = $el->getValue(); if (!isset($data[$k])) { $this->cleanAround($el); } else { if ($v instanceof ArrayNode && is_array($data[$k])) { $v = $this->updateArray($v, $data[$k]); unset($data[$k]); } else { $v->replaceWith(Parser::parseExpression($data[$k])); unset($data[$k]); } } } elseif ($el instanceof ArrayNode && (!isset($data[$i]) || is_array($data[$i]))) { if (!isset($data[$i])) { $this->cleanAround($el); } else { $this->updateArray($el, $data[$i]); unset($data[$i]); $i++; } } else { if (!isset($data[$i])) { $this->cleanAround($el); } else { $el->replaceWith(Parser::parseExpression($data[$i])); unset($data[$i]); $i++; } } } foreach ($data as $key => $val) { $v = Parser::parseExpression(self::var_codify($val)); if (!is_integer($key)) { $v = ArrayPairNode::create(Node::fromValue($key), $v); } $comma = false; $list = $node->getElementList(); $children = []; foreach ($list->children() as $child) { $children[] = $child; } $prev = end($children); if ($prev) { do { if ((string) $prev === ',') { $comma = true; break; } } while (is_object($prev) && ($prev = $prev->previous()) instanceof WhitespaceNode); } else { $comma = true; } $indent = 0; $prev = end($children); while ($prev && strpos($prev, "\n") === false) { $prev = $prev->previous(); } $indent = ''; if ($prev) { $prev = explode("\n", (string) $prev); $prev = array_pop($prev); for ($i = 0; $i < strlen($prev); $i++) { if (in_array($prev[$i], ["\t", ' '])) { $indent .= $prev[$i]; } else { break; } } } if (!$comma) { $list->append(Token::comma()); } $list->append(Token::newline()); if ($indent) { $list->append(WhitespaceNode::create($indent)); } $list->append($v); } }
/** * @expectedException \InvalidArgumentException */ public function testFromScalarInvalidArgument() { $node = Node::fromValue(new \stdClass()); }