@@ -111,8 +111,15 @@ class User extends ClassStructure
111111 */
112112 public static function setUpProperties($properties, Schema $ownerSchema)
113113 {
114+ // You can add custom meta to your schema
115+ $dbTable = new DbTable;
116+ $dbTable->tableName = 'users';
117+ $ownerSchema->addMeta($dbTable);
118+
114119 // Setup property schemas
115120 $properties->id = Schema::integer();
121+ $properties->id->addMeta(new DbId($dbTable)); // You can add meta to property.
122+
116123 $properties->name = Schema::string();
117124
118125 // You can embed structures to main level with nested schemas
@@ -130,7 +137,6 @@ class User extends ClassStructure
130137 }
131138}
132139
133-
134140class UserInfo extends ClassStructure {
135141 public $firstName;
136142 public $lastName;
@@ -149,9 +155,14 @@ class UserInfo extends ClassStructure {
149155}
150156
151157
152- class Order extends ClassStructure
158+ class Order implements ClassStructureContract
153159{
160+ use ClassStructureTrait; // You can use trait if you can't/don't want to extend ClassStructure
161+
162+ const FANCY_MAPPING = 'fAnCy'; // You can create additional mapping namespace
163+
154164 public $id;
165+ public $userId;
155166 public $dateTime;
156167 public $price;
157168
@@ -161,12 +172,27 @@ class Order extends ClassStructure
161172 */
162173 public static function setUpProperties($properties, Schema $ownerSchema)
163174 {
175+ // Add some meta data to your schema
176+ $dbMeta = new DbTable();
177+ $dbMeta->tableName = 'orders';
178+ $ownerSchema->addMeta($dbMeta);
179+
180+ // Define properties
164181 $properties->id = Schema::integer();
165- $properties->dateTime = Schema::string()->meta(new FieldName('date_time'));
182+ $properties->userId = User::properties()->id; // referencing property of another schema keeps meta
183+ $properties->dateTime = Schema::string();
166184 $properties->dateTime->format = Schema::FORMAT_DATE_TIME;
167185 $properties->price = Schema::number();
168186
169187 $ownerSchema->required[] = self::names()->id;
188+
189+ // Define default mapping if any
190+ $ownerSchema->addPropertyMapping('date_time', Order::names()->dateTime);
191+
192+ // Define additional mapping
193+ $ownerSchema->addPropertyMapping('DaTe_TiMe', Order::names()->dateTime, self::FANCY_MAPPING);
194+ $ownerSchema->addPropertyMapping('Id', Order::names()->id, self::FANCY_MAPPING);
195+ $ownerSchema->addPropertyMapping('PrIcE', Order::names()->price, self::FANCY_MAPPING);
170196 }
171197}
172198```
@@ -261,26 +287,49 @@ $this->assertSame(2.66, $order->price);
261287#### Keys mapping
262288
263289If property names of PHP objects should be different from raw data you
264- can apply ` \Swaggest\JsonSchema\PreProcessor\NameMapper ` during processing.
265- It takes ` Swaggest\JsonSchema\Meta\FieldName ` as source of raw name.
290+ can call ` ->addPropertyMapping ` on owner schema.
266291
267292``` php
268- $properties->dateTime = Schema::string()->meta(new FieldName('date_time'));
293+ // Define default mapping if any
294+ $ownerSchema->addPropertyMapping('date_time', Order::names()->dateTime);
295+
296+ // Define additional mapping
297+ $ownerSchema->addPropertyMapping('DaTe_TiMe', Order::names()->dateTime, self::FANCY_MAPPING);
298+ $ownerSchema->addPropertyMapping('Id', Order::names()->id, self::FANCY_MAPPING);
299+ $ownerSchema->addPropertyMapping('PrIcE', Order::names()->price, self::FANCY_MAPPING);
269300```
270301
302+ It will affect data mapping:
271303``` php
272- $mapper = new NameMapper();
273- $options = new Context();
274- $options->dataPreProcessor = $mapper;
275-
276304$order = new Order();
277305$order->id = 1;
278306$order->dateTime = '2015-10-28T07:28:00Z';
279- $exported = Order::export($order, $options);
307+ $order->price = 2.2;
308+ $exported = Order::export($order);
280309$json = <<<JSON
281310{
282311 " id" : 1,
283- " date_time" : " 2015-10-28T07:28:00Z"
312+ " date_time" : " 2015-10-28T07:28:00Z" ,
313+ " price" : 2.2
314+ }
315+ JSON;
316+ $this- >assertSame($json, json_encode($exported, JSON_PRETTY_PRINT));
317+
318+ $imported = Order::import(json_decode($json));
319+ $this->assertSame('2015-10-28T07:28:00Z', $imported->dateTime);
320+ ```
321+
322+ You can have multiple mapping namespaces, controlling with ` mapping ` property of ` Context `
323+ ``` php
324+ $options = new Context();
325+ $options->mapping = Order::FANCY_MAPPING;
326+
327+ $exported = Order::export($order, $options);
328+ $json = <<<JSON
329+ {
330+ " Id" : 1,
331+ " DaTe_TiMe" : " 2015-10-28T07:28:00Z" ,
332+ " PrIcE" : 2.2
284333}
285334JSON;
286335$this- >assertSame($json, json_encode($exported, JSON_PRETTY_PRINT));
@@ -297,16 +346,16 @@ You can create your own pre-processor implementing `Swaggest\JsonSchema\DataPreP
297346
298347You can store it.
299348``` php
300- $schema = new Schema ();
301- // Setting meta
302- $schema->meta(new FieldName('my-value') );
349+ $dbMeta = new DbTable ();
350+ $dbMeta->tableName = 'orders';
351+ $ownerSchema->addMeta($dbMeta );
303352```
304353
305354And get back.
306355``` php
307356// Retrieving meta
308- $myMeta = FieldName ::get($ schema);
309- $this->assertSame('my-value ', $myMeta->name );
357+ $dbTable = DbTable ::get(Order:: schema() );
358+ $this->assertSame('orders ', $dbTable->tableName );
310359```
311360
312361
0 commit comments