Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ResourceObjectの引数のcastについて #259

Closed
yuki777 opened this issue Jan 25, 2022 · 0 comments · Fixed by #260
Closed

ResourceObjectの引数のcastについて #259

yuki777 opened this issue Jan 25, 2022 · 0 comments · Fixed by #260

Comments

@yuki777
Copy link

yuki777 commented Jan 25, 2022

  • 質問: 下記のとおりですが、php bin/app.php get '/weekday?year=2001&month=1&day='のようなアクセスがBOTアクセスにより発生している環境がありまして、対処方法を検討しています。可能であれば400エラーになってくれるとありがたいですが、どのように対応するべきでしょうか?
  • https://bearsunday.github.io/manuals/1.0/ja/tutorial.html のコード参考にしています
$(cat << 'EOF' > src/Resource/App/Weekday.php
<?php

declare(strict_types=1);

namespace MyVendor\Weekday\Resource\App;

use BEAR\Resource\ResourceObject;
use DateTimeImmutable;

class Weekday extends ResourceObject
{
    public function onGet(int $year, int $month, int $day): static
    {
        $dateTime = DateTimeImmutable::createFromFormat('Y-m-d', "$year-$month-$day");
        $weekday = $dateTime->format('D');
        $this->body = ['weekday' => $weekday];

        return $this;
    }
}
EOF
)


# 200 OK: 想定通りです。問題ありません。
php bin/app.php get '/weekday?year=2001&month=1&day=1'

# 400 Bad Request: 想定通りです。問題ありません。
php bin/app.php get '/weekday?year=2001'

# 500 Internal Server Errorでなく、400 Bad Requestにしたいです
php bin/app.php get '/weekday?year=2001&month=1&day='
500 Internal Server Error
content-type: application/vnd.error+json

{
    "message": "Internal Server Error",
    "logref": "87cb1b89",
    "request": "get app://self/weekday?year=2001&month=1&day=",
    "exceptions": "ErrorException(MyVendor\\Weekday\\Resource\\App\\Weekday::onGet(): Argument #3 ($day) must be of type int, string given, called in /Users/adachi/tmp/MyVendor.Weekday/vendor/bear/resource/src/Invoker.php on line 40)",
    "file": "/Users/adachi/tmp/MyVendor.Weekday/src/Resource/App/Weekday.php(12)"
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant