Skip to content

Commit

Permalink
The author IP address and user agent is fetched from the $_SERVER v…
Browse files Browse the repository at this point in the history
…ariable
  • Loading branch information
cedx committed Mar 28, 2020
1 parent 2f4d201 commit bec218d
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 14 deletions.
3 changes: 1 addition & 2 deletions doc/features/comment_check.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ use Nyholm\Psr7\{Uri};

function main(): void {
try {
$author = new Author($_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']);
$comment = (new Comment($author))
$comment = (new Comment(new Author))
->setContent('A user comment')
->setType(CommentType::contactForm);

Expand Down
4 changes: 1 addition & 3 deletions doc/features/submit_ham.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ use Nyholm\Psr7\{Uri};

function main(): void {
try {
$author = new Author($_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']);
$comment = (new Comment($author))->setContent('A valid user comment (ham)');

$blog = new Blog(new Uri('https://www.yourblog.com'));
$client = new Client('123YourAPIKey', $blog);

$comment = (new Comment(new Author))->setContent('A valid user comment (ham)');
$result = $client->checkComment($comment);
// Got `CheckResult::isSpam`, but `CheckResult::isHam` expected.

Expand Down
4 changes: 1 addition & 3 deletions doc/features/submit_spam.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ use Nyholm\Psr7\{Uri};

function main(): void {
try {
$author = new Author($_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']);
$comment = (new Comment($author))->setContent('An invalid user comment (spam)');

$blog = new Blog(new Uri('https://www.yourblog.com'));
$client = new Client('123YourAPIKey', $blog);

$comment = (new Comment(new Author))->setContent('An invalid user comment (spam)');
$result = $client->checkComment($comment);
// Got `CheckResult::isHam`, but `CheckResult::isSpam` expected.

Expand Down
2 changes: 1 addition & 1 deletion example/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function main(): void {
echo $isValid ? 'The API key is valid' : 'The API key is invalid';

// Comment check.
$author = (new Author($_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']))
$author = (new Author)
->setEmail('[email protected]')
->setName('John Doe')
->setRole('guest');
Expand Down
10 changes: 5 additions & 5 deletions lib/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class Author implements \JsonSerializable {

/**
* Creates a new author.
* @param string $ipAddress The author's IP address.
* @param string $userAgent The author's user agent.
* @param string|null $ipAddress The author's IP address. Defaults to `$_SERVER['REMOTE_ADDR']`.
* @param string|null $userAgent The author's user agent. Defaults to `$_SERVER['HTTP_USER_AGENT']`.
*/
function __construct(string $ipAddress, string $userAgent) {
$this->ipAddress = $ipAddress;
$this->userAgent = $userAgent;
function __construct(?string $ipAddress = null, ?string $userAgent = null) {
$this->ipAddress = $ipAddress ?? ($_SERVER['REMOTE_ADDR'] ?: '');
$this->userAgent = $userAgent ?? ($_SERVER['HTTP_USER_AGENT'] ?: '');
}

/**
Expand Down

0 comments on commit bec218d

Please sign in to comment.