Changeset View
Changeset View
Standalone View
Standalone View
src/symbols/PhutilClassMapQuery.php
Show All 35 Lines | |||||
* @task config Configuring the Query | * @task config Configuring the Query | ||||
* @task exec Executing the Query | * @task exec Executing the Query | ||||
* @task cache Managing the Map Cache | * @task cache Managing the Map Cache | ||||
*/ | */ | ||||
final class PhutilClassMapQuery extends Phobject { | final class PhutilClassMapQuery extends Phobject { | ||||
private $ancestorClass; | private $ancestorClass; | ||||
private $expandMethod; | private $expandMethod; | ||||
private $filterNull = false; | |||||
private $uniqueMethod; | private $uniqueMethod; | ||||
private $sortMethod; | private $sortMethod; | ||||
// NOTE: If you add more configurable properties here, make sure that | // NOTE: If you add more configurable properties here, make sure that | ||||
// cache key construction in getCacheKey() is updated properly. | // cache key construction in getCacheKey() is updated properly. | ||||
private static $cache = array(); | private static $cache = array(); | ||||
Show All 18 Lines | /* -( Configuring the Query )---------------------------------------------- */ | ||||
* Provide a method to select a unique key for each instance. | * Provide a method to select a unique key for each instance. | ||||
* | * | ||||
* If you provide a method here, the map will be keyed with these values, | * If you provide a method here, the map will be keyed with these values, | ||||
* instead of with class names. Exceptions will be raised if entries are | * instead of with class names. Exceptions will be raised if entries are | ||||
* not unique. | * not unique. | ||||
* | * | ||||
* You must provide a method here to use @{method:setExpandMethod}. | * You must provide a method here to use @{method:setExpandMethod}. | ||||
* | * | ||||
* @param string Name of the unique key method. | * @param string Name of the unique key method. | ||||
* @param bool If true, then classes which return `null` will be filtered | |||||
* from the results. | |||||
epriestley: Prefer documenting. | |||||
* @return this | * @return this | ||||
* @task config | * @task config | ||||
*/ | */ | ||||
public function setUniqueMethod($unique_method) { | public function setUniqueMethod($unique_method, $filter_null = false) { | ||||
$this->uniqueMethod = $unique_method; | $this->uniqueMethod = $unique_method; | ||||
$this->filterNull = $filter_null; | |||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
* Provide a method to expand each concrete subclass into available instances. | * Provide a method to expand each concrete subclass into available instances. | ||||
* | * | ||||
* With some class maps, each class is allowed to provide multiple entries | * With some class maps, each class is allowed to provide multiple entries | ||||
▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Lines | if (strlen($expand)) { | ||||
$list = $objects; | $list = $objects; | ||||
} | } | ||||
// Apply the "unique" mechanism, if it is configured. | // Apply the "unique" mechanism, if it is configured. | ||||
if (strlen($unique)) { | if (strlen($unique)) { | ||||
$map = array(); | $map = array(); | ||||
foreach ($list as $object) { | foreach ($list as $object) { | ||||
$key = call_user_func(array($object, $unique)); | $key = call_user_func(array($object, $unique)); | ||||
if ($key === null && $this->filterNull) { | |||||
continue; | |||||
} | |||||
if (empty($map[$key])) { | if (empty($map[$key])) { | ||||
$map[$key] = $object; | $map[$key] = $object; | ||||
continue; | continue; | ||||
} | } | ||||
throw new Exception( | throw new Exception( | ||||
pht( | pht( | ||||
'Two objects (of classes "%s" and "%s", descendants of ancestor '. | 'Two objects (of classes "%s" and "%s", descendants of ancestor '. | ||||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |
Prefer documenting.