Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 2a14807

Browse files
committed
Support for constants with complex values (PHP 5.6 compatible)
1 parent 03fbc4d commit 2a14807

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/Generator/ClassGenerator.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -526,14 +526,11 @@ public function addConstant($name, $value)
526526
));
527527
}
528528

529-
if (empty($value) || !is_string($value)) {
530-
throw new Exception\InvalidArgumentException(sprintf(
531-
'%s expects value for constant, value must be a string',
532-
__METHOD__
533-
));
534-
}
529+
$this->validateConstantValue($value);
535530

536-
return $this->addConstantFromGenerator(new PropertyGenerator($name, $value, PropertyGenerator::FLAG_CONSTANT));
531+
return $this->addConstantFromGenerator(
532+
new PropertyGenerator($name, new PropertyValueGenerator($value), PropertyGenerator::FLAG_CONSTANT)
533+
);
537534
}
538535

539536
/**
@@ -986,4 +983,29 @@ public function generate()
986983

987984
return $output;
988985
}
986+
987+
/**
988+
* @param mixed $value
989+
*
990+
* @return void
991+
*
992+
* @throws Exception\InvalidArgumentException
993+
*/
994+
private function validateConstantValue($value)
995+
{
996+
if (null === $value || is_scalar($value)) {
997+
return;
998+
}
999+
1000+
if (is_array($value)) {
1001+
array_walk($value, [$this, 'validateConstantValue']);
1002+
1003+
return;
1004+
}
1005+
1006+
throw new Exception\InvalidArgumentException(sprintf(
1007+
'Expected value for constant, value must be a "scalar" or "null", "%s" found',
1008+
gettype($value)
1009+
));
1010+
}
9891011
}

0 commit comments

Comments
 (0)