skir-php-generator 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  Generates framework-agnostic PHP data objects, RPC clients, and server procedure contracts from Skir schemas.
10
10
 
11
- Generated PHP uses `php-skir/runtime` for dense JSON serialization. Install `php-skir/client` when using generated RPC clients and `php-skir/server` when using generated server contracts.
11
+ Generated PHP uses `php-skir/runtime` for dense JSON, readable JSON, and binary serialization. Install `php-skir/client` when using generated RPC clients and `php-skir/server` when using generated server contracts.
12
12
 
13
13
  The npm package uses [`@php-skir/generator-core`](https://github.com/php-skir/generator-core) at generation time for schema normalization, PHP naming and imports, shared enum and RPC rendering, server manifests, and Composer configuration. This package remains the framework-agnostic adapter: it owns the standard PHP data-object representation and conversions, while `php-skir/runtime` remains the Composer dependency used by the generated PHP at runtime.
14
14
 
@@ -130,9 +130,19 @@ The generator emits readonly PHP classes for Skir structs and enum wrapper class
130
130
 
131
131
  - `skirType()` for runtime type descriptors.
132
132
  - `toArray()` and `fromArray()` for PHP array conversion.
133
- - `toDenseJson()` and `fromDenseJson()` for dense JSON payloads.
133
+ - `toDenseJson()` and `fromDenseJson()` for dense JSON payloads. `fromDenseJson()` also accepts readable JSON, so there is no separate `fromReadableJson()`.
134
+ - `toReadableJson()` for readable JSON payloads.
135
+ - `toBytes()` and `fromBytes()` for Skir binary payloads.
134
136
  - `toSkirValue()` and `fromSkirValue()` on generated enum classes.
135
137
 
138
+ ```php
139
+ $json = $user->toReadableJson();
140
+ $user = User::fromDenseJson($json);
141
+
142
+ $bytes = $user->toBytes();
143
+ $user = User::fromBytes($bytes);
144
+ ```
145
+
136
146
  SkirRPC methods are emitted in `SkirMethods.php` as `MethodDescriptor` instances. The generator also emits a module-scoped method enum such as `AdminSkirMethod.php` for typed server routing.
137
147
 
138
148
  When a module defines SkirRPC methods, the generator emits `SkirRpcClient.php`. It wraps `Skir\Client\SkirClient` and exposes typed methods:
package/dist/target.d.ts CHANGED
@@ -14,4 +14,7 @@ export declare class StandardPhpTarget implements PhpTargetAdapter {
14
14
  private renderFromArray;
15
15
  private renderToDenseJson;
16
16
  private renderFromDenseJson;
17
+ private renderToReadableJson;
18
+ private renderToBytes;
19
+ private renderFromBytes;
17
20
  }
package/dist/target.js CHANGED
@@ -10,8 +10,10 @@ export class StandardPhpTarget {
10
10
  throw new Error(`Cannot render non-struct record ${record.identity} as a struct.`);
11
11
  }
12
12
  const className = classNameForRecord(record, context);
13
+ const binaryFormat = importClass(context.imports, "Skir\\Runtime\\BinaryFormat");
13
14
  const denseJson = importClass(context.imports, "Skir\\Runtime\\DenseJson");
14
15
  const fieldClass = importClass(context.imports, "Skir\\Runtime\\Field");
16
+ const readableJson = importClass(context.imports, "Skir\\Runtime\\ReadableJson");
15
17
  const typeClass = importClass(context.imports, "Skir\\Runtime\\Type");
16
18
  const fields = record.fields.filter(isStructField);
17
19
  const constructor = this.renderConstructor(fields, context);
@@ -22,6 +24,9 @@ export class StandardPhpTarget {
22
24
  this.renderFromArray(className, fields, context),
23
25
  this.renderToDenseJson(denseJson),
24
26
  this.renderFromDenseJson(className, denseJson),
27
+ this.renderToReadableJson(readableJson),
28
+ this.renderToBytes(binaryFormat),
29
+ this.renderFromBytes(className, binaryFormat),
25
30
  ].filter((member) => member !== null);
26
31
  const body = [
27
32
  `final readonly class ${className}`,
@@ -189,6 +194,30 @@ export class StandardPhpTarget {
189
194
  "}",
190
195
  ].join("\n");
191
196
  }
197
+ renderToReadableJson(readableJson) {
198
+ return [
199
+ "public function toReadableJson(): string",
200
+ "{",
201
+ ` return ${readableJson}::toJson(self::skirType(), $this->toArray());`,
202
+ "}",
203
+ ].join("\n");
204
+ }
205
+ renderToBytes(binaryFormat) {
206
+ return [
207
+ "public function toBytes(): string",
208
+ "{",
209
+ ` return ${binaryFormat}::encode(self::skirType(), $this->toArray());`,
210
+ "}",
211
+ ].join("\n");
212
+ }
213
+ renderFromBytes(className, binaryFormat) {
214
+ return [
215
+ `public static function fromBytes(string $bytes): ${className}`,
216
+ "{",
217
+ ` return self::fromArray(${binaryFormat}::decode(self::skirType(), $bytes));`,
218
+ "}",
219
+ ].join("\n");
220
+ }
192
221
  }
193
222
  function nullablePhpType(type) {
194
223
  const unionMembers = type.split("|");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skir-php-generator",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Skir code generator for standard PHP data objects.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -45,7 +45,7 @@
45
45
  "typecheck": "tsc -p tsconfig.json --noEmit"
46
46
  },
47
47
  "dependencies": {
48
- "@php-skir/generator-core": "^0.1.0",
48
+ "@php-skir/generator-core": "^0.2.0",
49
49
  "jsonc-parser": "^3.3.1",
50
50
  "skir-internal": "^0.2.21",
51
51
  "yaml": "^2.9.0",