From 58ccfad60035843516b7883b4a3ff9e2b678f34f Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 6 Jan 2024 23:26:08 -0500 Subject: [PATCH 1/2] Add docs for casting functions Refs cakephp/cakephp#17295 --- en/appendices/5-1-upgrade-guide.rst | 7 +++++++ en/controllers/request-response.rst | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/en/appendices/5-1-upgrade-guide.rst b/en/appendices/5-1-upgrade-guide.rst index da96d27aa6..5deb064675 100644 --- a/en/appendices/5-1-upgrade-guide.rst +++ b/en/appendices/5-1-upgrade-guide.rst @@ -32,6 +32,13 @@ Console - ``Arguments::getBooleanOption()`` and ``Arguments::getMultipleOption()`` were added. +Core +---- + +- The ``toString``, ``toInt``, ``toBool`` were added. These methods give you + a typesafe way to cast request data or other input. Should casting not + succeed, ``null`` will be returned. + Http ---- diff --git a/en/controllers/request-response.rst b/en/controllers/request-response.rst index 47f1c5671c..c5ea1f9b7e 100644 --- a/en/controllers/request-response.rst +++ b/en/controllers/request-response.rst @@ -100,6 +100,25 @@ If you want to access all the query parameters you can use $query = $this->request->getQueryParams(); +You can use the casting utility functions to provide typesafe access to request +data and other input:: + + use function Cake\Core\toBool; + use function Cake\Core\toInt; + use function Cake\Core\toString; + + // $active is bool|null. + $active = toBool($this->request->getQuery('active')); + + // $page is int|null. + $page = toInt($this->request->getQuery('page')); + + // $query is string|null. + $query = toString($this->request->getQuery('query')); + +.. versionadded:: 5.1.0 + Casting functions were added. + Request Body Data ----------------- From 73b168c45f95a2747c3cf493d48a4db4a5d223d8 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 8 Jan 2024 20:57:06 -0500 Subject: [PATCH 2/2] Update en/appendices/5-1-upgrade-guide.rst Co-authored-by: othercorey --- en/appendices/5-1-upgrade-guide.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/en/appendices/5-1-upgrade-guide.rst b/en/appendices/5-1-upgrade-guide.rst index 5deb064675..4b0ddd8364 100644 --- a/en/appendices/5-1-upgrade-guide.rst +++ b/en/appendices/5-1-upgrade-guide.rst @@ -35,9 +35,8 @@ Console Core ---- -- The ``toString``, ``toInt``, ``toBool`` were added. These methods give you - a typesafe way to cast request data or other input. Should casting not - succeed, ``null`` will be returned. +- The ``toString``, ``toInt``, ``toBool`` functions were added. They give you + a typesafe way to cast request data or other input and return ``null`` when conversion fails. Http ----