|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nette Framework (https://nette.org) |
| 5 | + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\PhpGenerator; |
| 11 | + |
| 12 | +use Nette; |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * Class/Interface/Trait/Enum description. |
| 17 | + */ |
| 18 | +abstract class ClassLike |
| 19 | +{ |
| 20 | + use Nette\SmartObject; |
| 21 | + use Traits\CommentAware; |
| 22 | + use Traits\AttributeAware; |
| 23 | + |
| 24 | + public const |
| 25 | + VISIBILITY_PUBLIC = 'public', |
| 26 | + VISIBILITY_PROTECTED = 'protected', |
| 27 | + VISIBILITY_PRIVATE = 'private'; |
| 28 | + |
| 29 | + private ?PhpNamespace $namespace; |
| 30 | + private ?string $name; |
| 31 | + |
| 32 | + |
| 33 | + public static function from(string|object $class, bool $withBodies = false, ?bool $materializeTraits = null): self |
| 34 | + { |
| 35 | + if ($materializeTraits !== null) { |
| 36 | + trigger_error(__METHOD__ . '() parameter $materializeTraits has been removed (is always false).', E_USER_DEPRECATED); |
| 37 | + } |
| 38 | + return (new Factory) |
| 39 | + ->fromClassReflection(new \ReflectionClass($class), $withBodies); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + /** @deprecated use from(..., withBodies: true) */ |
| 44 | + public static function withBodiesFrom(string|object $class): self |
| 45 | + { |
| 46 | + trigger_error(__METHOD__ . '() is deprecated, use from(..., withBodies: true)', E_USER_DEPRECATED); |
| 47 | + return (new Factory) |
| 48 | + ->fromClassReflection(new \ReflectionClass($class), withBodies: true); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public static function fromCode(string $code): self |
| 53 | + { |
| 54 | + return (new Factory) |
| 55 | + ->fromClassCode($code); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + public function __construct(string $name, ?PhpNamespace $namespace = null) |
| 60 | + { |
| 61 | + $this->setName($name); |
| 62 | + $this->namespace = $namespace; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + public function __toString(): string |
| 67 | + { |
| 68 | + return (new Printer)->printClass($this, $this->namespace); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** @deprecated an object can be in multiple namespaces */ |
| 73 | + public function getNamespace(): ?PhpNamespace |
| 74 | + { |
| 75 | + return $this->namespace; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + public function setName(?string $name): static |
| 80 | + { |
| 81 | + if ($name !== null && (!Helpers::isIdentifier($name) || isset(Helpers::KEYWORDS[strtolower($name)]))) { |
| 82 | + throw new Nette\InvalidArgumentException("Value '$name' is not valid class name."); |
| 83 | + } |
| 84 | + |
| 85 | + $this->name = $name; |
| 86 | + return $this; |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + public function getName(): ?string |
| 91 | + { |
| 92 | + return $this->name; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + public function isClass(): bool |
| 97 | + { |
| 98 | + return $this instanceof ClassType; |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + public function isInterface(): bool |
| 103 | + { |
| 104 | + return $this instanceof InterfaceType; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + public function isTrait(): bool |
| 109 | + { |
| 110 | + return $this instanceof TraitType; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + public function isEnum(): bool |
| 115 | + { |
| 116 | + return $this instanceof EnumType; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + /** @param string[] $names */ |
| 121 | + protected function validateNames(array $names): void |
| 122 | + { |
| 123 | + foreach ($names as $name) { |
| 124 | + if (!Helpers::isNamespaceIdentifier($name, allowLeadingSlash: true)) { |
| 125 | + throw new Nette\InvalidArgumentException("Value '$name' is not valid class name."); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + public function validate(): void |
| 132 | + { |
| 133 | + } |
| 134 | +} |
0 commit comments