Skip to content

Commit

Permalink
ignore private properties in base entities...
Browse files Browse the repository at this point in the history
also entities starting with underscore are ignored!
  • Loading branch information
Zrnik committed Nov 9, 2021
1 parent 949507b commit 6435549
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"phpstan/phpstan-phpunit": "^0.12",
"nette/neon": "^3",
"brick/date-time": "^0.3",
"zrnik/phpunit-exceptions": "^0.0.4"
"zrnik/phpunit-exceptions": "^0.0.5"
},
"autoload": {
"psr-4": {
Expand Down
57 changes: 30 additions & 27 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/Repository/BaseEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ public function toArray(): array

foreach ($reflection->getProperties() as $reflectionProperty) {

if ($reflectionProperty->isPrivate()) {
continue;
}

if (str_starts_with($reflectionProperty->getName(), '_')) {
continue;
}

if (!$reflectionProperty->isInitialized($this)) {
continue;
}
Expand Down Expand Up @@ -287,6 +295,14 @@ public static function fromIterable(iterable $iterable): static

foreach ($reflection->getProperties() as $reflectionProperty) {

if (str_starts_with($reflectionProperty->getName(), '_')) {
continue;
}

if ($reflectionProperty->isPrivate()) {
continue;
}

$propertyName = $reflectionProperty->getName();
$reflectionPropertyName = $reflectionProperty->getName();
$intrinsicType = $reflectionProperty->getType();
Expand Down
22 changes: 22 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Tests\Mock\BaseRepositoryAndBaseEntity\Entities\Auction;
use Tests\Mock\BaseRepositoryAndBaseEntity\Entities\AuctionItem;
use Tests\Mock\BaseRepositoryAndBaseEntity\Entities\AutoHydrateAndCircularReference\ReferencingEntityOne;
use Tests\Mock\BaseRepositoryAndBaseEntity\Entities\EntityWithPrivateProperty\EntityWithPrivateProperty;
use Tests\Mock\BaseRepositoryAndBaseEntity\Entities\EntityWithPrivateProperty\PrivatePropertyRepository;
use Tests\Mock\BaseRepositoryAndBaseEntity\Entities\NullableForeignKeyNoError\NullableForeignKeyNoErrorRepository;
use Tests\Mock\BaseRepositoryAndBaseEntity\Entities\NullableForeignKeyNoError\SelfRepeating;
use Tests\Mock\BaseRepositoryAndBaseEntity\Entities\NullableForeignKeyNoError\SubClass;
Expand Down Expand Up @@ -105,6 +107,7 @@ public function testIntegration(): void
* @param PDO $pdo
* @param string $version
* @throws JsonException
* @throws Exception
*/
private function processTest(PDO $pdo, string $version = ''): void
{
Expand Down Expand Up @@ -193,6 +196,8 @@ function () use ($Updater) {

$this->treeSavingFailed($pdo);

$this->subTestAllowPrivatePropertiesInEntities($pdo);


echo ']' . PHP_EOL . 'Complete!';

Expand Down Expand Up @@ -1185,4 +1190,21 @@ private function treeSavingFailed(PDO $pdo): void
static::assertSame($subNode1->id, (int)$saved[2]['parent']);
static::assertSame($subNode2->id, (int)$saved[3]['parent']);
}

private function subTestAllowPrivatePropertiesInEntities(PDO $pdo): void
{
$repo = new PrivatePropertyRepository($pdo);

/** @var EntityWithPrivateProperty $entity */
$entity = EntityWithPrivateProperty::create();
$entity->name = 'Hello World';

$repo->save($entity);

/** @var EntityWithPrivateProperty[] $entities */
$entities = $repo->getAll(EntityWithPrivateProperty::class);

static::assertSame($entities[count($entities) - 1]->id, $entity->id);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace Tests\Mock\BaseRepositoryAndBaseEntity\Entities\EntityWithPrivateProperty;

use Zrnik\MkSQL\Repository\Attributes\ColumnType;
use Zrnik\MkSQL\Repository\Attributes\PrimaryKey;
use Zrnik\MkSQL\Repository\Attributes\TableName;
use Zrnik\MkSQL\Repository\BaseEntity;

#[TableName('EntityWithPrivateProperty')]
class EntityWithPrivateProperty extends BaseEntity
{
#[PrimaryKey]
public ?int $id = null;

#[ColumnType('varchar(255)')]
public string $name;

private string $secret = '';

public function getSecret(): string {
return $this->secret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Tests\Mock\BaseRepositoryAndBaseEntity\Entities\EntityWithPrivateProperty;

use Zrnik\MkSQL\Updater;
use Zrnik\MkSQL\Utilities\Installable;

class PrivatePropertyRepository extends Installable
{
protected function install(Updater $updater): void
{
$updater->use(EntityWithPrivateProperty::class);
}
}

0 comments on commit 6435549

Please sign in to comment.