Unverified Commit 5ad28d28 authored by totten's avatar totten Committed by GitHub

Merge pull request #13214 from aydun/getcontextvalues

 Add \Civi\Token\TokenProcessor::getContextValues()
parents ef60d307 9fd3215e
......@@ -168,6 +168,43 @@ class TokenProcessor {
return new TokenRowIterator($this, new \ArrayIterator($this->rowContexts));
}
/**
* Get a list of all unique values for a given context field,
* whether defined at the processor or row level.
*
* @param string $field
* Ex: 'contactId'.
* @return array
* Ex: [12, 34, 56].
*/
public function getContextValues($field, $subfield = NULL) {
$values = [];
if (isset($this->context[$field])) {
if ($subfield) {
if (isset($this->context[$field]->$subfield)) {
$values[] = $this->context[$field]->$subfield;
}
}
else {
$values[] = $this->context[$field];
}
}
foreach ($this->getRows() as $row) {
if (isset($row->context[$field])) {
if ($subfield) {
if (isset($row->context[$field]->$subfield)) {
$values[] = $row->context[$field]->$subfield;
}
}
else {
$values[] = $row->context[$field];
}
}
}
$values = array_unique($values);
return $values;
}
/**
* Get the list of available tokens.
*
......
......@@ -58,6 +58,22 @@ class TokenProcessorTest extends \CiviUnitTestCase {
}
}
/**
* Check that getContextValues() returns the correct data
*/
public function testGetContextValues() {
$p = new TokenProcessor($this->dispatcher, array(
'controller' => __CLASS__,
'omega' => '99',
));
$p->addRow()->context('id', 10)->context('omega', '98');
$p->addRow()->context('id', 10)->context('contact', (object) ['cid' => 10]);
$p->addRow()->context('id', 11)->context('contact', (object) ['cid' => 11]);
$this->assertArrayValuesEqual([10, 11], $p->getContextValues('id'));
$this->assertArrayValuesEqual(['99', '98'], $p->getContextValues('omega'));
$this->assertArrayValuesEqual([10, 11], $p->getContextValues('contact', 'cid'));
}
/**
* Check that the TokenRow helper can correctly read/update token
* values.
......
......@@ -684,6 +684,18 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
);
}
/**
* @param $expected
* @param $actual
*/
public function assertArrayValuesEqual($array1, $array2) {
$array1 = array_values($array1);
$array2 = array_values($array2);
sort($array1);
sort($array2);
$this->assertEquals($array1, $array2);
}
/**
* @param $expected
* @param $actual
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment