-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
607 additions
and
2 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,154 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the guanguans/notify. | ||
* | ||
* (c) guanguans <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled. | ||
*/ | ||
|
||
namespace Guanguans\Notify\Foundation\Concerns; | ||
|
||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* @property array $defined; | ||
* @property array $required; | ||
* @property array $deprecated; | ||
* @property array $defaults; | ||
* @property bool $prototype; | ||
* @property array $allowedValues; | ||
* @property array $allowedTypes; | ||
* @property array $normalizers; | ||
* @property array $infos; | ||
*/ | ||
trait HasOptions | ||
{ | ||
protected array $options = []; | ||
|
||
protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver | ||
{ | ||
// configure options resolver... | ||
return $optionsResolver; | ||
} | ||
|
||
private function configureAndResolveOptions(array $options, callable $configurator): array | ||
{ | ||
$resolver = new OptionsResolver(); | ||
|
||
$configurator($resolver); | ||
|
||
return $resolver->resolve($options); | ||
} | ||
|
||
private function preConfigureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver | ||
{ | ||
property_exists($this, 'defined') and $optionsResolver->setDefined($this->defined); | ||
property_exists($this, 'required') and $optionsResolver->setRequired($this->required); | ||
property_exists($this, 'defaults') and $optionsResolver->setDefaults($this->defaults); | ||
property_exists($this, 'prototype') and $optionsResolver->setPrototype($this->prototype); | ||
|
||
if (property_exists($this, 'deprecated')) { | ||
foreach ($this->deprecated as $option => $deprecated) { | ||
array_unshift($deprecated, $option); | ||
$optionsResolver->setDeprecated(...$deprecated); | ||
} | ||
} | ||
|
||
if (property_exists($this, 'allowedValues')) { | ||
foreach ($this->allowedValues as $option => $allowedValue) { | ||
$optionsResolver->setAllowedValues($option, $allowedValue); | ||
} | ||
} | ||
|
||
if (property_exists($this, 'allowedTypes')) { | ||
foreach ($this->allowedTypes as $option => $allowedType) { | ||
$optionsResolver->setAllowedTypes($option, $allowedType); | ||
} | ||
} | ||
|
||
if (property_exists($this, 'normalizers')) { | ||
foreach ($this->normalizers as $option => $normalizer) { | ||
$optionsResolver->setNormalizer($option, $normalizer); | ||
} | ||
} | ||
|
||
if (property_exists($this, 'infos')) { | ||
foreach ($this->infos as $option => $info) { | ||
$optionsResolver->setInfo($option, $info); | ||
} | ||
} | ||
|
||
return $optionsResolver; | ||
} | ||
|
||
public function setOptions(array $options): self | ||
{ | ||
$this->options = array_replace_recursive( | ||
$this->options, | ||
$this->configureAndResolveOptions($options, function (OptionsResolver $optionsResolver): void { | ||
$this->configureOptionsResolver($this->preConfigureOptionsResolver($optionsResolver)); | ||
}) | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function setOption(string $option, $value): self | ||
{ | ||
return $this->setOptions([$option => $value]); | ||
} | ||
|
||
public function getOption(string $option, $default = null) | ||
{ | ||
return $this->options[$option] ?? $default; | ||
} | ||
|
||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
public function __get($option) | ||
{ | ||
return $this->offsetGet($option); | ||
} | ||
|
||
public function __set($option, $value) | ||
{ | ||
$this->offsetSet($option, $value); | ||
} | ||
|
||
public function __isset($option) | ||
{ | ||
return $this->offsetExists($option); | ||
} | ||
|
||
public function __unset($option) | ||
{ | ||
$this->offsetUnset($option); | ||
} | ||
|
||
public function offsetExists($offset) | ||
{ | ||
return isset($this->options[$offset]); | ||
} | ||
|
||
public function offsetGet($offset) | ||
{ | ||
return $this->getOption($offset); | ||
} | ||
|
||
public function offsetSet($offset, $value) | ||
{ | ||
$this->setOption($offset, $value); | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
unset($this->options[$offset]); | ||
} | ||
} |
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); | ||
|
||
/** | ||
* This file is part of the guanguans/notify. | ||
* | ||
* (c) guanguans <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled. | ||
*/ | ||
|
||
namespace Guanguans\Notify\Foundation\Concerns; | ||
|
||
trait Tappable | ||
{ | ||
public function tap(callable $callback): self | ||
{ | ||
$callback($this); | ||
|
||
return $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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the guanguans/notify. | ||
* | ||
* (c) guanguans <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled. | ||
*/ | ||
|
||
namespace Guanguans\Notify\Foundation\Contracts; | ||
|
||
/** | ||
* @template TKey of array-key | ||
* @template TValue | ||
*/ | ||
interface Arrayable | ||
{ | ||
/** | ||
* Get the instance as an array. | ||
* | ||
* @return array<TKey, TValue> | ||
*/ | ||
public function toArray(): 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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the guanguans/notify. | ||
* | ||
* (c) guanguans <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled. | ||
*/ | ||
|
||
namespace Guanguans\Notify\Foundation\Contracts; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
|
||
interface Credential | ||
{ | ||
public function applyToRequest(RequestInterface $request): RequestInterface; | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the guanguans/notify. | ||
* | ||
* (c) guanguans <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled. | ||
*/ | ||
|
||
namespace Guanguans\Notify\Foundation\Contracts; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
|
||
interface HttpMessage extends Message | ||
{ | ||
public function toRequest(): RequestInterface; | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the guanguans/notify. | ||
* | ||
* (c) guanguans <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled. | ||
*/ | ||
|
||
namespace Guanguans\Notify\Foundation\Contracts; | ||
|
||
interface Jsonable | ||
{ | ||
/** | ||
* @return string|false | ||
*/ | ||
public function toJson(int $options = 0); | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the guanguans/notify. | ||
* | ||
* (c) guanguans <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled. | ||
*/ | ||
|
||
namespace Guanguans\Notify\Foundation\Contracts; | ||
|
||
interface Message extends \ArrayAccess, Arrayable, \Stringable, Jsonable | ||
{ | ||
} |
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the guanguans/notify. | ||
* | ||
* (c) guanguans <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled. | ||
*/ | ||
|
||
namespace Guanguans\Notify\Foundation; | ||
|
||
use Guanguans\Notify\Foundation\Concerns\Tappable; | ||
use Guanguans\Notify\Foundation\Contracts\Credential; | ||
use Guanguans\Notify\Foundation\Contracts\Message; | ||
use Http\Discovery\Psr18ClientDiscovery; | ||
use Psr\Http\Client\ClientExceptionInterface; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class HttpClient | ||
{ | ||
use Tappable; | ||
|
||
private Credential $credential; | ||
private ClientInterface $httpClient; | ||
|
||
public function __construct(Credential $credential = null, ClientInterface $httpClient = null) | ||
{ | ||
$this->credential = $credential ?? new NullCredential(); | ||
$this->httpClient = $httpClient ?? Psr18ClientDiscovery::find(); | ||
} | ||
|
||
/** | ||
* @throws ClientExceptionInterface | ||
*/ | ||
public function sendMessage(Message $message): ResponseInterface | ||
{ | ||
return $this->httpClient->sendRequest( | ||
$this->credential->applyToRequest($message->toRequest()) | ||
); | ||
} | ||
} |
Oops, something went wrong.