* @return bool
*/
public static function pathIsset($values, $path) {
+ if ($path === []) {
+ return ($values !== NULL);
+ }
foreach ($path as $key) {
if (!is_array($values) || !isset($values[$key])) {
return FALSE;
* TRUE if anything has been removed. FALSE if no changes were required.
*/
public static function pathUnset(&$values, $path, $cleanup = FALSE) {
+ if (count($path) === 0) {
+ $values = NULL;
+ return TRUE;
+ }
+
if (count($path) === 1) {
if (isset($values[$path[0]])) {
unset($values[$path[0]]);
* Ex: 456.
*/
public static function pathSet(&$values, $pathParts, $value) {
+ if ($pathParts === []) {
+ $values = $value;
+ return;
+ }
$r = &$values;
$last = array_pop($pathParts);
foreach ($pathParts as $part) {
}
+ public function testGetSet_EmptyPath() {
+ $emptyPath = [];
+
+ $x = 'hello';
+ $this->assertEquals(TRUE, CRM_Utils_Array::pathIsset($x, $emptyPath));
+ $this->assertEquals('hello', CRM_Utils_Array::pathGet($x, $emptyPath));
+ $this->assertEquals('hello', $x);
+
+ CRM_Utils_Array::pathSet($x, $emptyPath, 'bon jour');
+ $this->assertEquals(TRUE, CRM_Utils_Array::pathIsset($x, $emptyPath));
+ $this->assertEquals('bon jour', CRM_Utils_Array::pathGet($x, $emptyPath));
+ $this->assertEquals('bon jour', $x);
+
+ CRM_Utils_Array::pathUnset($x, $emptyPath);
+ $this->assertEquals(FALSE, CRM_Utils_Array::pathIsset($x, $emptyPath));
+ $this->assertEquals(NULL, CRM_Utils_Array::pathGet($x, $emptyPath));
+ $this->assertEquals(NULL, $x);
+
+ CRM_Utils_Array::pathSet($x, $emptyPath, 'buenos dias');
+ $this->assertEquals(TRUE, CRM_Utils_Array::pathIsset($x, $emptyPath));
+ $this->assertEquals('buenos dias', CRM_Utils_Array::pathGet($x, $emptyPath));
+ $this->assertEquals('buenos dias', $x);
+ }
+
public function getSortExamples() {
$red = ['label' => 'Red', 'id' => 1, 'weight' => '90'];
$orange = ['label' => 'Orange', 'id' => 2, 'weight' => '70'];