Skip to content

Commit

Permalink
Allow specifying of rules manually (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
morrislaptop authored Oct 17, 2023
1 parent 4f879e4 commit dabe597
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
31 changes: 17 additions & 14 deletions src/ConditionalLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ public function __construct(
string $name,
string $operator,
mixed $value = null,
string $group = null
string $group = null,
string $key = null,
) {
$this->rules[] = $this->rule($name, $operator, $value, $group);
$this->rules[] = [
'name' => $name,
'operator' => $operator,
'value' => $value,
'group' => $group,
'key' => $key,
];
}

/**
* @param string $operator `==` is equal to, `!=` is not equal to, `>` is greater than, `<` is less than, `==pattern` matches pattern, `==contains` contains value, `==empty` has no value, `!=empty` has any value
* @throws \InvalidArgumentException
*/
public static function where(string $name, string $operator, mixed $value = null, string $group = null): static
public static function where(string $name, string $operator, mixed $value = null, string $group = null, string $key = null): static
{
$allowedOperators = [
'>',
Expand All @@ -49,24 +56,20 @@ public static function where(string $name, string $operator, mixed $value = null
throw new InvalidArgumentException("Invalid conditional logic operator [$operator].");
}

return new self($name, $operator, $value, $group);
}

public function and(string|array $name, string $operator, mixed $value = null, string $group = null): static
{
$this->rules[] = $this->rule($name, $operator, $value, $group);

return $this;
return new self($name, $operator, $value, $group, $key);
}

private function rule(string|array $name, string $operator, mixed $value = null, string $group = null): array
public function and(string|array $name, string $operator, mixed $value = null, string $group = null, string $key = null): static
{
return [
$this->rules[] = [
'name' => $name,
'operator' => $operator,
'value' => $value,
'group' => $group,
'field' => $key,
];

return $this;
}

/** @internal */
Expand All @@ -79,7 +82,7 @@ public function get(string|null $parentKey = null): array
$key = $resolvedParentKey . '_' . Key::sanitize($rule['name']);

$newRule = [
'field' => 'field_' . Key::hash($key),
'field' => $rule['key'] ?? 'field_' . Key::hash($key),
'operator' => $rule['operator'],
];

Expand Down
6 changes: 3 additions & 3 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,21 @@ public function get(string|null $parentKey = null): array

if (isset($this->settings['conditional_logic'])) {
$this->settings['conditional_logic'] = array_map(
fn ($rules) => $rules->get($parentKey),
fn($rules) => $rules->get($parentKey),
$this->settings['conditional_logic']
);
}

if (isset($this->settings['layouts'])) {
$this->settings['layouts'] = array_map(
fn ($layout) => $layout->get($key),
fn($layout) => $layout->get($key),
$this->settings['layouts']
);
}

if (isset($this->settings['sub_fields'])) {
$this->settings['sub_fields'] = array_map(
fn ($field) => $field->get($key),
fn($field) => $field->get($key),
$this->settings['sub_fields']
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Settings/Choices.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait Choices
public function choices(array $choices): static
{
if (array_is_list($choices)) {
$choices = array_combine(array_map(fn ($key) => Key::sanitize($key), $choices), $choices);
$choices = array_combine(array_map(fn($key) => Key::sanitize($key), $choices), $choices);
}

$this->settings['choices'] = $choices;
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ function register_extended_field_group(array $settings): array

$settings['style'] ??= 'seamless';

$settings['fields'] = array_map(fn ($field) => $field->get($key), $settings['fields']);
$settings['fields'] = array_map(fn($field) => $field->get($key), $settings['fields']);

$settings['location'] = array_map(fn ($location) => $location->get(), $settings['location']);
$settings['location'] = array_map(fn($location) => $location->get(), $settings['location']);

$settings['key'] = Key::generate($key, 'group');

Expand Down
27 changes: 27 additions & 0 deletions tests/ConditionalLogicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,31 @@ public function testResolvedParentKey()
$settings['fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['field']
);
}

public function testFieldKey()
{
$settings = register_extended_field_group([
'title' => 'Field Key',
'fields' => [
Select::make('Select')
->key('field_123abc')
->choices(['Red']),
Text::make('Text')
->conditionalLogic([
ConditionalLogic::where(
name: 'select',
operator: '==',
value: 'red',
key: 'field_123abc'
),
]),
],
'location' => []
]);

$this->assertSame(
'field_123abc',
$settings['fields'][1]['conditional_logic'][0][0]['field']
);
}
}

0 comments on commit dabe597

Please sign in to comment.