Skip to content

Commit 7bece74

Browse files
committed
Adding ability to implement an interface in DAO and beans
1 parent 2acdb0c commit 7bece74

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ $db->table('posts')
4141
->protectedSetter() // The Post.setUser() method is protected
4242
->protectedOneToMany() // The User.getPosts() method is protected
4343

44+
// Customize implemented interfaces
45+
$db->table('posts')
46+
->implementsInterface('App\\PostInterface') // The generated bean will implement interface App\\PostInterface
47+
->implementsInterfaceOnDao('App\\PostDaoInterface'); // The generated DAO will implement interface App\\PostDaoInterface
48+
4449
// The "posts" table will generate a GraphQL type (i.e. the bean will be annotated with the GraphQLite @Type annotation).
4550
$posts = $db->table('posts')->graphqlType();
4651

src/TdbmFluidTable.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function uuid(string $version = 'v4'): TdbmFluidColumnOptions
7777
return $this->column('uuid')->guid();
7878
}
7979

80-
public function timestamps(): TdbmFluidTable
80+
public function timestamps(): self
8181
{
8282
$this->fluidTable->timestamps();
8383
return $this;
@@ -86,7 +86,7 @@ public function timestamps(): TdbmFluidTable
8686
/**
8787
* @throws FluidSchemaException
8888
*/
89-
public function extends(string $tableName): TdbmFluidTable
89+
public function extends(string $tableName): self
9090
{
9191
$this->fluidTable->extends($tableName);
9292
return $this;
@@ -95,7 +95,7 @@ public function extends(string $tableName): TdbmFluidTable
9595
/**
9696
* Adds a "Bean" annotation to the table.
9797
*/
98-
public function customBeanName(string $beanName): TdbmFluidTable
98+
public function customBeanName(string $beanName): self
9999
{
100100
$this->addAnnotation('Bean', ['name'=>$beanName]);
101101
return $this;
@@ -104,12 +104,36 @@ public function customBeanName(string $beanName): TdbmFluidTable
104104
/**
105105
* Adds a "Type" annotation.
106106
*/
107-
public function graphqlType(): TdbmFluidTable
107+
public function graphqlType(): self
108108
{
109109
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Type');
110110
return $this;
111111
}
112112

113+
/**
114+
* Makes the generated bean implement the interface $interfaceName
115+
*
116+
* @param string $interfaceName The fully qualified name of the PHP interface to implement.
117+
* @return TdbmFluidTable
118+
*/
119+
public function implementsInterface(string $interfaceName): self
120+
{
121+
$this->addAnnotation('AddInterface', ['name' => $interfaceName], false);
122+
return $this;
123+
}
124+
125+
/**
126+
* Makes the generated DAO implement the interface $interfaceName
127+
*
128+
* @param string $interfaceName The fully qualified name of the PHP interface to implement.
129+
* @return TdbmFluidTable
130+
*/
131+
public function implementsInterfaceOnDao(string $interfaceName): self
132+
{
133+
$this->addAnnotation('AddInterfaceOnDao', ['name' => $interfaceName], false);
134+
return $this;
135+
}
136+
113137
private function getComment(): Comment
114138
{
115139
$options = $this->fluidTable->getDbalTable()->getOptions();

tests/TdbmFluidTableTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,30 @@ public function testCustomBeanName()
123123
$this->assertSame("\n@Bean(name = \"Article\")", $schema->getTable('posts')->getOptions()['comment']);
124124
}
125125

126+
public function testImplementsInterface()
127+
{
128+
$schema = new Schema();
129+
$fluid = new TdbmFluidSchema($schema);
130+
131+
$posts = $fluid->table('posts');
132+
133+
$posts->implementsInterface('Foo\\Bar');
134+
$posts->implementsInterface('Foo\\Bar2');
135+
136+
$this->assertSame("\n@AddInterface(name = \"Foo\\Bar\")\n@AddInterface(name = \"Foo\\Bar2\")", $schema->getTable('posts')->getOptions()['comment']);
137+
}
138+
139+
public function testImplementsInterfaceOnDao()
140+
{
141+
$schema = new Schema();
142+
$fluid = new TdbmFluidSchema($schema);
143+
144+
$posts = $fluid->table('posts');
145+
146+
$posts->implementsInterfaceOnDao('Foo\\Bar');
147+
148+
$this->assertSame("\n@AddInterfaceOnDao(name = \"Foo\\Bar\")", $schema->getTable('posts')->getOptions()['comment']);
149+
}
126150

127151
public function testTimestamps()
128152
{

0 commit comments

Comments
 (0)