Zend\Code\Generator\MethodGenerator::fromArray PHP Method

fromArray() public static method

Generate from array
public static fromArray ( array $array ) : MethodGenerator
$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(['.', '-', '_'], '', $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 'interface':
                    $method->setInterface($value);
                    break;
                case 'static':
                    $method->setStatic($value);
                    break;
                case 'visibility':
                    $method->setVisibility($value);
                    break;
                case 'returntype':
                    $method->setReturnType($value);
                    break;
            }
        }
        return $method;
    }

Usage Example

 /**
  * @param \Protobuf\Compiler\Entity $entity
  *
  * @return string
  */
 protected function generateMethod(Entity $entity)
 {
     $lines = $this->generateBody($entity);
     $body = implode(PHP_EOL, $lines);
     $method = MethodGenerator::fromArray(['name' => 'fromArray', 'body' => $body, 'static' => true, 'parameters' => [['name' => 'values', 'type' => 'array']], 'docblock' => ['shortDescription' => "{@inheritdoc}"]]);
     return $method;
 }
All Usage Examples Of Zend\Code\Generator\MethodGenerator::fromArray