@@ -95,6 +95,8 @@ public function generateFiles(
95
95
}
96
96
97
97
/**
98
+ * @deprecated Use addPropertiesGetterMethods
99
+ *
98
100
* Generation of getter methods. Use $skip callable to skip generation e. g. for value objects
99
101
*
100
102
* @param FileCollection $classBuilderCollection Only ClassBuilder objects are considered
@@ -115,28 +117,45 @@ public function addGetterMethodsForProperties(
115
117
}
116
118
117
119
foreach ($ classBuilderCollection as $ classBuilder ) {
118
- if (! $ classBuilder instanceof ClassBuilder) {
120
+ if (! $ classBuilder instanceof ClassBuilder
121
+ || true === ($ skip )($ classBuilder )
122
+ ) {
119
123
continue ;
120
124
}
121
- foreach ($ classBuilder ->getProperties () as $ classPropertyBuilder ) {
122
- $ methodName = ($ methodNameFilter )($ classPropertyBuilder ->getName ());
125
+ $ this ->addPropertiesGetterMethods ($ classBuilder , $ typed , $ methodNameFilter );
126
+ }
127
+ }
123
128
124
- if (true === ($ skip )($ classBuilder )
125
- || $ classBuilder ->hasMethod ($ methodName )
126
- ) {
127
- continue 2 ;
128
- }
129
- $ classBuilder ->addMethod (
130
- ClassMethodBuilder::fromScratch ($ methodName , $ typed )
131
- ->setReturnType ($ classPropertyBuilder ->getType ())
132
- ->setReturnTypeDocBlockHint ($ classPropertyBuilder ->getTypeDocBlockHint ())
133
- ->setBody ('return $this-> ' . $ classPropertyBuilder ->getName () . '; ' )
134
- );
129
+ /**
130
+ * Generation of getter methods.
131
+ *
132
+ * @param ClassBuilder $classBuilder
133
+ * @param bool $typed Should the generated code be typed
134
+ * @param callable $methodNameFilter Filter the property name to your desired method name e.g. with "get" prefix
135
+ */
136
+ public function addPropertiesGetterMethods (
137
+ ClassBuilder $ classBuilder ,
138
+ bool $ typed ,
139
+ callable $ methodNameFilter
140
+ ): void {
141
+ foreach ($ classBuilder ->getProperties () as $ classPropertyBuilder ) {
142
+ $ methodName = ($ methodNameFilter )($ classPropertyBuilder ->getName ());
143
+
144
+ if ($ classBuilder ->hasMethod ($ methodName )) {
145
+ continue ;
135
146
}
147
+ $ classBuilder ->addMethod (
148
+ ClassMethodBuilder::fromScratch ($ methodName , $ typed )
149
+ ->setReturnType ($ classPropertyBuilder ->getType ())
150
+ ->setReturnTypeDocBlockHint ($ classPropertyBuilder ->getTypeDocBlockHint ())
151
+ ->setBody ('return $this-> ' . $ classPropertyBuilder ->getName () . '; ' )
152
+ );
136
153
}
137
154
}
138
155
139
156
/**
157
+ * @deprecated Use addPropertiesClassConstants
158
+ *
140
159
* Generation of constants for each property. Use $skip callable to skip generation e. g. for value objects
141
160
*
142
161
* @param FileCollection $fileCollection Only ClassBuilder objects are considered
@@ -180,4 +199,34 @@ public function addClassConstantsForProperties(
180
199
}
181
200
}
182
201
}
202
+
203
+ /**
204
+ * Generation of constants for each property.
205
+ *
206
+ * @param ClassBuilder $classBuilder
207
+ * @param callable $constantNameFilter Converts the name to a proper class constant name
208
+ * @param callable $constantValueFilter Converts the name to a proper class constant value e.g. snake_case or camelCase
209
+ * @param int $visibility Visibility of the class constant
210
+ */
211
+ public function addPropertiesClassConstants (
212
+ ClassBuilder $ classBuilder ,
213
+ callable $ constantNameFilter ,
214
+ callable $ constantValueFilter ,
215
+ int $ visibility = ClassConstGenerator::FLAG_PUBLIC
216
+ ): void {
217
+ foreach ($ classBuilder ->getProperties () as $ classPropertyBuilder ) {
218
+ $ constantName = ($ constantNameFilter )($ classPropertyBuilder ->getName ());
219
+
220
+ if ($ classBuilder ->hasConstant ($ constantName )) {
221
+ continue ;
222
+ }
223
+ $ classBuilder ->addConstant (
224
+ ClassConstBuilder::fromScratch (
225
+ $ constantName ,
226
+ ($ constantValueFilter )($ classPropertyBuilder ->getName ()),
227
+ $ visibility
228
+ )
229
+ );
230
+ }
231
+ }
183
232
}
0 commit comments