Permalink
Browse files

Merge branch 'feature/3378' into develop

Close #3378
  • Loading branch information...
2 parents 2a8ca79 + 319a681 commit ccb663851d121650ff2a41461b6773c6ba389097 @weierophinney weierophinney committed Jan 14, 2013
@@ -91,36 +91,6 @@ public function removeFlag($flag)
return $this;
}
- /**
- * @param DocBlockGenerator|string $docBlock
- * @throws Exception\InvalidArgumentException
- * @return AbstractMemberGenerator
- */
- public function setDocBlock($docBlock)
- {
- if (is_string($docBlock)) {
- $docBlock = new DocBlockGenerator($docBlock);
- } elseif (!$docBlock instanceof DocBlockGenerator) {
- throw new Exception\InvalidArgumentException(sprintf(
- '%s is expecting either a string, array or an instance of %s\DocBlockGenerator',
- __METHOD__,
- __NAMESPACE__
- ));
- }
-
- $this->docBlock = $docBlock;
-
- return $this;
- }
-
- /**
- * @return DocBlockGenerator
- */
- public function getDocBlock()
- {
- return $this->docBlock;
- }
-
/**
* @param bool $isAbstract
* @return AbstractMemberGenerator
@@ -135,7 +105,7 @@ public function setAbstract($isAbstract)
*/
public function isAbstract()
{
- return ($this->flags & self::FLAG_ABSTRACT);
+ return (bool) ($this->flags & self::FLAG_ABSTRACT);
}
/**
@@ -152,7 +122,7 @@ public function setFinal($isFinal)
*/
public function isFinal()
{
- return ($this->flags & self::FLAG_FINAL);
+ return (bool) ($this->flags & self::FLAG_FINAL);
}
/**
@@ -229,4 +199,33 @@ public function getName()
return $this->name;
}
+ /**
+ * @param DocBlockGenerator|string $docBlock
+ * @throws Exception\InvalidArgumentException
+ * @return AbstractMemberGenerator
+ */
+ public function setDocBlock($docBlock)
+ {
+ if (is_string($docBlock)) {
+ $docBlock = new DocBlockGenerator($docBlock);
+ } elseif (!$docBlock instanceof DocBlockGenerator) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ '%s is expecting either a string, array or an instance of %s\DocBlockGenerator',
+ __METHOD__,
+ __NAMESPACE__
+ ));
+ }
+
+ $this->docBlock = $docBlock;
+
+ return $this;
+ }
+
+ /**
+ * @return DocBlockGenerator
+ */
+ public function getDocBlock()
+ {
+ return $this->docBlock;
+ }
}
@@ -185,15 +185,14 @@ public static function fromArray(array $values)
{
$fileGenerator = new static;
foreach ($values as $name => $value) {
- switch ($name) {
+ switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
case 'filename':
- $fileGenerator->filename = $value;
+ $fileGenerator->setFilename($value);
continue;
case 'class':
$fileGenerator->setClass(($value instanceof ClassGenerator) ? : ClassGenerator::fromArray($value));
continue;
- case 'requiredFiles':
- case 'required_files':
+ case 'requiredfiles':
$fileGenerator->setRequiredFiles($value);
continue;
default:
@@ -23,11 +23,6 @@ class MethodGenerator extends AbstractMemberGenerator
*/
protected $docBlock = null;
- /**
- * @var bool
- */
- protected $isFinal = false;
-
/**
* @var ParameterGenerator[]
*/
@@ -76,6 +71,66 @@ public static function fromReflection(MethodReflection $reflectionMethod)
return $method;
}
+ /**
+ * Generate from array
+ *
+ * @configkey name string [required] Class Name
+ * @configkey docblock string The docblock information
+ * @configkey flags int Flags, one of MethodGenerator::FLAG_ABSTRACT MethodGenerator::FLAG_FINAL
+ * @configkey parameters string Class which this class is extending
+ * @configkey body string
+ * @configkey abstract bool
+ * @configkey final bool
+ * @configkey static bool
+ * @configkey visibility string
+ *
+ * @throws Exception\InvalidArgumentException
+ * @param array $array
+ * @return MethodGenerator
+ */
+ public static function fromArray(array $array)
+ {
+ if (!isset($array['name'])) {
+ throw new Exception\InvalidArgumentException(
+ 'Method generator requires that a name is provided for this object'
+ );
+ }
+
+ $method = new static($array['name']);
+ foreach ($array as $name => $value) {
+ // normalize key
+ switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
+ case 'docblock':
+ $docBlock = ($value instanceof DocBlockGenerator) ? $value : DocBlockGenerator::fromArray($value);
+ $method->setDocBlock($docBlock);
+ break;
+ case 'flags':
+ $method->setFlags($value);
+ break;
+ case 'parameters':
+ $method->setParameters($value);
+ break;
+ case 'body':
+ $method->setBody($value);
+ break;
+ case 'abstract':
+ $method->setAbstract($value);
+ break;
+ case 'final':
+ $method->setFinal($value);
+ break;
+ case 'static':
+ $method->setStatic($value);
+ break;
+ case 'visibility':
+ $method->setVisibility($value);
+ break;
+ }
+ }
+
+ return $method;
+ }
+
/**
* @param string $name
* @param array $parameters
@@ -77,6 +77,61 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
return $param;
}
+ /**
+ * Generate from array
+ *
+ * @configkey name string [required] Class Name
+ * @configkey type string
+ * @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
+ * @configkey passedbyreference bool
+ * @configkey position int
+ * @configkey sourcedirty bool
+ * @configkey indentation string
+ * @configkey sourcecontent string
+ *
+ * @throws Exception\InvalidArgumentException
+ * @param array $array
+ * @return ParameterGenerator
+ */
+ public static function fromArray(array $array)
+ {
+ if (!isset($array['name'])) {
+ throw new Exception\InvalidArgumentException(
+ 'Paramerer generator requires that a name is provided for this object'
+ );
+ }
+
+ $param = new static($array['name']);
+ foreach ($array as $name => $value) {
+ // normalize key
+ switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
+ case 'type':
+ $param->setType($value);
+ break;
+ case 'defaultvalue':
+ $param->setDefaultValue($value);
+ break;
+ case 'passedbyreference':
+ $param->setPassedByReference($value);
+ break;
+ case 'position':
+ $param->setPosition($value);
+ break;
+ case 'sourcedirty':
+ $param->setSourceDirty($value);
+ break;
+ case 'indentation':
+ $param->setIndentation($value);
+ break;
+ case 'sourcecontent':
+ $param->setSourceContent($value);
+ break;
+ }
+ }
+
+ return $param;
+ }
+
/**
* @param string $name
* @param string $type
@@ -65,6 +65,65 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
return $property;
}
+ /**
+ * Generate from array
+ *
+ * @configkey name string [required] Class Name
+ * @configkey const bool
+ * @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
+ * @configkey flags int
+ * @configkey abstract bool
+ * @configkey final bool
+ * @configkey static bool
+ * @configkey visibility string
+ *
+ * @throws Exception\InvalidArgumentException
+ * @param array $array
+ * @return PropertyGenerator
+ */
+ public static function fromArray(array $array)
+ {
+ if (!isset($array['name'])) {
+ throw new Exception\InvalidArgumentException(
+ 'Property generator requires that a name is provided for this object'
+ );
+ }
+
+ $property = new static($array['name']);
+ foreach ($array as $name => $value) {
+ // normalize key
+ switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
+ case 'const':
+ $property->setConst($value);
+ break;
+ case 'defaultvalue':
+ $property->setDefaultValue($value);
+ break;
+ case 'docblock':
+ $docBlock = ($value instanceof DocBlockGenerator) ? $value : DocBlockGenerator::fromArray($value);
+ $property->setDocBlock($docBlock);
+ break;
+ case 'flags':
+ $property->setFlags($value);
+ break;
+ case 'abstract':
+ $property->setAbstract($value);
+ break;
+ case 'final':
+ $property->setFinal($value);
+ break;
+ case 'static':
+ $property->setStatic($value);
+ break;
+ case 'visibility':
+ $property->setVisibility($value);
+ break;
+ }
+ }
+
+ return $property;
+ }
+
/**
* @param string $name
* @param PropertyValueGenerator|string|array $defaultValue
@@ -104,7 +163,7 @@ public function setConst($const)
*/
public function isConst()
{
- return ($this->flags & self::FLAG_CONSTANT);
+ return (bool) ($this->flags & self::FLAG_CONSTANT);
}
/**
@@ -186,8 +186,7 @@ public function testHasProperty()
public function testToString()
{
- $classGenerator = ClassGenerator::fromArray(
- array(
+ $classGenerator = ClassGenerator::fromArray(array(
'name' => 'SampleClass',
'flags' => ClassGenerator::FLAG_ABSTRACT,
'extendedClass' => 'ExtendedClassName',
@@ -371,8 +370,7 @@ public function testAddUses()
public function testCreateFromArrayWithDocBlockFromArray()
{
- $classGenerator = ClassGenerator::fromArray(
- array(
+ $classGenerator = ClassGenerator::fromArray(array(
'name' => 'SampleClass',
'docblock' => array(
'shortdescription' => 'foo',
@@ -385,8 +383,7 @@ public function testCreateFromArrayWithDocBlockFromArray()
public function testCreateFromArrayWithDocBlockInstance()
{
- $classGenerator = ClassGenerator::fromArray(
- array(
+ $classGenerator = ClassGenerator::fromArray(array(
'name' => 'SampleClass',
'docblock' => new DocBlockGenerator('foo'),
));
@@ -23,7 +23,7 @@
* @group Zend_Code_Generator
* @group Zend_Code_Generator_Php
*/
-class PhpMethodTest extends \PHPUnit_Framework_TestCase
+class MethodGeneratorTest extends \PHPUnit_Framework_TestCase
{
public function testMethodConstructor()
{
@@ -179,4 +179,27 @@ public function testDefaultValueGenerationDoesNotIncludeTrailingSemicolon()
$generated = $method->generate();
$this->assertRegexp('/array \$options = array\(\)\)/', $generated, $generated);
}
+
+ public function testCreateFromArray()
+ {
+ $methodGenerator = MethodGenerator::fromArray(array(
+ 'name' => 'SampleMethod',
+ 'body' => 'foo',
+ 'docblock' => array(
+ 'shortdescription' => 'foo',
+ ),
+ 'abstract' => true,
+ 'final' => true,
+ 'static' => true,
+ 'visibility' => MethodGenerator::VISIBILITY_PROTECTED,
+ ));
+
+ $this->assertEquals('SampleMethod', $methodGenerator->getName());
+ $this->assertEquals('foo', $methodGenerator->getBody());
+ $this->assertInstanceOf('Zend\Code\Generator\DocBlockGenerator', $methodGenerator->getDocBlock());
+ $this->assertTrue($methodGenerator->isAbstract());
+ $this->assertTrue($methodGenerator->isFinal());
+ $this->assertTrue($methodGenerator->isStatic());
+ $this->assertEquals(MethodGenerator::VISIBILITY_PROTECTED, $methodGenerator->getVisibility());
+ }
}
Oops, something went wrong.

0 comments on commit ccb6638

Please sign in to comment.