Skip to content

Commit

Permalink
Added method to get entities based on expected return type
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Wolf committed Jul 9, 2015
1 parent a6262b8 commit bb52bfa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,39 @@ public function getByIdentifiers($values)
return $entities;
}

/**
* Get entities by identifier for a given return type.
*
* Note that the order of the returned entities may not match the provided order.
*
* @param mixed $values Entity identifier or the entity (or a listing of those)
* @param boolean $returnType "collection" or "single"
* @return mixed
*/
public function getByIdentifiersForType($values, $returnType = null)
{
if (empty($values) // No need to look up entities when none are provided
|| ($returnType == self::RETURN_TYPE_COLLECTION && !is_array($values))
|| ($returnType == self::RETURN_TYPE_SINGLE && !(is_scalar($values) || is_object($values)))
) {
// Default to empty array for now (we're handling the type below)
$entities = array();
} else {
$entities = $this->getByIdentifiers($values);
}

// Respect (or cast to) type when returning value
switch ($returnType) {
case self::RETURN_TYPE_COLLECTION:
return $entities;
case self::RETURN_TYPE_SINGLE:
return count($entities) > 0 ? reset($entities) : null;
default:
// Let's guess...
return is_array($values) ? $entities : null;
}
}

/**
* @param array $criteria
* @return int
Expand Down
12 changes: 12 additions & 0 deletions src/Detail/Persistence/Repository/EntityRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

interface EntityRepositoryInterface extends RepositoryInterface
{
const RETURN_TYPE_SINGLE = 'single';
const RETURN_TYPE_COLLECTION = 'collection';

/**
* @param array $identifiers
* @return array
Expand All @@ -18,6 +21,15 @@ public function findByIdentifiers(array $identifiers);
*/
public function getByIdentifiers($values);

/**
* Get entities by identifier for a given return type.
*
* @param mixed $values Entity identifier or the entity (or a listing of those)
* @param boolean $returnType "collection" or "single"
* @return mixed
*/
public function getByIdentifiersForType($values, $returnType);

/**
* Inquire the entity repository for the single primary key
*
Expand Down

0 comments on commit bb52bfa

Please sign in to comment.