Skip to content

Commit 9b3464d

Browse files
committed
Split Endpoint into Trait and Interface
1 parent c59d716 commit 9b3464d

File tree

3 files changed

+157
-112
lines changed

3 files changed

+157
-112
lines changed

src/Discord/Endpoint.php

Lines changed: 4 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Discord\Http;
1313

14-
class Endpoint
14+
class Endpoint implements EndpointInterface
1515
{
16+
use EndpointTrait;
17+
1618
// GET
1719
public const GATEWAY = 'gateway';
1820
// GET
@@ -98,7 +100,7 @@ class Endpoint
98100
// GET
99101
public const CHANNEL_THREADS_ARCHIVED_PRIVATE_ME = self::CHANNEL.'/users/@me/threads/archived/private';
100102
// POST
101-
public const CHANNEL_SEND_SOUNDBOARD_SOUND = self::CHANNEL . '/send-soundboard-sound';
103+
public const CHANNEL_SEND_SOUNDBOARD_SOUND = self::CHANNEL.'/send-soundboard-sound';
102104

103105
// GET, PATCH, DELETE
104106
public const THREAD = 'channels/:thread_id';
@@ -344,114 +346,4 @@ public function __construct(string $endpoint)
344346
$this->vars = $vars[1] ?? [];
345347
}
346348
}
347-
348-
/**
349-
* Binds a list of arguments to the endpoint.
350-
*
351-
* @param string[] ...$args
352-
* @return this
353-
*/
354-
public function bindArgs(...$args): self
355-
{
356-
for ($i = 0; $i < count($this->vars) && $i < count($args); $i++) {
357-
$this->args[$this->vars[$i]] = $args[$i];
358-
}
359-
360-
return $this;
361-
}
362-
363-
/**
364-
* Binds an associative array to the endpoint.
365-
*
366-
* @param string[] $args
367-
* @return this
368-
*/
369-
public function bindAssoc(array $args): self
370-
{
371-
$this->args = array_merge($this->args, $args);
372-
373-
return $this;
374-
}
375-
376-
/**
377-
* Adds a key-value query pair to the endpoint.
378-
*
379-
* @param string $key
380-
* @param string|bool $value
381-
*/
382-
public function addQuery(string $key, $value): void
383-
{
384-
if (! is_bool($value)) {
385-
$value = (string) $value;
386-
}
387-
388-
$this->query[$key] = $value;
389-
}
390-
391-
/**
392-
* Converts the endpoint into the absolute endpoint with
393-
* placeholders replaced.
394-
*
395-
* Passing a true boolean in will only replace the major parameters.
396-
* Used for rate limit buckets.
397-
*
398-
* @param bool $onlyMajorParameters
399-
* @return string
400-
*/
401-
public function toAbsoluteEndpoint(bool $onlyMajorParameters = false): string
402-
{
403-
$endpoint = $this->endpoint;
404-
405-
foreach ($this->vars as $var) {
406-
if (! isset($this->args[$var]) || ($onlyMajorParameters && ! $this->isMajorParameter($var))) {
407-
continue;
408-
}
409-
410-
$endpoint = str_replace(":{$var}", $this->args[$var], $endpoint);
411-
}
412-
413-
if (! $onlyMajorParameters && count($this->query) > 0) {
414-
$endpoint .= '?'.http_build_query($this->query);
415-
}
416-
417-
return $endpoint;
418-
}
419-
420-
/**
421-
* Converts the endpoint to a string.
422-
* Alias of ->toAbsoluteEndpoint();.
423-
*
424-
* @return string
425-
*/
426-
public function __toString(): string
427-
{
428-
return $this->toAbsoluteEndpoint();
429-
}
430-
431-
/**
432-
* Creates an endpoint class and binds arguments to
433-
* the newly created instance.
434-
*
435-
* @param string $endpoint
436-
* @param string[] $args
437-
* @return Endpoint
438-
*/
439-
public static function bind(string $endpoint, ...$args)
440-
{
441-
$endpoint = new Endpoint($endpoint);
442-
$endpoint->bindArgs(...$args);
443-
444-
return $endpoint;
445-
}
446-
447-
/**
448-
* Checks if a parameter is a major parameter.
449-
*
450-
* @param string $param
451-
* @return bool
452-
*/
453-
private static function isMajorParameter(string $param): bool
454-
{
455-
return in_array($param, self::MAJOR_PARAMETERS);
456-
}
457349
}

