public function generate()
{
$type = $this->type;
if ($type != self::TYPE_AUTO) {
$type = $this->getValidatedType($type);
}
$value = $this->value;
if ($type == self::TYPE_AUTO) {
$type = $this->getAutoDeterminedType($value);
}
$isArrayType = in_array($type, [self::TYPE_ARRAY, self::TYPE_ARRAY_LONG, self::TYPE_ARRAY_SHORT]);
if ($isArrayType) {
foreach ($value as &$curValue) {
if ($curValue instanceof self) {
continue;
}
if (is_array($curValue)) {
$newType = $type;
} else {
$newType = self::TYPE_AUTO;
}
$curValue = new self($curValue, $newType, self::OUTPUT_MULTIPLE_LINE, $this->getConstants());
}
}
$output = '';
switch ($type) {
case self::TYPE_BOOLEAN:
case self::TYPE_BOOL:
$output .= $value ? 'true' : 'false';
break;
case self::TYPE_STRING:
$output .= self::escape($value);
break;
case self::TYPE_NULL:
$output .= 'null';
break;
case self::TYPE_NUMBER:
case self::TYPE_INTEGER:
case self::TYPE_INT:
case self::TYPE_FLOAT:
case self::TYPE_DOUBLE:
case self::TYPE_CONSTANT:
$output .= $value;
break;
case self::TYPE_ARRAY:
case self::TYPE_ARRAY_LONG:
case self::TYPE_ARRAY_SHORT:
if ($type == self::TYPE_ARRAY_SHORT) {
$startArray = '[';
$endArray = ']';
} else {
$startArray = 'array(';
$endArray = ')';
}
$output .= $startArray;
if ($this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
$output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1);
}
$outputParts = [];
$noKeyIndex = 0;
foreach ($value as $n => $v) {
/* @var $v ValueGenerator */
$v->setArrayDepth($this->arrayDepth + 1);
$partV = $v->generate();
$short = false;
if (is_int($n)) {
if ($n === $noKeyIndex) {
$short = true;
$noKeyIndex++;
} else {
$noKeyIndex = max($n + 1, $noKeyIndex);
}
}
if ($short) {
$outputParts[] = $partV;
} else {
$outputParts[] = (is_int($n) ? $n : self::escape($n)) . ' => ' . $partV;
}
}
$padding = $this->outputMode == self::OUTPUT_MULTIPLE_LINE ? self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1) : ' ';
$output .= implode(',' . $padding, $outputParts);
if ($this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
if (count($outputParts) > 0) {
$output .= ',';
}
$output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth);
}
$output .= $endArray;
break;
case self::TYPE_OTHER:
default:
throw new Exception\RuntimeException(sprintf('Type "%s" is unknown or cannot be used as property default value.', get_class($value)));
}
return $output;
}
public function testPropertyDefaultValueCanHandleComplexArrayOfTypes() { $targetValue = array(5, 'one' => 1, 'two' => '2', 'constant1' => '__DIR__ . \'/anydir1/anydir2\'', array('baz' => true, 'foo', 'bar', array('baz1', 'baz2', 'constant2' => 'ArrayObject::STD_PROP_LIST')), new ValueGenerator('PHP_EOL', 'constant')); $expectedSource = <<<EOS array( 5, 'one' => 1, 'two' => '2', 'constant1' => __DIR__ . '/anydir1/anydir2', array( 'baz' => true, 'foo', 'bar', array( 'baz1', 'baz2', 'constant2' => ArrayObject::STD_PROP_LIST ) ), PHP_EOL ) EOS; $valueGenerator = new ValueGenerator(); $valueGenerator->initEnvironmentConstants(); $valueGenerator->setValue($targetValue); $generatedTargetSource = $valueGenerator->generate(); $this->assertEquals($expectedSource, $generatedTargetSource); }