Skip to content

Commit

Permalink
3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurlan Mukhanov committed Apr 6, 2023
1 parent 805e6c0 commit c3427bd
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .run/DBD-PHP-Entity.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="DBD-PHP-Entity" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
<CommandLine>
<PhpTestInterpreterSettings>
<option name="interpreterName" value="8.1" />
</PhpTestInterpreterSettings>
</CommandLine>
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml" directory="$PROJECT_DIR$/tests" use_alternative_configuration_file="true" />
<method v="2" />
</configuration>
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "falseclock/dbd-php-entity",
"type": "library",
"version": "3.0.0",
"description": "DTO like library to fetch any data in an Object-Oriented manner (ORM)",
"keywords": [
"dbd",
Expand All @@ -23,7 +24,7 @@
}
],
"require": {
"php": "^7.1|^8.0",
"php": "^8.0",
"ext-json": "*",
"myclabs/php-enum": "^1.7.7"
},
Expand Down
22 changes: 19 additions & 3 deletions src/DBD/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ private function setModelData(Mapper $map, int $maxLevels, int $currentLevel): v
* @param Mapper $mapper
*
* @throws EntityException
* @throws \ReflectionException
*/
private function setBaseColumns(Mapper $mapper)
{
Expand Down Expand Up @@ -284,10 +285,25 @@ private function setBaseColumns(Mapper $mapper)
* Entity public variables should not have default values.
* But sometimes we need to have default value for column in case of $rowData has null value
* In this case we should not override default value if $columnValue is null
* Иными словами нельзя переписывать дефолтное значение, если из базы пришло null
* но, если нет дефолтного значения, то мы должны его проинизиализировать null значением
*/
if (!isset($this->$property) and isset($columnValue)) {
$this->$property = &$columnValue;
}
$reflection = new ReflectionObject($this);
$reflectionProperty = $reflection->getProperty($property);

// Если мы еще не инциализировали переменную и у нас есть значение для этой переменной
//if (!isset($this->$property)) {

// Если у нас есть значение, то ставим его
if (isset($columnValue)) {
$this->$property = &$columnValue;
} else {
// У нас нет прицепленного значения
if (!$reflectionProperty->hasDefaultValue()) {
$this->$property = $columnValue; // this is NULL value
}
}
//}
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/DBD/Entity/Tests/Entities/EntityWithDefaults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace DBD\Entity\Tests\Entities;

use DBD\Entity\Column;
use DBD\Entity\Interfaces\SyntheticEntity;
use DBD\Entity\Mapper;
use DBD\Entity\Primitives\StringPrimitives;
use DBD\Entity\View;

class EntityWithDefaults extends View implements SyntheticEntity
{
public const PREFILL = "shouldn't be changed";

public ?string $prefiled = self::PREFILL;

public ?string $unfiled;
}


class EntityWithDefaultsMap extends Mapper
{
public $prefiled = [
Column::NAME => "prefilled",
Column::PRIMITIVE_TYPE => StringPrimitives::String,
Column::NULLABLE => true
];

public $unfiled = [
Column::NAME => "unfilled",
Column::PRIMITIVE_TYPE => StringPrimitives::String,
Column::NULLABLE => true
];
}
21 changes: 21 additions & 0 deletions tests/DBD/Entity/Tests/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use DBD\Entity\Tests\Entities\DeclarationChain\C;
use DBD\Entity\Tests\Entities\DeclarationChain\D;
use DBD\Entity\Tests\Entities\DeclarationChain\E;
use DBD\Entity\Tests\Entities\EntityWithDefaults;
use DBD\Entity\Tests\Entities\JsonTypeColumn;
use DBD\Entity\Tests\Entities\JsonTypeColumnMap;
use DBD\Entity\Tests\Entities\PersonBase;
Expand Down Expand Up @@ -65,6 +66,26 @@ public function testRaw()
self::assertSame($personData, $person->raw());
}

public function testPrefilledWithNullValues() {
$data = [
'prefilled' => null,
'unfilled' => null,
];

$entity = new EntityWithDefaults($data);
self::assertSame(EntityWithDefaults::PREFILL, $entity->prefiled);
self::assertSame(null, $entity->unfiled);

$data = [
'prefilled' => "123",
'unfilled' => "123",
];

$entity = new EntityWithDefaults($data);
self::assertSame("123", $entity->prefiled);
self::assertSame("123", $entity->unfiled);
}

/**
* @throws EntityException
*/
Expand Down

0 comments on commit c3427bd

Please sign in to comment.