Files
investbrain/app/Rules/SymbolValidationRule.php
hackerESQ 54cf25aabc wip
2024-08-24 22:19:40 -05:00

45 lines
988 B
PHP

<?php
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.
*
* @param string $attribute
* @param mixed $value
* @param \Closure $fail
* @return void
*/
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$this->symbol = $value;
if (MarketData::find($this->symbol)) {
return;
}
// Check if the symbol exists in the Market Data table first (avoid API call)
if (!app(MarketDataInterface::class)->exists($value)) {
$fail('The symbol provided (' . $this->symbol . ') is not valid');
}
}
}