*/ protected $plaidEnvironments = [ "production" => "https://production.plaid.com/", "development" => "https://development.plaid.com/", "sandbox" => "https://sandbox.plaid.com/", ]; /** * ClientInterface instance. * * @var ClientInterface|null */ protected $httpClient; /** * Resource instance cache. * * @var array */ protected $resource_cache = []; /** * @param string $client_id * @param string $client_secret * @param string $environment Possible values are: production, development, sandbox * @throws UnexpectedValueException */ public function __construct( string $client_id, string $client_secret, string $environment = "production") { if( !\array_key_exists($environment, $this->plaidEnvironments) ){ throw new UnexpectedValueException("Invalid environment. Environment must be one of: production, development, or sandbox."); } $this->client_id = $client_id; $this->client_secret = $client_secret; $this->environment = $environment; } /** * Magic getter for resources. * * @param string $resource * @throws UnexpectedValueException * @return AbstractResource */ public function __get(string $resource): AbstractResource { if( !isset($this->resource_cache[$resource]) ){ $resource = \str_replace([" "], "", \ucwords(\str_replace(["_"], " ", $resource))); $resource_class = "\\TomorrowIdeas\\Plaid\\Resources\\" . $resource; if( !\class_exists($resource_class) ){ throw new UnexpectedValueException("Unknown Plaid resource: {$resource}"); } $reflectionClass = new ReflectionClass($resource_class); /** * @var AbstractResource $resource_instance */ $resource_instance = $reflectionClass->newInstanceArgs([ $this->getHttpClient(), $this->client_id, $this->client_secret, $this->plaidEnvironments[$this->environment] ]); $this->resource_cache[$resource] = $resource_instance; } return $this->resource_cache[$resource]; } /** * Set a specific ClientInterface instance to be used to make HTTP calls. * * @param ClientInterface $httpClient * @return void */ public function setHttpClient(ClientInterface $httpClient): void { $this->httpClient = $httpClient; } /** * Get the ClientInterface instance being used to make HTTP calls. * * @return ClientInterface */ public function getHttpClient(): ClientInterface { if( empty($this->httpClient) ){ $this->httpClient = new Shuttle; } return $this->httpClient; } }