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;
|
|
|
|
|
|
2025-01-28 20:32:43 -06:00
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 20:32:43 -06:00
|
|
|
// 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
|
|
|
}
|