From d49b22d9ab1d7c7cee4c2ad3fae6fd64f351f2df Mon Sep 17 00:00:00 2001 From: Tarvo R Date: Wed, 17 Feb 2021 17:32:50 +0200 Subject: [PATCH 1/2] Use data_get to retrieve value from resource Using Laravel's `data_get` helper allows nested values to be used for the dependencies, which is important for packages like Nova Page Manager where all the fields are stored in a `data` JSON column (that is casted into an array in the model). This allows dependencies on field attributes such as `data->type`. This PR does not introduce any breaking changes and does not affect existing setups. --- src/NovaDependencyContainer.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/NovaDependencyContainer.php b/src/NovaDependencyContainer.php index e8157e3..5e31c7b 100644 --- a/src/NovaDependencyContainer.php +++ b/src/NovaDependencyContainer.php @@ -150,31 +150,32 @@ public function resolveForDisplay($resource, $attribute = null) } foreach ($this->meta['dependencies'] as $index => $dependency) { - $this->meta['dependencies'][$index]['satisfied'] = false; - if (array_key_exists('empty', $dependency) && empty($resource->{$dependency['property']})) { + $value = data_get($resource, str_replace('->', '.', $dependency['property'])); + + if (array_key_exists('empty', $dependency) && empty($value)) { $this->meta['dependencies'][$index]['satisfied'] = true; continue; } // inverted `empty()` - if (array_key_exists('notEmpty', $dependency) && !empty($resource->{$dependency['property']})) { + if (array_key_exists('notEmpty', $dependency) && !empty($value)) { $this->meta['dependencies'][$index]['satisfied'] = true; continue; } // inverted - if (array_key_exists('nullOrZero', $dependency) && in_array($resource->{$dependency['property']}, [null, 0, '0'], true)) { + if (array_key_exists('nullOrZero', $dependency) && in_array($value, [null, 0, '0'], true)) { $this->meta['dependencies'][$index]['satisfied'] = true; continue; } - if (array_key_exists('not', $dependency) && $resource->{$dependency['property']} != $dependency['not']) { + if (array_key_exists('not', $dependency) && $value != $dependency['not']) { $this->meta['dependencies'][$index]['satisfied'] = true; continue; } if (array_key_exists('value', $dependency)) { - if ($dependency['value'] == $resource->{$dependency['property']}) { + if ($dependency['value'] == $value) { $this->meta['dependencies'][$index]['satisfied'] = true; continue; } @@ -185,7 +186,6 @@ public function resolveForDisplay($resource, $attribute = null) continue; } } - } } From 0fe2d6624e44c280a7a6edf45411fd3660ad3f2c Mon Sep 17 00:00:00 2001 From: Tarvo R Date: Wed, 17 Feb 2021 17:35:03 +0200 Subject: [PATCH 2/2] Code style fixes (undo deleted lines) --- src/NovaDependencyContainer.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/NovaDependencyContainer.php b/src/NovaDependencyContainer.php index 5e31c7b..0080db3 100644 --- a/src/NovaDependencyContainer.php +++ b/src/NovaDependencyContainer.php @@ -150,6 +150,7 @@ public function resolveForDisplay($resource, $attribute = null) } foreach ($this->meta['dependencies'] as $index => $dependency) { + $this->meta['dependencies'][$index]['satisfied'] = false; $value = data_get($resource, str_replace('->', '.', $dependency['property'])); @@ -186,6 +187,7 @@ public function resolveForDisplay($resource, $attribute = null) continue; } } + } }