|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Xbyter\PhpObjectMapping; |
| 4 | + |
| 5 | +class PropertyDocParser |
| 6 | +{ |
| 7 | + /** |
| 8 | + * 文档属性类型 |
| 9 | + * |
| 10 | + * @var array |
| 11 | + */ |
| 12 | + protected static array $_properties = []; |
| 13 | + |
| 14 | + /** |
| 15 | + * 获取对象文档属性类型 |
| 16 | + * |
| 17 | + * @param string $className |
| 18 | + * @return array |
| 19 | + * @throws \ReflectionException |
| 20 | + * @throws \Exception |
| 21 | + */ |
| 22 | + public static function getProperties(string $className): array |
| 23 | + { |
| 24 | + $_properties = &self::$_properties[$className]; |
| 25 | + if (isset($_properties)) { |
| 26 | + return self::$_properties[$className]; |
| 27 | + } |
| 28 | + |
| 29 | + $_properties = []; |
| 30 | + $reflection = new \ReflectionClass($className); |
| 31 | + $properties = $reflection->getProperties(); |
| 32 | + foreach ($properties as $property) { |
| 33 | + if (!$property->isPublic()) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + $name = $property->getName(); |
| 38 | + $_properties[$name] = [ |
| 39 | + 'name' => $name, |
| 40 | + 'type' => 'string', |
| 41 | + 'class_name' => null, |
| 42 | + 'nullable' => true, |
| 43 | + 'decorator' => null,//装饰器, 用于转换为新的值 |
| 44 | + ]; |
| 45 | + |
| 46 | + //根据参数Doc文档设置指定对象值 |
| 47 | + $doc = $property->getDocComment(); |
| 48 | + //解析$type装饰类,用于转换某种具体规则的值。比如转换时区(业务逻辑层以固定时区做逻辑处理,展示层可以加$type转为相应时区值) |
| 49 | + if ($doc) { |
| 50 | + $_properties[$name]['decorator'] = self::getDecoratorByDoc($doc); |
| 51 | + } |
| 52 | + |
| 53 | + //如果属性是强类型, 则以强类型为主将类型注入到propertyTypes里 |
| 54 | + if ($property->hasType() && $property->getType() instanceof \ReflectionNamedType) { |
| 55 | + $typeName = $property->getType()->getName(); |
| 56 | + $_properties[$name] = [ |
| 57 | + 'type' => $typeName, |
| 58 | + 'class_name' => self::isScalarType($typeName) || self::isArrayType($typeName) ? null : $typeName, |
| 59 | + 'nullable' => $property->getType()->allowsNull(), |
| 60 | + ] + $_properties[$name]; |
| 61 | + |
| 62 | + //如果不是数组的话则已经确定类型了, 不必往下走 |
| 63 | + if (!self::isArrayType($typeName)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + //非强类型解析 |
| 70 | + if ($doc) { |
| 71 | + //解析类似@var \DemoNamespace\DemoProperty[]的文档对象 |
| 72 | + preg_match("/@var \??(.*?)(\[\])?[\|\s\*]/", $doc, $matches); |
| 73 | + |
| 74 | + //解析属性类型到propertyTypes |
| 75 | + //如果未设置强类型, 或者强类型为数组并且注释类型为对象数组时则重新设置类型 |
| 76 | + $matchType = $matches[1] ?? ''; |
| 77 | + if (!$matchType) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + //如果注释没有命名空间, 则查看是否再当前命名空间下存在该类 |
| 82 | + if (strpos($matchType, "\\") !== 0) { |
| 83 | + $matchType = $reflection->getNamespaceName() . "\\" . $matchType; |
| 84 | + } |
| 85 | + |
| 86 | + $isArray = isset($matches[2]); |
| 87 | + if ($isArray && $_properties[$name]['type'] === 'array' && class_exists($matchType)) { |
| 88 | + $_properties[$name]['class_name'] = $matchType; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return $_properties; |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + /** |
| 98 | + * 获取装饰类 |
| 99 | + * |
| 100 | + * @param string $doc |
| 101 | + * @return string|null |
| 102 | + * @throws \Exception |
| 103 | + */ |
| 104 | + protected static function getDecoratorByDoc(string $doc): ?string |
| 105 | + { |
| 106 | + //解析类似@return DecoratorInterface 的文档对象 |
| 107 | + preg_match("/@return[ ]+([\w\\\]+)/", $doc, $matches); |
| 108 | + $decorator = $matches[1] ?? ''; |
| 109 | + if (!$decorator || !class_exists($decorator)) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + return $decorator; |
| 114 | + } |
| 115 | + |
| 116 | + public static function isStringType(string $type): bool |
| 117 | + { |
| 118 | + return $type === 'string'; |
| 119 | + } |
| 120 | + |
| 121 | + public static function isIntType(string $type): bool |
| 122 | + { |
| 123 | + return $type === 'int'; |
| 124 | + } |
| 125 | + |
| 126 | + public static function isFloatType(string $type): bool |
| 127 | + { |
| 128 | + return $type === 'float'; |
| 129 | + } |
| 130 | + |
| 131 | + public static function isBoolType(string $type): bool |
| 132 | + { |
| 133 | + return $type === 'bool'; |
| 134 | + } |
| 135 | + |
| 136 | + public static function isArrayType(string $type): bool |
| 137 | + { |
| 138 | + return $type === 'array'; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * 是否是标量(简单类型) |
| 143 | + * |
| 144 | + * @param string $type |
| 145 | + * @return bool |
| 146 | + */ |
| 147 | + public static function isScalarType(string $type): bool |
| 148 | + { |
| 149 | + return self::isStringType($type) || self::isIntType($type) || self::isFloatType($type) || self::isBoolType($type); |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + public static function isNumberType(string $type): bool |
| 154 | + { |
| 155 | + return self::isIntType($type) || self::isFloatType($type); |
| 156 | + } |
| 157 | + |
| 158 | + public static function isClassType(array $property): bool |
| 159 | + { |
| 160 | + return $property['class_name'] && !self::isArrayType($property['type']); |
| 161 | + } |
| 162 | + |
| 163 | + public static function isClassArrayType(array $property): bool |
| 164 | + { |
| 165 | + return $property['class_name'] && self::isArrayType($property['type']); |
| 166 | + } |
| 167 | +} |
0 commit comments