Skip to content

Commit 65625f0

Browse files
committed
Refactor
1 parent 4f6d728 commit 65625f0

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/lib/FakerStubResolver.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use cebe\yii2openapi\lib\items\JunctionSchemas;
2323
use cebe\yii2openapi\lib\openapi\ComponentSchema;
2424
use cebe\yii2openapi\lib\openapi\PropertySchema;
25+
use stdClass;
2526
use Symfony\Component\VarExporter\Exception\ExceptionInterface;
2627
use Symfony\Component\VarExporter\VarExporter;
2728
use Yii;
@@ -58,6 +59,7 @@ public function __construct(Attribute $attribute, PropertySchema $property, ?Con
5859
* @throws UnresolvableReferenceException
5960
* @throws InvalidDefinitionException
6061
* @throws ExceptionInterface
62+
* @throws IOException
6163
*/
6264
public function resolve(): ?string
6365
{
@@ -285,18 +287,18 @@ private function fakeForArray(SpecObjectInterface $property, int $count = 4): st
285287
}
286288

287289
if ($items instanceof Reference) {
288-
$aFakerForRef = $this->aElementFaker($items);
290+
$aFakerForRef = $this->aElementFaker($items, $this->attribute->columnName);
289291
return $this->wrapInArray($aFakerForRef, $uniqueItems, $count);
290292
}
291293
if (!empty($items->oneOf)) {
292-
return $this->handleOneOf($items, $count);
294+
return $this->wrapInArray($this->handleOneOf($items, $count), $uniqueItems, $count, true);
293295
}
294296

295297
$type = $items->type;
296298
if ($type === null) {
297299
return $this->arbitraryArray();
298300
}
299-
$aFaker = $this->aElementFaker($this->property->getProperty());
301+
$aFaker = $this->aElementFaker($this->property->getProperty(), $this->attribute->columnName);
300302
if (in_array($type, ['string', 'number', 'integer', 'boolean', 'array'])) {
301303
return $this->wrapInArray($aFaker, $uniqueItems, $count);
302304
}
@@ -326,20 +328,15 @@ public function fakeForObject(SpecObjectInterface $items): string
326328
}
327329

328330
$props = '[' . PHP_EOL;
329-
$cs = new ComponentSchema($items, 'unnamed');
330-
$dbModels = (new AttributeResolver('unnamed', $cs, new JunctionSchemas([])))->resolve();
331331

332332
foreach ($items->properties as $name => $prop) {
333333
/** @var SpecObjectInterface $prop */
334334

335335
if ($prop->properties) { // nested object
336336
$result = $this->{__FUNCTION__}($prop);
337337
} else {
338-
$ps = new PropertySchema($prop, $name, $cs);
339-
$attr = $dbModels->attributes[$name];
340-
$result = (string)((new static($attr, $ps, $this->config))->resolve());
338+
$result = $this->aElementFaker(['items' => $prop->getSerializableData()], $name);
341339
}
342-
343340
$props .= '\'' . $name . '\' => ' . $result . ',' . PHP_EOL;
344341
}
345342

@@ -363,24 +360,24 @@ public function fakeForObject(SpecObjectInterface $items): string
363360
*/
364361
public function handleOneOf(SpecObjectInterface $items, int $count): string
365362
{
366-
$result = 'array_map(function () use ($faker, $uniqueFaker) {';
363+
$result = '';
367364
foreach ($items->oneOf as $key => $aDataType) {
368365
/** @var Schema|Reference $aDataType */
369366

370367
$inp = $aDataType instanceof Reference ? $aDataType : ['items' => $aDataType->getSerializableData()];
371-
$aFaker = $this->aElementFaker($inp);
368+
$aFaker = $this->aElementFaker($inp, $this->attribute->columnName);
372369
$result .= '$dataType' . $key . ' = ' . $aFaker . ';';
373370
}
374371
$ct = count($items->oneOf) - 1;
375-
$result .= 'return ${"dataType".rand(0, ' . $ct . ')};';
376-
$result .= '}, range(1, ' . $count . '))';
372+
$result .= 'return ${"dataType".rand(0, ' . $ct . ')}';
377373
return $result;
378374
}
379375

