Skip to content
Merged
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
45 changes: 45 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private function __construct(
public bool $addSymfonyExcludeAttribute = false,
public bool $indexByDirective = false,
public bool $addUnknownCaseToEnums = false,
public bool $dumpEnumIsMethods = false,
) {}

public static function create(
Expand Down Expand Up @@ -86,6 +87,7 @@ public function enableDumpMethods() : self
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -114,6 +116,7 @@ public function enableDumpOrThrows() : self
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -142,6 +145,7 @@ public function enableDumpDefinition() : self
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -170,6 +174,7 @@ public function enableUseNodeNameForEdgeNodes() : self
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -198,6 +203,7 @@ public function enableUseConnectionNameForConnections() : self
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -226,6 +232,7 @@ public function enableUseEdgeNameForEdges() : self
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -254,6 +261,7 @@ public function enableAddNodesOnConnections() : self
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -310,6 +318,7 @@ public function enableIndexByDirective() : self
$this->addSymfonyExcludeAttribute,
true,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -338,6 +347,36 @@ public function enableAddUnknownCaseToEnums() : self
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
true,
$this->dumpEnumIsMethods,
);
}

public function dumpEnumIsMethods() : self
{
return new self(
$this->schema,
$this->projectDir,
$this->queriesDir,
$this->outputDir,
$this->namespace,
$this->client,
$this->dumpMethods,
$this->dumpOrThrows,
$this->dumpDefinition,
$this->useNodeNameForEdgeNodes,
$this->scalars,
$this->inputObjectTypes,
$this->objectTypes,
$this->enumTypes,
$this->ignoreTypes,
$this->typeInitializers,
$this->useConnectionNameForConnections,
$this->useEdgeNameForEdges,
$this->addNodesOnConnections,
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
true,
);
}

Expand Down Expand Up @@ -369,6 +408,7 @@ public function withScalar(string $name, Type $type, ?Type $payloadType = null)
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -400,6 +440,7 @@ public function withInputObjectType(string $name, Type $type) : self
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -431,6 +472,7 @@ public function withObjectType(string $name, Type $payloadShape, Type $payloadTy
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -462,6 +504,7 @@ public function withEnumType(string $name, Type $type) : self
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -493,6 +536,7 @@ public function withIgnoreType(string $type) : self
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}

Expand Down Expand Up @@ -524,6 +568,7 @@ public function withTypeInitializer(TypeInitializer\TypeInitializer $typeInitial
$this->addSymfonyExcludeAttribute,
$this->indexByDirective,
$this->addUnknownCaseToEnums,
$this->dumpEnumIsMethods,
);
}
}
29 changes: 16 additions & 13 deletions src/Generator/EnumTypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ public function generate(EnumClassPlan $plan) : string
yield 'case Unknown__ = \'unknown__\';';
}

