|
| 1 | +# Coding standards |
| 2 | + |
| 3 | +The `CodingStandardsStrategyInterface` lets you customize how names are normalized during code generation. |
| 4 | +This is useful when your project has specific naming conventions that differ from the defaults. |
| 5 | + |
| 6 | +By default, `DefaultCodingStandardsStrategy` is used, which delegates to the built-in `Normalizer` methods. |
| 7 | +This matches the existing behavior, so no changes are needed for existing users. |
| 8 | + |
| 9 | +## Interface |
| 10 | + |
| 11 | +```php |
| 12 | +use Phpro\SoapClient\CodeGenerator\CodingStandards\CodingStandardsStrategyInterface; |
| 13 | + |
| 14 | +interface CodingStandardsStrategyInterface |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Convert SOAP type name to PHP class/enum name (e.g. "my-type" -> "MyType") |
| 18 | + */ |
| 19 | + public function normalizeTypeName(string $name): string; |
| 20 | + |
| 21 | + /** |
| 22 | + * Convert SOAP operation name to PHP method name (e.g. "GetUsers" -> "getUsers") |
| 23 | + */ |
| 24 | + public function normalizeOperationName(string $method): string; |
| 25 | + |
| 26 | + /** |
| 27 | + * Normalize XML namespace segment for PHP namespace (e.g. "my-prefix" -> "MyPrefix") |
| 28 | + * Return null to skip the segment. |
| 29 | + */ |
| 30 | + public function normalizeNamespaceSegment(string $segment): ?string; |
| 31 | + |
| 32 | + /** |
| 33 | + * Normalize enum value to PHP enum case name (e.g. "some-value" -> "SomeValue") |
| 34 | + */ |
| 35 | + public function normalizeEnumCaseName(string $value): string; |
| 36 | + |
| 37 | + /** |
| 38 | + * Generate accessor method name from prefix + property name |
| 39 | + * (e.g. "get" + "firstName" -> "getFirstName") |
| 40 | + */ |
| 41 | + public function generatePropertyAccessorMethodName(string $prefix, string $property): string; |
| 42 | + |
| 43 | + /** |
| 44 | + * Normalize a parameter name for constructors and setters. |
| 45 | + * Input is the fixed property name. Output is the parameter name for PHP code. |
| 46 | + * This enables named arguments: new Foo(fooBar: 'x') while property stays $foo_bar. |
| 47 | + */ |
| 48 | + public function normalizeParameterName(string $propertyName): string; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Configuration |
| 53 | + |
| 54 | +Configure coding standards via `Config::setCodingStandards()`. |
| 55 | +The generated config uses `return ($config = Config::create())` so that `$config` can be referenced in nested calls: |
| 56 | + |
| 57 | +```php |
| 58 | +use Phpro\SoapClient\CodeGenerator\Config\Config; |
| 59 | +use Phpro\SoapClient\CodeGenerator\Config\Destination; |
| 60 | +use Phpro\SoapClient\CodeGenerator\Config\TypeNamespaceMap; |
| 61 | +use Phpro\SoapClient\CodeGenerator\TypeNamespaceMap\Strategy\PrefixBasedTypeNamespaceStrategy; |
| 62 | + |
| 63 | +return ($config = Config::create()) |
| 64 | + ->setCodingStandards(new MyCodingStandards()) |
| 65 | + ->setEngine(...) |
| 66 | + ->setTypeNamespaceMap( |
| 67 | + TypeNamespaceMap::create(new Destination('src/Type', 'App\\Type')) |
| 68 | + ->withStrategy(new PrefixBasedTypeNamespaceStrategy($config->getCodingStandards())) |
| 69 | + ) |
| 70 | + ->setClient(...) |
| 71 | + ->setClassMap(...) |
| 72 | +; |
| 73 | +``` |
| 74 | + |
| 75 | +When using `PrefixBasedTypeNamespaceStrategy`, pass the coding standards to its constructor so that namespace segments are normalized using the same strategy. |
| 76 | + |
| 77 | +## Creating a custom strategy |
| 78 | + |
| 79 | +Implement `CodingStandardsStrategyInterface` to define your own naming conventions: |
| 80 | + |
| 81 | +```php |
| 82 | +use Phpro\SoapClient\CodeGenerator\CodingStandards\CodingStandardsStrategyInterface; |
| 83 | + |
| 84 | +class MyCodingStandards implements CodingStandardsStrategyInterface |
| 85 | +{ |
| 86 | + public function normalizeTypeName(string $name): string |
| 87 | + { |
| 88 | + // e.g. prefix all types |
| 89 | + return 'Soap' . ucfirst($name); |
| 90 | + } |
| 91 | + |
| 92 | + public function normalizeOperationName(string $method): string |
| 93 | + { |
| 94 | + return lcfirst($method); |
| 95 | + } |
| 96 | + |
| 97 | + public function normalizeNamespaceSegment(string $segment): ?string |
| 98 | + { |
| 99 | + return ucfirst($segment); |
| 100 | + } |
| 101 | + |
| 102 | + public function normalizeEnumCaseName(string $value): string |
| 103 | + { |
| 104 | + return strtoupper($value); |
| 105 | + } |
| 106 | + |
| 107 | + public function generatePropertyAccessorMethodName(string $prefix, string $property): string |
| 108 | + { |
| 109 | + return $prefix . ucfirst($property); |
| 110 | + } |
| 111 | + |
| 112 | + public function normalizeParameterName(string $propertyName): string |
| 113 | + { |
| 114 | + return lcfirst($propertyName); |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Accessing coding standards in custom assemblers |
| 120 | + |
| 121 | +Inside a custom assembler, the `CodeGeneratorContext` is available through the assembler context: |
| 122 | + |
| 123 | +```php |
| 124 | +use Phpro\SoapClient\CodeGenerator\Assembler\AssemblerInterface; |
| 125 | +use Phpro\SoapClient\CodeGenerator\Context\ContextInterface; |
| 126 | +use Phpro\SoapClient\CodeGenerator\Context\PropertyContext; |
| 127 | + |
| 128 | +class MyAssembler implements AssemblerInterface |
| 129 | +{ |
| 130 | + public function canAssemble(ContextInterface $context): bool |
| 131 | + { |
| 132 | + return $context instanceof PropertyContext; |
| 133 | + } |
| 134 | + |
| 135 | + public function assemble(ContextInterface $context): void |
| 136 | + { |
| 137 | + assert($context instanceof PropertyContext); |
| 138 | + |
| 139 | + $codingStandards = $context->getCodeGeneratorContext()->codingStandards; |
| 140 | + $typeNamespaceMap = $context->getCodeGeneratorContext()->typeNamespaceMap; |
| 141 | + |
| 142 | + // Use coding standards to generate a custom method name: |
| 143 | + $methodName = $codingStandards->generatePropertyAccessorMethodName( |
| 144 | + 'has', |
| 145 | + $context->getProperty()->getName() |
| 146 | + ); |
| 147 | + |
| 148 | + // Or use the convenience methods on Property: |
| 149 | + $getterName = $context->getProperty()->methodName('get'); |
| 150 | + $paramName = $context->getProperty()->parameterName(); |
| 151 | + |
| 152 | + // ... |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
0 commit comments