Skip to content

Commit

Permalink
Rework Repo template for booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Oct 13, 2024
1 parent 204e6b4 commit 2f5ba2a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
14 changes: 7 additions & 7 deletions src/Template/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,19 @@ protected function setSetMethod($table)
$type = $type . '|null';
}

$lines[] = '/** @var ' . $type . ' */';
$lines[] = '$' . $name . ' = $data[\'' . $name . '\'];';

$isNull = $col->isNull();

$space = $isNull ? ' ' : '';
$space = $isNull || $type === 'boolean' ? ' ' : '';

if ($isNull)
if ($isNull || $type === 'boolean')
{
$lines[] = 'if ($' . $name . ')';
$lines[] = 'if (array_key_exists(\'' . $name . '\', $data))';
$lines[] = '{';
}

$lines[] = $space . '/** @var ' . $type . ' */';
$lines[] = $space . '$' . $name . ' = $data[\'' . $name . '\'];';

if ($col->isForeignKey())
{
$foreign = $col->getReferencedTable();
Expand All @@ -187,7 +187,7 @@ protected function setSetMethod($table)

$lines[] = $space . '$entity->set_' . $name . '($' . $name . ');';

if ($isNull)
if ($isNull || $type === 'boolean')
{
$lines[] = '}';
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixture/Plates/Doctrine/Repos/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public function set($data, $entity, $id = null)
$text = $data['text'];
$entity->set_text($text);

/** @var integer|null */
$user_id = $data['user_id'];
if ($user_id)
if (array_key_exists('user_id', $data))
{
/** @var integer|null */
$user_id = $data['user_id'];
$user = $this->_em->find('User', $user_id);
$entity->set_user($user);
}
Expand Down
15 changes: 9 additions & 6 deletions tests/Fixture/Plates/Doctrine/Repos/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,17 @@ public function set($data, $entity, $id = null)
$year = $data['year'];
$entity->set_year($year);

/** @var boolean */
$admin = $data['admin'];
$entity->set_admin($admin);
if (array_key_exists('admin', $data))
{
/** @var boolean */
$admin = $data['admin'];
$entity->set_admin($admin);
}

/** @var string|null */
$remarks = $data['remarks'];
if ($remarks)
if (array_key_exists('remarks', $data))
{
/** @var string|null */
$remarks = $data['remarks'];
$entity->set_remarks($remarks);
}
// -----------------------------------
Expand Down

0 comments on commit 2f5ba2a

Please sign in to comment.