Skip to content

Commit

Permalink
Improved Entity::__isset
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharos committed Jun 3, 2014
1 parent 33c2cbf commit b4a9dc7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
11 changes: 9 additions & 2 deletions LeanMapper/Entity.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public function __set($name, $value)
* Tells whether given property exists and is not null * Tells whether given property exists and is not null
* *
* @param string $name * @param string $name
* @throws LeanMapperException
* @return bool * @return bool
*/ */
public function __isset($name) public function __isset($name)
Expand All @@ -169,6 +170,11 @@ public function __isset($name)
return $this->$name !== null; return $this->$name !== null;
} catch (MemberAccessException $e) { } catch (MemberAccessException $e) {
return false; return false;
} catch (LeanMapperException $e) {
if ($this->isDetached() and $e->getCode() === Result::ERROR_MISSING_COLUMN) {
return false;
}
throw $e;
} }
} }


Expand Down Expand Up @@ -447,7 +453,7 @@ protected function get($property, array $filterArgs = array())
try { try {
$value = $this->row->$column; $value = $this->row->$column;
} catch (LeanMapperException $e) { } catch (LeanMapperException $e) {
throw new LeanMapperException("Cannot get value of property '{$property->getName()}' in entity " . get_called_class() . ' due to low-level failure: ' . $e->getMessage()); throw new LeanMapperException("Cannot get value of property '{$property->getName()}' in entity " . get_called_class() . ' due to low-level failure: ' . $e->getMessage(), $e->getCode());
} }
if ($pass !== null) { if ($pass !== null) {
$value = $this->$pass($value); $value = $this->$pass($value);
Expand Down Expand Up @@ -491,7 +497,7 @@ protected function get($property, array $filterArgs = array())
try { try {
$value = $this->row->$column; $value = $this->row->$column;
} catch (LeanMapperException $e) { } catch (LeanMapperException $e) {
throw new LeanMapperException("Cannot get value of property '{$property->getName()}' in entity " . get_called_class() . ' due to low-level failure: ' . $e->getMessage()); throw new LeanMapperException("Cannot get value of property '{$property->getName()}' in entity " . get_called_class() . ' due to low-level failure: ' . $e->getMessage(), $e->getCode());
} }
if ($pass !== null) { if ($pass !== null) {
$value = $this->$pass($value); $value = $this->$pass($value);
Expand Down Expand Up @@ -707,6 +713,7 @@ private function getHasOneValue(Property $property, Relationship\HasOne $relatio
return null; return null;
} else { } else {
$entityClass = $this->mapper->getEntityClass($targetTable, $row); $entityClass = $this->mapper->getEntityClass($targetTable, $row);

$entity = $this->entityFactory->createEntity($entityClass, $row); $entity = $this->entityFactory->createEntity($entityClass, $row);
$this->checkConsistency($property, $entityClass, $entity); $this->checkConsistency($property, $entityClass, $entity);
$entity->makeAlive($this->entityFactory); $entity->makeAlive($this->entityFactory);
Expand Down
4 changes: 3 additions & 1 deletion LeanMapper/Result.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Result implements \Iterator


const PRELOADED_KEY = 'preloaded'; const PRELOADED_KEY = 'preloaded';


const ERROR_MISSING_COLUMN = 1;

/** @var bool */ /** @var bool */
private $isDetached; private $isDetached;


Expand Down Expand Up @@ -210,7 +212,7 @@ public function getDataEntry($id, $key)
$key = $this->trimAlias($key); $key = $this->trimAlias($key);
} }
if (!array_key_exists($key, $this->data[$id])) { if (!array_key_exists($key, $this->data[$id])) {
throw new InvalidArgumentException("Missing '$key' column in row with id $id."); throw new InvalidArgumentException("Missing '$key' column in row with id $id.", self::ERROR_MISSING_COLUMN);
} }
return $this->data[$id][$key]; return $this->data[$id][$key];
} }
Expand Down
29 changes: 29 additions & 0 deletions tests/LeanMapper/Entity.isset.phpt
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use LeanMapper\Entity;
use LeanMapper\Exception\Exception;
use LeanMapper\Result;
use Tester\Assert;

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

/**
* @property int $id
* @property string $name
*/
class Author extends Entity
{
}

$author = new Author;

isset($author->name);

//////////

$author->makeAlive($entityFactory, $connection, $mapper);
$author->attach(1);

Assert::exception(function () use ($author) {
isset($author->name);
}, Exception::class, null, Result::ERROR_MISSING_COLUMN);

0 comments on commit b4a9dc7

Please sign in to comment.