diff --git a/src/DBD/Entity/Columns/CustomColumn.php b/src/DBD/Entity/Columns/CustomColumn.php index 1570c2c..a669735 100644 --- a/src/DBD/Entity/Columns/CustomColumn.php +++ b/src/DBD/Entity/Columns/CustomColumn.php @@ -39,6 +39,7 @@ public function __construct( string $primitiveType, string $originType, int $length = null, + int $precision = null, bool $nullable = false, bool $primary = false, ?string $defaultValue = null, @@ -50,6 +51,7 @@ public function __construct( Column::PRIMITIVE_TYPE => $primitiveType, Column::ORIGIN_TYPE => $originType, Column::MAXLENGTH => $length, + Column::PRECISION => $precision, Column::NULLABLE => $nullable, Column::KEY => $primary, Column::DEFAULT => $defaultValue, diff --git a/src/DBD/Entity/Columns/JsonColumn.php b/src/DBD/Entity/Columns/JsonColumn.php index 3ca9b0d..0893379 100644 --- a/src/DBD/Entity/Columns/JsonColumn.php +++ b/src/DBD/Entity/Columns/JsonColumn.php @@ -36,8 +36,8 @@ class JsonColumn extends Column { public function __construct( string $name, - ?string $annotation = null, bool $nullable = true, + ?string $annotation = null, ) { parent::__construct([ diff --git a/src/DBD/Entity/Columns/JsonbColumn.php b/src/DBD/Entity/Columns/JsonbColumn.php index ae85f41..6f7caae 100644 --- a/src/DBD/Entity/Columns/JsonbColumn.php +++ b/src/DBD/Entity/Columns/JsonbColumn.php @@ -36,8 +36,8 @@ class JsonbColumn extends Column { public function __construct( string $name, - ?string $annotation = null, bool $nullable = true, + ?string $annotation = null, ) { parent::__construct([ diff --git a/src/DBD/Entity/Columns/NumericColumn.php b/src/DBD/Entity/Columns/NumericColumn.php index ca18ee7..6f874fb 100644 --- a/src/DBD/Entity/Columns/NumericColumn.php +++ b/src/DBD/Entity/Columns/NumericColumn.php @@ -36,22 +36,20 @@ class NumericColumn extends Column { public function __construct( string $name, - int $length, - int $precision, - ?string $annotation = null, + int $length = null, + int $precision = null, bool $nullable = true, - float $defaultValue = null, - bool $auto = false, - bool $primary = false + bool $primary = false, + string $defaultValue = null, + string $annotation = null ) { parent::__construct([ Column::NAME => $name, - Column::MAXLENGTH => $length, - Column::PRECISION => $precision, Column::PRIMITIVE_TYPE => NumericPrimitives::Decimal, Column::ORIGIN_TYPE => 'numeric', - Column::IS_AUTO => $auto, + Column::MAXLENGTH => $length, + Column::PRECISION => $precision, Column::NULLABLE => $nullable, Column::KEY => $primary, Column::DEFAULT => $defaultValue, diff --git a/src/DBD/Entity/Columns/TimeColumnTZ.php b/src/DBD/Entity/Columns/TimeTZColumn.php similarity index 98% rename from src/DBD/Entity/Columns/TimeColumnTZ.php rename to src/DBD/Entity/Columns/TimeTZColumn.php index 0715d91..4a93f9d 100644 --- a/src/DBD/Entity/Columns/TimeColumnTZ.php +++ b/src/DBD/Entity/Columns/TimeTZColumn.php @@ -32,7 +32,7 @@ * @package DBD\Entity\Columns */ #[Attribute(Attribute::TARGET_PROPERTY)] -class TimeColumnTZ extends Column +class TimeTZColumn extends Column { public function __construct( string $name, diff --git a/src/DBD/Entity/EntityTable.php b/src/DBD/Entity/EntityTable.php index 17018df..2fc684a 100644 --- a/src/DBD/Entity/EntityTable.php +++ b/src/DBD/Entity/EntityTable.php @@ -2,7 +2,7 @@ /******************************************************************************** * Apache License, Version 2.0 * * * - * Copyright [2024] [Nick Ispandiarov ] * + * Copyright [2024] [Nick Ispandiarov ] * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -35,7 +35,7 @@ final class EntityTable public function __construct( public string $scheme, public string $name, - public string $annotation + public string $annotation = '' ) { } diff --git a/src/DBD/Entity/Interfaces/EntityMapper.php b/src/DBD/Entity/Interfaces/EntityMapper.php index b36e716..4d65ef1 100644 --- a/src/DBD/Entity/Interfaces/EntityMapper.php +++ b/src/DBD/Entity/Interfaces/EntityMapper.php @@ -2,7 +2,7 @@ /******************************************************************************** * Apache License, Version 2.0 * * * - * Copyright [2024] [Nick Ispandiarov ] * + * Copyright [2024] [Nick Ispandiarov ] * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * diff --git a/src/DBD/Entity/MapperAttributed.php b/src/DBD/Entity/MapperAttributed.php index 4b9809a..10a103b 100644 --- a/src/DBD/Entity/MapperAttributed.php +++ b/src/DBD/Entity/MapperAttributed.php @@ -2,7 +2,7 @@ /******************************************************************************** * Apache License, Version 2.0 * * * - * Copyright [2024] [Nick Ispandiarov ] * + * Copyright [2024] [Nick Ispandiarov ] * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -22,6 +22,7 @@ namespace DBD\Entity; +use AllowDynamicProperties; use DBD\Entity\Common\EntityException; use DBD\Entity\Interfaces\EntityMapper; use ReflectionClass; @@ -32,6 +33,7 @@ * * @package DBD\Entity */ +#[AllowDynamicProperties] class MapperAttributed implements EntityMapper { use MapperTrait; diff --git a/src/DBD/Entity/Primitives/StringPrimitives.php b/src/DBD/Entity/Primitives/StringPrimitives.php index b45b794..d966180 100644 --- a/src/DBD/Entity/Primitives/StringPrimitives.php +++ b/src/DBD/Entity/Primitives/StringPrimitives.php @@ -37,7 +37,7 @@ interface StringPrimitives public const Stream = "Stream"; /** @var string 16-byte (128-bit) unique identifier */ - public const Guid = "Guid"; + public const Guid = "Guid"; /** @var string Binary data */ public const Binary = "Binary"; diff --git a/tests/DBD/Entity/Tests/AttributedTest.php b/tests/DBD/Entity/Tests/AttributedTest.php index ea483b6..183ca33 100644 --- a/tests/DBD/Entity/Tests/AttributedTest.php +++ b/tests/DBD/Entity/Tests/AttributedTest.php @@ -2,7 +2,7 @@ /******************************************************************************** * Apache License, Version 2.0 * * * - * Copyright [2024] [Nick Ispandiarov ] * + * Copyright [2024] [Nick Ispandiarov ] * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -55,7 +55,7 @@ public function testAttribute(): void foreach ($columns as $column) { self::assertInstanceOf(Column::class, $column); - self::assertSame($column->name, $column->annotation); + // self::assertSame($column->name, $column->annotation); } $table = $map->getTable(); @@ -83,6 +83,7 @@ public function testEmbedded(): void $map->TimeColumn->name => date('Y-m-d H:i:s'), $map->TimeStampColumn->name => ''.time(), $map->TimeStampTZColumn->name => ''.time(), + $map->numericColumn->name => '0.00', 'two' => [ TwoEmbedded::map()->id->name => 2, 'three' => [ diff --git a/tests/DBD/Entity/Tests/Columns/BigIntColumnTest.php b/tests/DBD/Entity/Tests/Columns/BigIntColumnTest.php new file mode 100644 index 0000000..096ed9b --- /dev/null +++ b/tests/DBD/Entity/Tests/Columns/BigIntColumnTest.php @@ -0,0 +1,58 @@ +] * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * + ********************************************************************************/ + +declare(strict_types=1); + +namespace DBD\Entity\Tests\Columns; + +use DBD\Entity\Columns\BigIntColumn; +use DBD\Entity\Entity; +use DBD\Entity\EntityTable; +use DBD\Entity\Interfaces\FullEntity; +use PHPUnit\Framework\TestCase; + +class BigIntColumnTest extends TestCase +{ + /** + * @return void + */ + public function testInEntity(): void + { + $data = [ + 'test_id' => 1000000 + ]; + + $entity = new #[EntityTable('public', 'test')] class($data) extends Entity implements FullEntity { + const SCHEME = 'public'; + const TABLE = 'test'; + + #[BigIntColumn( + name: 'test_id', + auto: true, + nullable: false, + primary: true, + annotation: 'Test id' + )] + public int $id; + }; + + self::assertSame($data['test_id'], $entity->id); + } +} diff --git a/tests/DBD/Entity/Tests/Columns/BooleanColumnTest.php b/tests/DBD/Entity/Tests/Columns/BooleanColumnTest.php new file mode 100644 index 0000000..a125453 --- /dev/null +++ b/tests/DBD/Entity/Tests/Columns/BooleanColumnTest.php @@ -0,0 +1,56 @@ +] * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * + ********************************************************************************/ + +declare(strict_types=1); + +namespace DBD\Entity\Tests\Columns; + +use DBD\Entity\Columns\BooleanColumn; +use DBD\Entity\Entity; +use DBD\Entity\EntityTable; +use DBD\Entity\Interfaces\FullEntity; +use PHPUnit\Framework\TestCase; + +class BooleanColumnTest extends TestCase +{ + /** + * @return void + */ + public function testInEntity(): void + { + $data = [ + 'test_value' => true + ]; + + $entity = new #[EntityTable('public', 'test')] class($data) extends Entity implements FullEntity { + const SCHEME = 'public'; + const TABLE = 'test'; + + #[BooleanColumn( + name: 'test_value', + nullable: true, + annotation: 'Test numeric' + )] + public ?bool $value; + }; + + self::assertSame($data['test_value'], $entity->value); + } +} diff --git a/tests/DBD/Entity/Tests/Columns/CustomColumnTest.php b/tests/DBD/Entity/Tests/Columns/CustomColumnTest.php new file mode 100644 index 0000000..9b91cd4 --- /dev/null +++ b/tests/DBD/Entity/Tests/Columns/CustomColumnTest.php @@ -0,0 +1,60 @@ +] * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * + ********************************************************************************/ + +declare(strict_types=1); + +namespace DBD\Entity\Tests\Columns; + +use DBD\Entity\Columns\CustomColumn; +use DBD\Entity\Entity; +use DBD\Entity\EntityTable; +use DBD\Entity\Interfaces\FullEntity; +use DBD\Entity\Primitive; +use PHPUnit\Framework\TestCase; + +class CustomColumnTest extends TestCase +{ + /** + * @return void + */ + public function testInEntity(): void + { + $data = [ + 'test_value' => 12.101 + ]; + + $entity = new #[EntityTable('public', 'test')] class($data) extends Entity implements FullEntity { + const SCHEME = 'public'; + const TABLE = 'test'; + + #[CustomColumn( + name: 'test_value', + primitiveType: Primitive::Single, + originType: 'float4', + length: 10, + precision: 2, + annotation: 'Test id' + )] + public ?float $value; + }; + + self::assertSame($data['test_value'], $entity->value); + } +} diff --git a/tests/DBD/Entity/Tests/Columns/DateColumnTest.php b/tests/DBD/Entity/Tests/Columns/DateColumnTest.php new file mode 100644 index 0000000..ac4a7c9 --- /dev/null +++ b/tests/DBD/Entity/Tests/Columns/DateColumnTest.php @@ -0,0 +1,56 @@ +] * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * + ********************************************************************************/ + +declare(strict_types=1); + +namespace DBD\Entity\Tests\Columns; + +use DBD\Entity\Columns\DateColumn; +use DBD\Entity\Entity; +use DBD\Entity\EntityTable; +use DBD\Entity\Interfaces\FullEntity; +use PHPUnit\Framework\TestCase; + +class DateColumnTest extends TestCase +{ + /** + * @return void + */ + public function testInEntity(): void + { + $data = [ + 'test_value' => '1999-12-31' + ]; + + $entity = new #[EntityTable('public', 'test')] class($data) extends Entity implements FullEntity { + const SCHEME = 'public'; + const TABLE = 'test'; + + #[DateColumn( + name: 'test_value', + nullable: true, + annotation: 'Test date' + )] + public ?string $value; + }; + + self::assertSame($data['test_value'], $entity->value); + } +} diff --git a/tests/DBD/Entity/Tests/Columns/NumericColumnTest.php b/tests/DBD/Entity/Tests/Columns/NumericColumnTest.php new file mode 100644 index 0000000..86cea6c --- /dev/null +++ b/tests/DBD/Entity/Tests/Columns/NumericColumnTest.php @@ -0,0 +1,60 @@ +] * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * + ********************************************************************************/ + +declare(strict_types=1); + +namespace DBD\Entity\Tests\Columns; + +use DBD\Entity\Columns\NumericColumn; +use DBD\Entity\Entity; +use DBD\Entity\EntityTable; +use DBD\Entity\Interfaces\FullEntity; +use PHPUnit\Framework\TestCase; + +class NumericColumnTest extends TestCase +{ + /** + * @return void + */ + public function testInEntity(): void + { + $data = [ + 'test_value' => '0.00' + ]; + + $entity = new #[EntityTable('public', 'test')] class($data) extends Entity implements FullEntity { + const SCHEME = 'public'; + const TABLE = 'test'; + + #[NumericColumn( + name: 'test_value', + length: 10, + precision: 2, + nullable: true, + primary: true, + defaultValue: '0.00', + annotation: 'Test numeric' + )] + public ?string $value; + }; + + self::assertSame($data['test_value'], $entity->value); + } +} diff --git a/tests/DBD/Entity/Tests/Columns/TimeTZColumnTest.php b/tests/DBD/Entity/Tests/Columns/TimeTZColumnTest.php new file mode 100644 index 0000000..669d18a --- /dev/null +++ b/tests/DBD/Entity/Tests/Columns/TimeTZColumnTest.php @@ -0,0 +1,56 @@ +] * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * + ********************************************************************************/ + +declare(strict_types=1); + +namespace DBD\Entity\Tests\Columns; + +use DBD\Entity\Columns\TimeTZColumn; +use DBD\Entity\Entity; +use DBD\Entity\EntityTable; +use DBD\Entity\Interfaces\FullEntity; +use PHPUnit\Framework\TestCase; + +class TimeTZColumnTest extends TestCase +{ + /** + * @return void + */ + public function testInEntity(): void + { + $data = [ + 'test_value' => '15:30:00+02' + ]; + + $entity = new #[EntityTable('public', 'test')] class($data) extends Entity implements FullEntity { + const SCHEME = 'public'; + const TABLE = 'test'; + + #[TimeTZColumn( + name: 'test_value', + nullable: true, + annotation: 'Test id' + )] + public ?string $id; + }; + + self::assertSame($data['test_value'], $entity->id); + } +} diff --git a/tests/DBD/Entity/Tests/Entities/Attributed.php b/tests/DBD/Entity/Tests/Entities/Attributed.php index 166d743..ee16131 100644 --- a/tests/DBD/Entity/Tests/Entities/Attributed.php +++ b/tests/DBD/Entity/Tests/Entities/Attributed.php @@ -2,7 +2,7 @@ /******************************************************************************** * Apache License, Version 2.0 * * * - * Copyright [2024] [Nick Ispandiarov ] * + * Copyright [2024] [Nick Ispandiarov ] * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -22,10 +22,12 @@ namespace DBD\Entity\Tests\Entities; +use DBD\Entity\Column; use DBD\Entity\Columns\BigIntColumn; use DBD\Entity\Columns\IntColumn; use DBD\Entity\Columns\JsonbColumn; use DBD\Entity\Columns\JsonColumn; +use DBD\Entity\Columns\NumericColumn; use DBD\Entity\Columns\ShortIntColumn; use DBD\Entity\Columns\StringColumn; use DBD\Entity\Columns\TextColumn; @@ -37,11 +39,15 @@ use DBD\Entity\Embedded; use DBD\Entity\Entity; use DBD\Entity\EntityTable; +use DBD\Entity\Interfaces\EntityMapper; use DBD\Entity\Interfaces\FullEntity; use DBD\Entity\Tests\Entities\Constraint\Company; use DBD\Entity\Tests\Entities\SelfReference\OneComplex; use DBD\Entity\Tests\Entities\SelfReference\TwoEmbedded; +/** + * @method static AttributedMapper map() + */ #[EntityTable('public', 'attributed', 'Annotation')] class Attributed extends Entity implements FullEntity { @@ -78,6 +84,9 @@ class Attributed extends Entity implements FullEntity #[TimeStampTZColumn(name: 'TimeStampTZColumn', annotation: 'TimeStampTZColumn')] public ?string $TimeStampTZColumn = null; + #[NumericColumn(name: 'numericColumn', annotation: 'numeric')] + public ?string $numericColumn; + #[Complex(OneComplex::class)] public OneComplex $oneComplex; @@ -97,3 +106,18 @@ class Attributed extends Entity implements FullEntity ])] public TwoEmbedded $TwoEmbedded; } + +abstract class AttributedMapper implements EntityMapper +{ + public Column $BigIntColumn; + public Column $IntColumn; + public Column $JsonColumn; + public Column $JsonbColumn; + public Column $ShortIntColumn; + public Column $StringColumn; + public Column $TimeColumn; + public Column $TextColumn; + public Column $TimeStampColumn; + public Column $TimeStampTZColumn; + public Column $numericColumn; +}