public function setClass(ClassObject $classObject) { $this->classObject = $classObject; $this->width = 200; $this->variables = [self::TV_WIDTH => $this->width, self::TV_HEAD_HEIGHT => 50, self::TV_NAME => $classObject->getName(), self::TV_ATTRIBUTES => [], self::TV_METHODS => []]; foreach ($classObject->getAttributes() as $attribute) { $view = new VariableView($this->settings); $view->setVariableObject($attribute); $this->variables[self::TV_ATTRIBUTES][] = $view; } foreach ($classObject->getMethods() as $method) { $view = new MethodView($this->settings); $view->setMethod($method, self::INNER_WIDTH); $this->variables[self::TV_METHODS][] = $view; } $this->height = $this->getVariable(self::TV_HEAD_HEIGHT); foreach ($this->getVariable(self::TV_ATTRIBUTES) as $attribute) { $this->height += $attribute->height; } foreach ($this->getVariable(self::TV_METHODS) as $method) { $this->height += $method->height; } if ($this->getVariable(self::TV_ATTRIBUTES) and $this->getVariable(self::TV_METHODS)) { $this->height += 10; } elseif ($this->getVariable(self::TV_ATTRIBUTES)) { $this->height += 5; } elseif ($this->getVariable(self::TV_METHODS)) { $this->height += 15; } }
/** * @param Method $method * @param int $maxWidth Optional, maximum width in pixels. Will never break if negative */ public function setMethod(Method $method, $maxWidth = -1) { $this->method = $method; $this->variables = [self::TV_NAME => $method->getName(), self::TV_RETURN_TYPE => $method->getReturnType(), self::TV_ARGUMENTS => [], self::TV_FONT_HEIGHT => self::FONT_HEIGHT, self::TV_MULTI_LINE => false]; $arguments = []; foreach ($this->method->getArguments() as $argument) { $view = new VariableView($this->settings); $view->setVariableObject($argument); $arguments[] = trim($view->render()); } $joinedArguments = join(', ', $arguments); $characters = mb_strlen($method->getName()) + mb_strlen($joinedArguments) + mb_strlen($method->getReturnType()); if ($characters * self::FONT_WIDTH < $maxWidth || $maxWidth < 0) { $this->variables[self::TV_ARGUMENTS] = $joinedArguments; } else { $this->height = count($arguments) * self::FONT_HEIGHT + 40; $this->variables[self::TV_MULTI_LINE] = true; $this->variables[self::TV_ARGUMENTS] = $arguments; } }