380-
public function wrapInArray(string $aFaker, bool $uniqueItems, int $count): string
376+
public function wrapInArray(string $aFaker, bool $uniqueItems, int $count, bool $oneOf = false): string
381377
{
378+
$ret = $oneOf ? '' : 'return ';
382379
return 'array_map(function () use ($faker, $uniqueFaker) {
383-
return ' . ($uniqueItems ? str_replace('$faker->', '$uniqueFaker->', $aFaker) : $aFaker) . ';
380+
' . $ret . ($uniqueItems ? str_replace('$faker->', '$uniqueFaker->', $aFaker) : $aFaker) . ';
384381
}, range(1, ' . $count . '))';
385382
}
386383

@@ -392,17 +389,18 @@ public function arbitraryArray(): string
392389
/**
393390
* This method is only for `fakeForArray()` or methods only used inside `fakeForArray()`. If needed to use outside `fakeForArray()` context then some changes might be required.
394391
* Also see OpenAPI extension `x-no-relation` in README.md
395-
* @param $data array|\stdClass|SpecObjectInterface
392+
* @param $data array|stdClass|SpecObjectInterface
393+
* @param string|null $columnName
396394
* @return string|null
397395
* @throws ExceptionInterface
396+
* @throws IOException
398397
* @throws InvalidConfigException
399398
* @throws InvalidDefinitionException
400399
* @throws TypeErrorException
401400
* @throws UnresolvableReferenceException
402-
* @throws IOException
403401
* @internal
404402
*/
405-
public function aElementFaker($data): ?string
403+
public function aElementFaker($data, ?string $columnName = null): ?string
406404
{
407405
if ($data instanceof Reference) {
408406
$class = str_replace('#/components/schemas/', '', $data->getReference());
@@ -412,27 +410,29 @@ public function aElementFaker($data): ?string
412410

413411
$inp = $data instanceof SpecObjectInterface ? $data->getSerializableData() : $data;
414412
$aElementData = Json::decode(Json::encode($inp));
413+
$columnName = $columnName ?? 'unnamedProp';
415414
$compoSchemaData = [
416415
'properties' => [
417-
'unnamedProp' => $aElementData['items']
416+
$columnName => $aElementData['items']
418417
]
419418
];
420419

421420
// This condition is only for properties with type = array
422421
// If you intend to use this method from out of `fakeForArray()` context then below condition should be changed depending on your use case
423422
// Also see OpenAPI extension `x-no-relation` in README.md
424-
if (!empty($compoSchemaData['properties']['unnamedProp']['items']['$ref'])) {
425-
$compoSchemaData['properties']['unnamedProp']['x-no-relation'] = true;
423+
if (!empty($compoSchemaData['properties'][$columnName]['items']['$ref'])) {
424+
$compoSchemaData['properties'][$columnName]['x-no-relation'] = true;
426425
}
427426

428427
$schema = new Schema($compoSchemaData);
429-
$cs = new ComponentSchema($schema, 'UnnamedCompo');
428+
$compo = 'UnnamedCompo';
429+
$cs = new ComponentSchema($schema, $compo);
430430
if ($this->config) {
431431
$rc = new ReferenceContext($this->config->getOpenApi(), Yii::getAlias($this->config->openApiPath));
432432
$schema->setReferenceContext($rc);
433433
}
434-
$dbModels = (new AttributeResolver('UnnamedCompo', $cs, new JunctionSchemas([]), $this->config))->resolve();
434+
$dbModels = (new AttributeResolver($compo, $cs, new JunctionSchemas([]), $this->config))->resolve();
435435

436-
return (new static($dbModels->attributes['unnamedProp'], $cs->getProperty('unnamedProp'), $this->config))->resolve();
436+
return (new static($dbModels->attributes[$columnName], $cs->getProperty($columnName), $this->config))->resolve();
437437
}
438438
}

0 commit comments

Comments
 (0)