-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
204 changed files
with
8,857 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
generators/php/codegen/src/asIs/Json/TraitTest.Template.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace <%= namespace%>; | ||
|
||
use <%= coreNamespace%>\Json\SerializableType; | ||
use <%= coreNamespace%>\Json\JsonProperty; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
trait IntegerPropertyTrait | ||
{ | ||
/** | ||
* @var int $integerProperty | ||
*/ | ||
#[JsonProperty('integer_property')] | ||
public int $integerProperty; | ||
} | ||
|
||
class TypeWithTrait extends SerializableType | ||
{ | ||
use IntegerPropertyTrait; | ||
|
||
/** | ||
* @var string $stringProperty | ||
*/ | ||
#[JsonProperty('string_property')] | ||
public string $stringProperty; | ||
|
||
/** | ||
* @param array{ | ||
* integerProperty: int, | ||
* stringProperty: string, | ||
* } $values | ||
*/ | ||
public function __construct(array $values) | ||
{ | ||
$this->integerProperty = $values['integerProperty']; | ||
$this->stringProperty = $values['stringProperty']; | ||
} | ||
} | ||
|
||
class TraitTest extends TestCase | ||
{ | ||
public function testTraitPropertyAndString(): void | ||
{ | ||
$data = [ | ||
'integer_property' => 42, | ||
'string_property' => 'Hello, World!', | ||
]; | ||
|
||
$json = json_encode($data, JSON_THROW_ON_ERROR); | ||
|
||
$object = TypeWithTrait::fromJson($json); | ||
|
||
$serializedJson = $object->toJson(); | ||
|
||
$this->assertJsonStringEqualsJsonString($json, $serializedJson, 'Serialized JSON does not match original JSON for ScalarTypesTestWithTrait.'); | ||
|
||
$this->assertEquals(42, $object->integerProperty, 'integer_property should be 42.'); | ||
$this->assertEquals('Hello, World!', $object->stringProperty, 'string_property should be "Hello, World!".'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { AstNode } from "./core/AstNode"; | ||
import { Writer } from "./core/Writer"; | ||
import { Field } from "./Field"; | ||
import { Method } from "./Method"; | ||
import { Comment } from "./Comment"; | ||
import { orderByAccess } from "./utils/orderByAccess"; | ||
import { ClassReference } from "./ClassReference"; | ||
|
||
export declare namespace Trait { | ||
interface Args { | ||
/* The name of the PHP trait */ | ||
name: string; | ||
/* The namespace of the PHP trait */ | ||
namespace: string; | ||
/* Docs associated with the trait */ | ||
docs?: string; | ||
/* The traits that this trait uses, if any */ | ||
traits?: ClassReference[]; | ||
} | ||
} | ||
|
||
export class Trait extends AstNode { | ||
public readonly name: string; | ||
public readonly namespace: string; | ||
public readonly docs: string | undefined; | ||
public readonly traits: ClassReference[]; | ||
|
||
public readonly fields: Field[] = []; | ||
public readonly methods: Method[] = []; | ||
|
||
constructor({ name, namespace, docs, traits }: Trait.Args) { | ||
super(); | ||
this.name = name; | ||
this.namespace = namespace; | ||
this.docs = docs; | ||
this.traits = traits ?? []; | ||
} | ||
|
||
public addField(field: Field): void { | ||
this.fields.push(field); | ||
} | ||
|
||
public addMethod(method: Method): void { | ||
this.methods.push(method); | ||
} | ||
|
||
public write(writer: Writer): void { | ||
this.writeComment(writer); | ||
writer.write(`trait ${this.name} `); | ||
writer.newLine(); | ||
writer.writeLine("{"); | ||
writer.indent(); | ||
|
||
if (this.traits.length > 0) { | ||
writer.write("use "); | ||
this.traits.forEach((trait, index) => { | ||
if (index > 0) { | ||
writer.write(","); | ||
} | ||
writer.writeNode(trait); | ||
}); | ||
writer.writeTextStatement(""); | ||
writer.newLine(); | ||
} | ||
|
||
this.writeFields({ writer, fields: orderByAccess(this.fields) }); | ||
this.writeMethods({ writer, methods: orderByAccess(this.methods) }); | ||
|
||
writer.dedent(); | ||
writer.writeLine("}"); | ||
return; | ||
} | ||
|
||
private writeComment(writer: Writer): void { | ||
if (this.docs == null) { | ||
return undefined; | ||
} | ||
const comment = new Comment({ docs: this.docs }); | ||
comment.write(writer); | ||
} | ||
|
||
private writeFields({ writer, fields }: { writer: Writer; fields: Field[] }): void { | ||
fields.forEach((field, index) => { | ||
if (index > 0) { | ||
writer.newLine(); | ||
} | ||
field.write(writer); | ||
writer.writeNewLineIfLastLineNot(); | ||
}); | ||
} | ||
|
||
private writeMethods({ writer, methods }: { writer: Writer; methods: Method[] }): void { | ||
methods.forEach((method, index) => { | ||
if (index > 0) { | ||
writer.newLine(); | ||
} | ||
method.write(writer); | ||
writer.writeNewLineIfLastLineNot(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const TRAITS_DIRECTORY = "Traits"; |
Oops, something went wrong.