if ($this->config->dumpMethods) {
if ($this->config->dumpMethods || $this->config->dumpEnumIsMethods) {
$numberOfValues = count($plan->values);
foreach ($plan->values as $name => $value) {
foreach ($plan->values as $value) {
yield '';
yield sprintf('public function is%s() : bool', u($value['value'])->lower()->pascal()->toString());
yield '{';
yield $generator->indent(function () use ($generator, $numberOfValues, $value) {
if ($numberOfValues === 1) {
// Only add the phpstan-ignore if there's exactly one value and no Unknown__ case
if ($numberOfValues === 1 && ! $this->config->addUnknownCaseToEnums) {
yield from $generator->comment('@phpstan-ignore identical.alwaysTrue');
}

Expand All @@ -62,16 +63,18 @@ public function generate(EnumClassPlan $plan) : string
});
yield '}';

yield '';
yield sprintf(
'public static function create%s() : self',
u($value['value'])->lower()->pascal()->toString(),
);
yield '{';
yield $generator->indent(function () use ($value) {
yield sprintf('return self::%s;', u($value['value'])->lower()->pascal()->toString());
});
yield '}';
if ($this->config->dumpMethods) {
yield '';
yield sprintf(
'public static function create%s() : self',
u($value['value'])->lower()->pascal()->toString(),
);
yield '{';
yield $generator->indent(function () use ($value) {
yield sprintf('return self::%s;', u($value['value'])->lower()->pascal()->toString());
});
yield '}';
}
}
}
});
Expand Down
14 changes: 14 additions & 0 deletions tests/EnumDumpIsMethods/CustomPriority.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Ruudk\GraphQLCodeGenerator\EnumDumpIsMethods;

enum CustomPriority : string
{
case Low = 'LOW';
case Medium = 'MEDIUM';
case High = 'HIGH';
case Urgent = 'URGENT';
case Unknown = 'UNKNOWN';
}
66 changes: 66 additions & 0 deletions tests/EnumDumpIsMethods/EnumDumpIsMethodsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Ruudk\GraphQLCodeGenerator\EnumDumpIsMethods;

use Override;
use Ruudk\GraphQLCodeGenerator\Config;
use Ruudk\GraphQLCodeGenerator\EnumDumpIsMethods\Generated\Enum\Role;
use Ruudk\GraphQLCodeGenerator\EnumDumpIsMethods\Generated\Enum\State;
use Ruudk\GraphQLCodeGenerator\EnumDumpIsMethods\Generated\Query\TestQuery;
use Ruudk\GraphQLCodeGenerator\GraphQLTestCase;
use Symfony\Component\TypeInfo\Type;

final class EnumDumpIsMethodsTest extends GraphQLTestCase
{
#[Override]
public function getConfig() : Config
{
return parent::getConfig()
->enableAddUnknownCaseToEnums()
->dumpEnumIsMethods()
->withEnumType('Priority', Type::enum(CustomPriority::class));
}

public function testGenerate() : void
{
$this->assertActualMatchesExpected();
}

public function testQuery() : void
{
$result = new TestQuery($this->getClient([
'data' => [
'accountStatus' => 'ACTIVE',
'role' => 'ADMIN',
'otherRole' => 'SUPERADMIN',
'priority' => 'HIGH',
],
]))->execute();

self::assertSame(State::Active, $result->accountStatus);
self::assertSame(Role::Admin, $result->role);
self::assertSame(Role::Unknown__, $result->otherRole);
self::assertSame(CustomPriority::High, $result->priority);

// Test that the is methods exist and work correctly
self::assertTrue($result->accountStatus->isActive());
self::assertTrue($result->role->isAdmin());
self::assertFalse($result->role->isUser());
}

public function testCustomEnumMapping() : void
{
$result = new TestQuery($this->getClient([
'data' => [
'accountStatus' => 'ACTIVE',
'role' => 'USER',
'otherRole' => 'USER',
'priority' => 'URGENT',
],
]))->execute();

self::assertSame(CustomPriority::Urgent, $result->priority);
}
}
41 changes: 41 additions & 0 deletions tests/EnumDumpIsMethods/Generated/Enum/Priority.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Ruudk\GraphQLCodeGenerator\EnumDumpIsMethods\Generated\Enum;

// This file was automatically generated and should not be edited.

/**
* @api
*/
enum Priority: string
{
case Low = 'LOW';
case Medium = 'MEDIUM';
case High = 'HIGH';
case Urgent = 'URGENT';

// When the server returns an unknown enum value, this is the value that will be used.
case Unknown__ = 'unknown__';

public function isLow() : bool
{
return $this === self::Low;
}

public function isMedium() : bool
{
return $this === self::Medium;
}

public function isHigh() : bool
{
return $this === self::High;
}

public function isUrgent() : bool
{
return $this === self::Urgent;
}
}
29 changes: 29 additions & 0 deletions tests/EnumDumpIsMethods/Generated/Enum/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Ruudk\GraphQLCodeGenerator\EnumDumpIsMethods\Generated\Enum;

// This file was automatically generated and should not be edited.

/**
* @api
*/
enum Role: string
{
case User = 'USER';
case Admin = 'ADMIN';

// When the server returns an unknown enum value, this is the value that will be used.
case Unknown__ = 'unknown__';

public function isUser() : bool
{
return $this === self::User;
}

public function isAdmin() : bool
{
return $this === self::Admin;
}
}
23 changes: 23 additions & 0 deletions tests/EnumDumpIsMethods/Generated/Enum/State.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Ruudk\GraphQLCodeGenerator\EnumDumpIsMethods\Generated\Enum;

// This file was automatically generated and should not be edited.

/**
* @api
*/
enum State: string
{
case Active = 'ACTIVE';

// When the server returns an unknown enum value, this is the value that will be used.
case Unknown__ = 'unknown__';

public function isActive() : bool
{
return $this === self::Active;
}
}
Loading