Skip to content

Commit

Permalink
Entity: get() - fixed converting of value type
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Aug 8, 2024
1 parent 0f09216 commit aa6fd41
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/LeanMapper/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ protected function get($property, array $filterArgs = [])
}

try {
Helpers::convertType($value, $property->getType());
$value = Helpers::convertType($value, $property->getType());

} catch (InvalidValueException $e) {
throw new InvalidValueException("Property '$name' in entity " . get_called_class() . " is expected to contain an {$property->getType()}, " . Helpers::getType($value) . " given.", 0, $e);
Expand Down
50 changes: 50 additions & 0 deletions tests/LeanMapper/Entity.get.types.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

use Tester\Assert;

require_once __DIR__ . '/../bootstrap.php';

//////////

/**
* @property int $id
* @property non-empty-string $name
* @property string|null $description
* @property bool $available
*/
class Book extends LeanMapper\Entity
{
}


class BookRepository extends \LeanMapper\Repository
{
/**
* @param $id
* @return Author
* @throws Exception
*/
public function find($id)
{
$row = $this->connection->select('*')->from($this->getTable())->where('id = %i', $id)->fetch();
if ($row === false) {
throw new \Exception('Entity was not found.');
}
return $this->createEntity($row);
}
}

//////////

$connection = Tests::createConnection();
$mapper = Tests::createMapper();
$entityFactory = Tests::createEntityFactory();

$bookRepository = new BookRepository($connection, $mapper, $entityFactory);
$book = $bookRepository->find(1);

Assert::same('The Pragmatic Programmer', $book->name);
Assert::null($book->description);
Assert::true($book->available);

0 comments on commit aa6fd41

Please sign in to comment.