Skip to content

Commit

Permalink
explain conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Nov 30, 2023
1 parent c77810b commit 182e3e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 7 additions & 2 deletions docs/schema-definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ final class Types
return self::$types[$typeName];
}

// For every type, this class must define a method with the same name
// but the first letter is in lower case.
$methodName = match ($typeName) {
'ID' => 'id',
default => lcfirst($typeName),
Expand All @@ -190,8 +192,11 @@ final class Types
/** @return Type&NamedType */
private static function byClassName(string $className): Type
{
$parts = \explode('\\', $className);
$typeName = \preg_replace('~Type$~', '', $parts[\count($parts) - 1]);
$classNameParts = explode('\\', $className);
$baseClassName = end($classNameParts);
// All type classes must use the suffix Type.
// This prevents name collisions between types and PHP keywords.
$typeName = preg_replace('~Type$~', '', $baseClassName);

// Type loading is very similar to PHP class loading, but keep in mind
// that the **typeLoader** must always return the same instance of a type.
Expand Down
10 changes: 7 additions & 3 deletions examples/01-blog/Blog/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public static function load(string $typeName): Type
return self::$types[$typeName];
}

// For every type, this class must define a method with the same name
// but the first letter is in lower case.
switch ($typeName) {
case 'ID':
$methodName = 'id';
Expand All @@ -66,9 +68,11 @@ public static function load(string $typeName): Type
*/
private static function byClassName(string $className): Type
{
$parts = \explode('\\', $className);

$typeName = \preg_replace('~Type$~', '', $parts[\count($parts) - 1]);
$classNameParts = \explode('\\', $className);
$baseClassName = end($classNameParts);
// All type classes must use the suffix Type.
// This prevents name collisions between types and PHP keywords.
$typeName = \preg_replace('~Type$~', '', $baseClassName);
assert(is_string($typeName), 'regex is statically known to be correct');

// Type loading is very similar to PHP class loading, but keep in mind
Expand Down

0 comments on commit 182e3e7

Please sign in to comment.