src/Discord/EndpointInterface.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of the DiscordPHP-Http project.
5+
*
6+
* Copyright (c) 2021-present David Cole <david.cole1340@gmail.com>
7+
*
8+
* This file is subject to the MIT license that is bundled
9+
* with this source code in the LICENSE file.
10+
*/
11+
12+
namespace Discord\Http;
13+
14+
interface EndpointInterface
15+
{
16+
public function bindArgs(...$args): self;
17+
public function bindAssoc(array $args): self;
18+
public function addQuery(string $key, $value): void;
19+
public function toAbsoluteEndpoint(bool $onlyMajorParameters = false): string;
20+
public function __toString(): string;
21+
public static function bind(string $endpoint, ...$args);
22+
}

src/Discord/EndpointTrait.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of the DiscordPHP-Http project.
5+
*
6+
* Copyright (c) 2021-present David Cole <david.cole1340@gmail.com>
7+
*
8+
* This file is subject to the MIT license that is bundled
9+
* with this source code in the LICENSE file.
10+
*/
11+
12+
namespace Discord\Http;
13+
14+
trait EndpointTrait
15+
{
16+
/**
17+
* Binds a list of arguments to the endpoint.
18+
*
19+
* @param string[] ...$args
20+
* @return this
21+
*/
22+
public function bindArgs(...$args): self
23+
{
24+
for ($i = 0; $i < count($this->vars) && $i < count($args); $i++) {
25+
$this->args[$this->vars[$i]] = $args[$i];
26+
}
27+
28+
return $this;
29+
}
30+
31+
/**
32+
* Binds an associative array to the endpoint.
33+
*
34+
* @param string[] $args
35+
* @return this
36+
*/
37+
public function bindAssoc(array $args): self
38+
{
39+
$this->args = array_merge($this->args, $args);
40+
41+
return $this;
42+
}
43+
44+
/**
45+
* Adds a key-value query pair to the endpoint.
46+
*
47+
* @param string $key
48+
* @param string|bool $value
49+
*/
50+
public function addQuery(string $key, $value): void
51+
{
52+
if (! is_bool($value)) {
53+
$value = (string) $value;
54+
}
55+
56+
$this->query[$key] = $value;
57+
}
58+
59+
/**
60+
* Converts the endpoint into the absolute endpoint with
61+
* placeholders replaced.
62+
*
63+
* Passing a true boolean in will only replace the major parameters.
64+
* Used for rate limit buckets.
65+
*
66+
* @param bool $onlyMajorParameters
67+
* @return string
68+
*/
69+
public function toAbsoluteEndpoint(bool $onlyMajorParameters = false): string
70+
{
71+
$endpoint = $this->endpoint;
72+
73+
foreach ($this->vars as $var) {
74+
if (
75+
! isset($this->args[$var]) ||
76+
(
77+
$onlyMajorParameters &&
78+
(method_exists($this, 'isMajorParameter') ? ! $this->isMajorParameter($var) : false)
79+
)
80+
) {
81+
continue;
82+
}
83+
84+
$endpoint = str_replace(":{$var}", $this->args[$var], $endpoint);
85+
}
86+
87+
if (! $onlyMajorParameters && count($this->query) > 0) {
88+
$endpoint .= '?'.http_build_query($this->query);
89+
}
90+
91+
return $endpoint;
92+
}
93+
94+
/**
95+
* Converts the endpoint to a string.
96+
* Alias of ->toAbsoluteEndpoint();.
97+
*
98+
* @return string
99+
*/
100+
public function __toString(): string
101+
{
102+
return $this->toAbsoluteEndpoint();
103+
}
104+
105+
/**
106+
* Creates an endpoint class and binds arguments to
107+
* the newly created instance.
108+
*
109+
* @param string $endpoint
110+
* @param string[] $args
111+
* @return Endpoint
112+
*/
113+
public static function bind(string $endpoint, ...$args)
114+
{
115+
$endpoint = new Endpoint($endpoint);
116+
$endpoint->bindArgs(...$args);
117+
118+
return $endpoint;
119+
}
120+
121+
/**
122+
* Checks if a parameter is a major parameter.
123+
*
124+
* @param string $param
125+
* @return bool
126+
*/
127+
private static function isMajorParameter(string $param): bool
128+
{
129+
return in_array($param, Endpoint::MAJOR_PARAMETERS);
130+
}
131+
}

0 commit comments

Comments
 (0)