diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..518633e --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +lts/fermium diff --git a/src/DependencyContainer.php b/src/DependencyContainer.php index 194fd03..09347d9 100644 --- a/src/DependencyContainer.php +++ b/src/DependencyContainer.php @@ -4,6 +4,7 @@ use Aqjw\MedialibraryField\Fields\Medialibrary; use Aqjw\MedialibraryField\Fields\Support\MediaCollectionRules; +use BackedEnum; use Illuminate\Support\Arr; use Laravel\Nova\Fields\Field; use Laravel\Nova\Http\Requests\NovaRequest; @@ -334,9 +335,14 @@ public function areDependenciesSatisfied(NovaRequest $request) if (array_key_exists('value', $dependency) && !array_key_exists('in', $dependency) && !array_key_exists('notin', $dependency) - && !array_key_exists('nullOrZero', $dependency) - && $dependency['value'] == $request->get($dependency['property'])) { - $satisfiedCounts++; + && !array_key_exists('nullOrZero', $dependency)) { + if ($dependency['value'] instanceof BackedEnum) { + if ($dependency['value']->value == $request->get($dependency['property'])) { + $satisfiedCounts++; + } + } elseif ($dependency['value'] == $request->get($dependency['property'])) { + $satisfiedCounts++; + } } } @@ -347,10 +353,10 @@ public function areDependenciesSatisfied(NovaRequest $request) * Get a rule set based on field property name * * @param NovaRequest $request - * @param string $propertyName + * @param string $methodName * @return array */ - protected function getSituationalRulesSet(NovaRequest $request, string $propertyName = 'rules') + protected function getSituationalRulesSet(NovaRequest $request, string $methodName = 'getRules') { $fieldsRules = [$this->attribute => []]; @@ -366,12 +372,10 @@ protected function getSituationalRulesSet(NovaRequest $request, string $property /** @var Field $field */ foreach ($this->meta['fields'] as $field) { // if field is DependencyContainer, then add rules from dependant fields - if ($field instanceof DependencyContainer && $propertyName === "rules") { - $fieldsRules[Str::random()] = $field->getSituationalRulesSet($request, $propertyName); + if ($field instanceof DependencyContainer && $methodName === "getRules") { + $fieldsRules[Str::random()] = $field->getSituationalRulesSet($request, $methodName); } elseif ($field instanceof Medialibrary) { - $rules = is_callable($field->{$propertyName}) - ? call_user_func($field->{$propertyName}, $request) - : $field->{$propertyName}; + $rules = $field->{$methodName}($request); $fieldsRules[$field->attribute] = MediaCollectionRules::make( $rules, @@ -379,9 +383,7 @@ protected function getSituationalRulesSet(NovaRequest $request, string $property $field, ); } else { - $fieldsRules[$field->attribute] = is_callable($field->{$propertyName}) - ? call_user_func($field->{$propertyName}, $request) - : $field->{$propertyName}; + $fieldsRules[$field->attribute] = $field->{$methodName}($request); } } @@ -429,7 +431,7 @@ public function getRules(NovaRequest $request) */ public function getCreationRules(NovaRequest $request) { - $fieldsRules = $this->getSituationalRulesSet($request, 'creationRules'); + $fieldsRules = $this->getSituationalRulesSet($request, 'getCreationRules'); return array_merge_recursive( $this->getRules($request), @@ -445,7 +447,7 @@ public function getCreationRules(NovaRequest $request) */ public function getUpdateRules(NovaRequest $request) { - $fieldsRules = $this->getSituationalRulesSet($request, 'updateRules'); + $fieldsRules = $this->getSituationalRulesSet($request, 'getUpdateRules'); return array_merge_recursive( $this->getRules($request),