trypost/app/Actions/Automation/Node/RunConditionNode.php
Paulo Castellano b23ab0166e feat(automations): implement automation features and UI enhancements
- Added new automation-related routes and controllers for managing automations.
- Introduced automation nodes in the UI with distinct styles and interactions.
- Updated sidebar to include navigation for automations.
- Enhanced post creation logic to support automation metadata.
- Refactored content type and platform enums into types for better type safety.
- Added localization for automation-related terms in English, Spanish, and Portuguese.
- Improved error handling in various components to accommodate new features.
2026-05-24 09:17:19 -03:00

61 lines
2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Actions\Automation\Node;
use App\DataTransferObjects\Automation\NodeRunResult;
use App\Enums\Automation\Condition\Operator;
use App\Models\AutomationRun;
use App\Services\Automation\ExpressionResolver;
class RunConditionNode
{
private const MAX_REGEX_LENGTH = 200;
public function __construct(private ExpressionResolver $resolver) {}
public function __invoke(AutomationRun $run, array $config): NodeRunResult
{
$field = $this->resolver->resolve(data_get($config, 'field', ''), $run->context ?? []);
$operator = Operator::from(data_get($config, 'operator', 'equals'));
$value = (string) data_get($config, 'value', '');
$matched = match ($operator) {
Operator::Contains => str_contains($field, $value),
Operator::NotContains => ! str_contains($field, $value),
Operator::Equals => $field === $value,
Operator::NotEquals => $field !== $value,
Operator::Matches => $this->safeRegexMatch($value, $field),
Operator::GreaterThan => is_numeric($field) && is_numeric($value) && (float) $field > (float) $value,
Operator::LessThan => is_numeric($field) && is_numeric($value) && (float) $field < (float) $value,
};
return NodeRunResult::completed(
output: ['condition' => ['resolved_field' => $field, 'matched' => $matched]],
nextHandle: $matched ? 'yes' : 'no',
);
}
private function safeRegexMatch(string $pattern, string $subject): bool
{
if (strlen($pattern) > self::MAX_REGEX_LENGTH) {
return false;
}
$escaped = str_replace('~', '\~', $pattern);
$regex = "~{$escaped}~u";
try {
$result = @preg_match($regex, $subject);
} catch (\Throwable) {
return false;
}
if ($result === false || preg_last_error() !== PREG_NO_ERROR) {
return false;
}
return $result === 1;
}
}