Files
investbrain/app/Rules/SymbolValidationRule.php
T

44 lines
942 B
PHP
Raw Normal View History

2024-08-24 22:19:40 -05:00
<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
2024-08-24 22:19:40 -05:00
namespace App\Rules;
use App\Interfaces\MarketData\MarketDataInterface;
use App\Models\MarketData;
use Illuminate\Contracts\Validation\ValidationRule;
class SymbolValidationRule implements ValidationRule
{
public $symbol;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Validate the attribute.
*/
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$this->symbol = $value;
// Check if the symbol exists in the Market Data table first (avoid API call)
2024-08-24 22:19:40 -05:00
if (MarketData::find($this->symbol)) {
return;
}
// Then check against market data provider
2025-01-28 17:14:49 -06:00
if (! app(MarketDataInterface::class)->exists($value)) {
$fail('The symbol provided ('.$this->symbol.') is not valid');
2024-08-24 22:19:40 -05:00
}
}
2025-01-28 17:14:49 -06:00
}