Skip to content

Commit

Permalink
Exporter: support property filters
Browse files Browse the repository at this point in the history
refs #2568
  • Loading branch information
Thomas-Gelf committed Jul 20, 2022
1 parent 164d1f5 commit cb355f9
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions library/Director/Data/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,17 @@ class Exporter
/** @var FieldReferenceLoader */
protected $fieldReferenceLoader;

/** @var bool */
protected $exportHostServices = false;

protected $showDefaults = false;

protected $showIds = false;
protected $resolveObjects = false;

/**
* @var Db
*/
/** @var Db */
protected $connection;

/** @var ?array */
protected $chosenProperties = null;

public function __construct(Db $connection)
{
$this->connection = $connection;
Expand All @@ -85,6 +84,17 @@ public function export(DbObject $object)
$this->stripDeniedProperties($props, $object);
$this->appendTypeSpecificRelations($props, $object);

if ($this->chosenProperties !== null) {
$chosen = [];
foreach ($this->chosenProperties as $k) {
if (array_key_exists($k, $props)) {
$chosen[$k] = $props[$k];
}
}

$props = $chosen;
}

ksort($props);
return (object) $props;
}
Expand All @@ -101,12 +111,24 @@ public function showDefaults($show = true)
return $this;
}

public function showIds($show = true)
{
$this->showIds = $show;
return $this;
}

public function resolveObjects($resolve = true)
{
$this->resolveObjects = $resolve;
return $this;
}

public function filterProperties(array $properties)
{
$this->chosenProperties = $properties;
return $this;
}

protected function appendTypeSpecificRelations(array &$props, DbObject $object)
{
if ($object instanceof DirectorDatalist) {
Expand Down Expand Up @@ -214,7 +236,9 @@ protected function stripDeniedProperties(array &$props, DbObject $object)
{
// TODO: this used to exist. Double-check all imports to verify it's not in use
// $originalId = $props['id'];
unset($props['id']);
if (! $this->showIds) {
unset($props['id']);
}
$class = get_class($object);
if (isset(self::$denyProperties[$class])) {
foreach (self::$denyProperties[$class] as $key) {
Expand Down

0 comments on commit cb355f9

Please sign in to comment.