@@ -95,6 +95,8 @@ public function generateFiles(
9595 }
9696
9797 /**
98+ * @deprecated Use addPropertiesGetterMethods
99+ *
98100 * Generation of getter methods. Use $skip callable to skip generation e. g. for value objects
99101 *
100102 * @param FileCollection $classBuilderCollection Only ClassBuilder objects are considered
@@ -115,28 +117,45 @@ public function addGetterMethodsForProperties(
115117 }
116118
117119 foreach ($ classBuilderCollection as $ classBuilder ) {
118- if (! $ classBuilder instanceof ClassBuilder) {
120+ if (! $ classBuilder instanceof ClassBuilder
121+ || true === ($ skip )($ classBuilder )
122+ ) {
119123 continue ;
120124 }
121- foreach ($ classBuilder ->getProperties () as $ classPropertyBuilder ) {
122- $ methodName = ($ methodNameFilter )($ classPropertyBuilder ->getName ());
125+ $ this ->addPropertiesGetterMethods ($ classBuilder , $ typed , $ methodNameFilter );
126+ }
127+ }
123128
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 ;
135146 }
147+ $ classBuilder ->addMethod (
148+ ClassMethodBuilder::fromScratch ($ methodName , $ typed )
149+ ->setReturnType ($ classPropertyBuilder ->getType ())
150+ ->setReturnTypeDocBlockHint ($ classPropertyBuilder ->getTypeDocBlockHint ())
151+ ->setBody ('return $this-> ' . $ classPropertyBuilder ->getName () . '; ' )
152+ );
136153 }
137154 }
138155
139156 /**
157+ * @deprecated Use addPropertiesClassConstants
158+ *
140159 * Generation of constants for each property. Use $skip callable to skip generation e. g. for value objects
141160 *
142161 * @param FileCollection $fileCollection Only ClassBuilder objects are considered
@@ -180,4 +199,34 @@ public function addClassConstantsForProperties(
180199 }
181200 }
182201 }
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+ }
183232}
0 commit comments