Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Replace _extends with unions #372

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 0 additions & 113 deletions src/Schema/DataObject/InheritanceChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,6 @@ class InheritanceChain
*/
private $inst;

/**
* @var string
* @config
*/
private static $field_name = '_extend';

/**
* @var callable
* @config
*/
private static $descendant_typename_creator = [ self::class, 'createDescendantTypename' ];

/**
* @var callable
* @config
*/
private static $subtype_name_creator = [ self::class, 'createSubtypeName' ];

/**
* @var array
*/
private $descendantTypeResult;

/**
* InheritanceChain constructor.
* @param string $dataObjectClass
Expand All @@ -73,14 +50,6 @@ public function __construct(string $dataObjectClass)
$this->inst = DataObject::singleton($this->dataObjectClass);
}

/**
* @return string
*/
public static function getName(): string
{
return static::config()->get('field_name');
}

/**
* @return array
*/
Expand Down Expand Up @@ -149,86 +118,4 @@ public function getBaseClass(): string
{
return $this->inst->baseClass();
}

/**
* @param SchemaConfig $schemaContext
* @return array|null
* @throws ReflectionException
* @throws SchemaBuilderException
*/
public function getExtensionType(SchemaConfig $schemaContext): ?array
{
if ($this->descendantTypeResult) {
return $this->descendantTypeResult;
}
if (empty($this->getDescendantModels())) {
return null;
}
$typeName = call_user_func_array(
$this->config()->get('descendant_typename_creator'),
[$this->inst, $schemaContext]
);

$nameCreator = $this->config()->get('subtype_name_creator');

$subtypes = [];
foreach ($this->getDescendantModels() as $className) {
$model = $schemaContext->createModel($className);
if (!$model) {
continue;
}
$modelType = ModelType::create($model);
$originalName = $modelType->getName();
$newName = call_user_func_array($nameCreator, [$originalName]);
$modelType->setName($newName);
$subtypes[$originalName] = $modelType;
}

$descendantType = Type::create($typeName, [
'fieldResolver' => [static::class, 'resolveExtensionType'],
]);

$this->descendantTypeResult = [$descendantType, $subtypes];

return $this->descendantTypeResult;
}



/**
* @param DataObject $dataObject
* @param SchemaConfig $schemaContext
* @return string
* @throws SchemaBuilderException
*/
public static function createDescendantTypename(DataObject $dataObject, SchemaConfig $schemaContext): string
{
$model = $schemaContext->createModel(get_class($dataObject));
Schema::invariant(
$model,
'No model defined for %s. Cannot create inheritance typename',
get_class($dataObject)
);

return $model->getTypeName() . 'Descendants';
}

/**
* @param string $modelTypeName
* @return string
*/
public static function createSubtypeName(string $modelTypeName): string
{
return $modelTypeName . 'ExtensionType';
}

/**
* Noop, because __extends is just structure
* @param $obj
* @return DataObject|null
*/
public static function resolveExtensionType($obj): ?DataObject
{
return $obj;
}
}
Loading