-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #290 from saloonphp/feature/v3-final-tweaks
Feature | v3 Small Tweaks
- Loading branch information
Showing
19 changed files
with
387 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\Exceptions; | ||
|
||
class InvalidHeaderException extends SaloonException | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\Http; | ||
|
||
class BaseResource | ||
{ | ||
/** | ||
* Constructor | ||
*/ | ||
public function __construct(readonly protected Connector $connector) | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\Http\Middleware; | ||
|
||
use Saloon\Http\PendingRequest; | ||
use Saloon\Contracts\RequestMiddleware; | ||
use Saloon\Exceptions\InvalidHeaderException; | ||
|
||
class ValidateProperties implements RequestMiddleware | ||
{ | ||
/** | ||
* Validate the properties on the request before it is sent | ||
* | ||
* @throws \Saloon\Exceptions\InvalidHeaderException | ||
*/ | ||
public function __invoke(PendingRequest $pendingRequest): void | ||
{ | ||
// Validate that each header provided has a string key | ||
|
||
foreach ($pendingRequest->headers()->all() as $key => $unused) { | ||
if (! is_string($key)) { | ||
throw new InvalidHeaderException('One or more of the headers are invalid. Make sure to use the header name as the key. For example: \'Content-Type\' => \'application/json\'.'); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\Traits; | ||
|
||
use Closure; | ||
use ReflectionClass; | ||
use ReflectionMethod; | ||
use BadMethodCallException; | ||
|
||
/** | ||
* Many thanks to Spatie for building this excellent trait. | ||
* | ||
* @see https://github.com/spatie/macroable | ||
*/ | ||
trait Macroable | ||
{ | ||
/** | ||
* Macros stored | ||
* | ||
* @var array<object|callable> | ||
*/ | ||
protected static array $macros = []; | ||
|
||
/** | ||
* Create a macro | ||
*/ | ||
public static function macro(string $name, object|callable $macro): void | ||
{ | ||
static::$macros[$name] = $macro; | ||
} | ||
|
||
/** | ||
* Add a mixin | ||
* | ||
* @param object|class-string $mixin | ||
* @throws \ReflectionException | ||
*/ | ||
public static function mixin(object|string $mixin): void | ||
{ | ||
$methods = (new ReflectionClass($mixin))->getMethods( | ||
ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED | ||
); | ||
|
||
foreach ($methods as $method) { | ||
$method->setAccessible(true); | ||
|
||
static::macro($method->name, $method->invoke($mixin)); | ||
} | ||
} | ||
|
||
/** | ||
* Check if we have a macro | ||
*/ | ||
public static function hasMacro(string $name): bool | ||
{ | ||
return isset(static::$macros[$name]); | ||
} | ||
|
||
/** | ||
* Handle a static call | ||
* | ||
* @param array<string, mixed> $parameters | ||
*/ | ||
public static function __callStatic(string $method, array $parameters): mixed | ||
{ | ||
if (! static::hasMacro($method)) { | ||
throw new BadMethodCallException("Method {$method} does not exist."); | ||
} | ||
|
||
$macro = static::$macros[$method]; | ||
|
||
if ($macro instanceof Closure) { | ||
return call_user_func_array(Closure::bind($macro, null, static::class), $parameters); | ||
} | ||
|
||
return call_user_func_array($macro, $parameters); | ||
} | ||
|
||
/** | ||
* Handle a method call | ||
* | ||
* @param array<string, mixed> $parameters | ||
*/ | ||
public function __call(string $method, array $parameters): mixed | ||
{ | ||
if (! static::hasMacro($method)) { | ||
throw new BadMethodCallException("Method {$method} does not exist."); | ||
} | ||
|
||
$macro = static::$macros[$method]; | ||
|
||
if ($macro instanceof Closure) { | ||
return call_user_func_array($macro->bindTo($this, static::class), $parameters); | ||
} | ||
|
||
return call_user_func_array($macro, $parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Saloon\Http\Faking\MockClient; | ||
use Saloon\Http\Faking\MockResponse; | ||
use Saloon\Tests\Fixtures\Connectors\ResourceConnector; | ||
|
||
test('a resource can be used to send a request', function () { | ||
$mockClient = new MockClient([ | ||
MockResponse::fixture('user'), | ||
]); | ||
|
||
$connector = new ResourceConnector; | ||
$connector->withMockClient($mockClient); | ||
|
||
expect($connector->user()->get())->toEqual([ | ||
'name' => 'Sammyjo20', | ||
'actual_name' => 'Sam', | ||
'twitter' => '@carre_sam', | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\Tests\Fixtures\Connectors; | ||
|
||
use Saloon\Tests\Fixtures\Resources\UserBaseResource; | ||
|
||
class ResourceConnector extends TestConnector | ||
{ | ||
public function user(): UserBaseResource | ||
{ | ||
return new UserBaseResource($this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\Tests\Fixtures\Resources; | ||
|
||
use Saloon\Http\BaseResource; | ||
use Saloon\Tests\Fixtures\Requests\UserRequest; | ||
|
||
class UserBaseResource extends BaseResource | ||
{ | ||
/** | ||
* Get User | ||
* | ||
* @throws \JsonException | ||
* @throws \ReflectionException | ||
* @throws \Throwable | ||
*/ | ||
public function get(): array | ||
{ | ||
return $this->connector->send(new UserRequest)->array(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"statusCode":200,"headers":{"Date":"Mon, 11 Sep 2023 21:20:43 GMT","Content-Type":"application\/json","Content-Length":"63","Connection":"keep-alive","access-control-allow-origin":"*","Cache-Control":"no-cache, private","x-ratelimit-limit":"1000","x-ratelimit-remaining":"999","x-frame-options":"SAMEORIGIN","x-xss-protection":"1; mode=block","x-content-type-options":"nosniff","CF-Cache-Status":"DYNAMIC","Report-To":"{\"endpoints\":[{\"url\":\"https:\\\/\\\/a.nel.cloudflare.com\\\/report\\\/v3?s=6FQ2ADSCfKyvoEWPs9KjRyZXMfPJmR6bUu%2BXwFY0wYhRdQacLvV%2FTlZdmMX8vS%2FkoEoaTr%2B0kDpLjTd8PFH0%2FhuFShA7T1FxFLE9b6kf%2BM8T4FIJPiaWJSq2MnsZzle07j%2BR\"}],\"group\":\"cf-nel\",\"max_age\":604800}","NEL":"{\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}","Server":"cloudflare","CF-RAY":"8052f4d0a80f0743-MAN","alt-svc":"h3=\":443\"; ma=86400"},"data":"{\"name\":\"Sammyjo20\",\"actual_name\":\"Sam\",\"twitter\":\"@carre_sam\"}"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.