Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into 4.6
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jun 5, 2024
2 parents ef00c11 + 0ce62d9 commit d883f86
Show file tree
Hide file tree
Showing 68 changed files with 144 additions and 109 deletions.
3 changes: 2 additions & 1 deletion system/Helpers/number_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ function number_to_amount($num, int $precision = 0, ?string $locale = null)
// Strip any formatting & ensure numeric input
try {
// @phpstan-ignore-next-line
$num = 0 + str_replace(',', '', $num);
$num = 0 + str_replace(',', '', (string) $num);
} catch (ErrorException) {
// Catch "Warning: A non-numeric value encountered"
return false;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/system/Helpers/NumberHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function testRomanNumber(): void
$this->assertSame('X', number_to_roman(10));
}

public function testRomanNumberString(): void
{
$this->assertSame('XCVI', number_to_roman('96'));
}

public function testRomanNumberRange(): void
{
$this->assertNull(number_to_roman(-1));
Expand Down Expand Up @@ -70,6 +75,11 @@ public function testNumberToSize(): void
$this->assertSame('456 Bytes', number_to_size(456, 1, 'en_US'));
}

public function testNumberToSizeString(): void
{
$this->assertSame('456 Bytes', number_to_size('456', 1, 'en_US'));
}

public function testKbFormat(): void
{
$this->assertSame('4.5 KB', number_to_size(4567, 1, 'en_US'));
Expand Down Expand Up @@ -109,6 +119,11 @@ public function testThousands(): void
$this->assertSame('1,000 thousand', number_to_amount('999999', 0, 'en_US'));
}

public function testThousandsInt(): void
{
$this->assertSame('123 thousand', number_to_amount(123000, 0, 'en_US'));
}

public function testMillions(): void
{
$this->assertSame('123.4 million', number_to_amount('123,400,000', 1, 'en_US'));
Expand Down
8 changes: 5 additions & 3 deletions user_guide_src/source/concepts/services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ come in handy.
Instead of creating the instance ourself, we let a central class create an instance of the
class for us. This class is kept very simple. It only contains a method for each class that we want
to use as a service. The method typically returns a **shared instance** of that class, passing any dependencies
it might have into it. Then, we would replace our timer creation code with code that calls this new class:
it might have into it. Then, we would replace our timer creation code with code that calls this global function or Services class:

.. literalinclude:: services/002.php

Expand All @@ -55,7 +55,7 @@ As many CodeIgniter classes are provided as services, you can get them like the

.. literalinclude:: services/013.php

The ``$typography`` is an instance of the Typography class, and if you call ``\Config\Services::typography()`` again, you will get the exactly same instance.
The ``$timer`` is an instance of the Timer class, and if you call ``service('timer')`` again, you will get the exactly same instance.

The Services typically return a **shared instance** of the class. The following code creates a ``CURLRequest`` instance at the first call. And the second call returns the exactly same instance.

Expand All @@ -66,7 +66,7 @@ Therefore, the parameter ``$options2`` for the ``$client2`` does not work. It is
Getting a New Instance
======================

If you want to get a new instance of the Typography class, you need to pass ``false`` to the argument ``$getShared``:
If you want to get a new instance of the Timer class, you need to pass ``false`` to the argument ``$getShared``:

.. literalinclude:: services/014.php

Expand All @@ -85,6 +85,8 @@ always return the same instance:

.. literalinclude:: services/003.php

.. note:: Since v4.5.0, when you don't pass parameters to the service, the global function ``service()`` is recommended due to performance improvements.

If the creation method requires additional parameters, they can be passed after the service name:

.. literalinclude:: services/004.php
Expand Down
3 changes: 3 additions & 0 deletions user_guide_src/source/concepts/services/002.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<?php

$timer = service('timer');

// The code above is the same as the code below.
$timer = \Config\Services::timer();
2 changes: 1 addition & 1 deletion user_guide_src/source/concepts/services/012.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

$postManager = \Config\Services::postManager();
$postManager = service('postManager');
2 changes: 1 addition & 1 deletion user_guide_src/source/concepts/services/013.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

$typography = \Config\Services::typography();
$timer = service('timer');
2 changes: 1 addition & 1 deletion user_guide_src/source/concepts/services/014.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

$typography = \Config\Services::typography(false);
$timer = \Config\Services::timer(false);
4 changes: 2 additions & 2 deletions user_guide_src/source/concepts/services/015.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
'baseURI' => 'http://example.com/api/v1/',
'timeout' => 3,
];
$client1 = \Config\Services::curlrequest($options1);
$client1 = service('curlrequest', $options1);

$options2 = [
'baseURI' => 'http://another.example.com/api/v2/',
'timeout' => 10,
];
$client2 = \Config\Services::curlrequest($options2);
$client2 = service('curlrequest', $options2);
// $options2 does not work.
// $client2 is the exactly same instance as $client1.
2 changes: 1 addition & 1 deletion user_guide_src/source/extending/basecontroller/003.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public function initController(/* ... */)
// Do Not Edit This Line
parent::initController($request, $response, $logger);

$this->session = \Config\Services::session();
$this->session = service('session');
}
}
4 changes: 2 additions & 2 deletions user_guide_src/source/general/common_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ Miscellaneous Functions
:returns: The shared Request object.
:rtype: IncomingRequest|CLIRequest

This function is a wrapper for ``Services::request()``.
This function is a wrapper for ``Services::request()`` and ``service('request')``.

.. php:function:: response()
Expand All @@ -381,7 +381,7 @@ Miscellaneous Functions
:returns: The shared Response object.
:rtype: Response

This function is a wrapper for ``Services::response()``.
This function is a wrapper for ``Services::response()`` and ``service('response')``.

.. php:function:: route_to($method[, ...$params])
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/general/errors/018.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$response = \Config\Services::response()
$response = service('response')
->redirect('https://example.com/path')
->setHeader('Some', 'header')
->setCookie('and', 'cookie');
Expand Down
47 changes: 26 additions & 21 deletions user_guide_src/source/helpers/form_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Escaping Field Values
*********************

You may need to use HTML and characters such as quotes within your form
elements. In order to do that safely, you'll need to use
elements. In order to do that safely, you'll need to use the
:doc:`common function <../general/common_functions>`
:php:func:`esc()`.

Expand Down Expand Up @@ -64,8 +64,8 @@ The following functions are available:

Creates an opening form tag with a site URL **built from your** ``Config\App::$baseURL``.
It will optionally let you add form attributes and hidden input fields, and
will always add the `accept-charset` attribute based on the charset value in your
config file.
will always add the `accept-charset` attribute based on the ``$charset`` property in your
**app/Config/App.php** config file.

The main benefit of using this tag rather than hard coding your own HTML is that
it permits your site to be more portable in the event your URLs ever change.
Expand Down Expand Up @@ -103,16 +103,21 @@ The following functions are available:

<form action="http://example.com/index.php/email/send" class="email" id="myform" method="post" accept-charset="utf-8">

If :ref:`CSRF <cross-site-request-forgery>` filter is turned on ``form_open()`` will generate CSRF field at the beginning of the form. You can specify ID of this field by passing csrf_id as one of the ``$attribute`` array:
If :ref:`CSRF <cross-site-request-forgery>` filter is turned on ``form_open()`` will generate CSRF field at the beginning of the form. You can specify ID of this field by passing **csrf_id** as an element of the ``$attributes`` array:

.. literalinclude:: form_helper/007.php

will return::

<form action="http://example.com/index.php/u/sign-up" method="post" accept-charset="utf-8">
<input type="hidden" id="my-id" name="csrf_field" value="964ede6e0ae8a680f7b8eab69136717d">
<input type="hidden" id="my-id" name="csrf_test_name" value="964ede6e0ae8a680f7b8eab69136717d">

.. note:: To use auto-generation of CSRF field, you need to turn CSRF filter on to the form page. In most cases it is requested using the ``GET`` method.
.. note:: To use auto-generation of CSRF field, you need to turn on the :ref:`CSRF filter <enable-csrf-protection>` in **app/Config/Filters.php** file.
In most cases the form page is requested using the GET method. Normally, CSRF protection is required
for POST/PUT/DELETE/PATCH requests, but even for GET requests, CSRF filters must be enabled for pages that display Forms.

If you enable CSRF filter with :ref:`filters-globals`, it will be active for all request types.
But if you enable CSRF filter with ``public array $methods = ['POST' => ['csrf']];``, the hidden CSRF field will not be added in GET requests.

**Adding Hidden Input Fields**

Expand Down Expand Up @@ -145,7 +150,7 @@ The following functions are available:
:param string $name: Field name
:param string $value: Field value
:returns: An HTML hidden input field tag
:returns: An HTML hidden input element
:rtype: string

Lets you generate hidden input fields. You can either submit a
Expand All @@ -171,7 +176,7 @@ The following functions are available:
:param string $value: Field value
:param mixed $extra: Extra attributes to be added to the tag either as an array or a literal string
:param string $type: The type of input field. i.e., 'text', 'email', 'number', etc.
:returns: An HTML text input field tag
:returns: An HTML text input element
:rtype: string

Lets you generate a standard text input field. You can minimally pass
Expand Down Expand Up @@ -206,7 +211,7 @@ The following functions are available:
:param array $data: Field attributes data
:param string $value: Field value
:param mixed $extra: Extra attributes to be added to the tag either as an array or a literal string
:returns: An HTML password input field tag
:returns: An HTML password input element
:rtype: string

This function is identical in all respects to the :php:func:`form_input()`
Expand All @@ -217,7 +222,7 @@ The following functions are available:
:param array $data: Field attributes data
:param string $value: Field value
:param mixed $extra: Extra attributes to be added to the tag either as an array or a literal string
:returns: An HTML file upload input field tag
:returns: An HTML file upload input element
:rtype: string

This function is identical in all respects to the :php:func:`form_input()`
Expand All @@ -229,7 +234,7 @@ The following functions are available:
:param array $data: Field attributes data
:param string $value: Field value
:param mixed $extra: Extra attributes to be added to the tag either as an array or a literal string
:returns: An HTML textarea tag
:returns: An HTML textarea element
:rtype: string

This function is identical in all respects to the :php:func:`form_input()`
Expand All @@ -244,7 +249,7 @@ The following functions are available:
:param array $options: An associative array of options to be listed
:param array $selected: List of fields to mark with the *selected* attribute
:param mixed $extra: Extra attributes to be added to the tag either as an array or a literal string
:returns: An HTML dropdown select field tag
:returns: An HTML select (dropdown) element
:rtype: string

Lets you create a standard drop-down field. The first parameter will
Expand Down Expand Up @@ -278,7 +283,7 @@ The following functions are available:
:param array $options: An associative array of options to be listed
:param array $selected: List of fields to mark with the *selected* attribute
:param mixed $extra: Extra attributes to be added to the tag either as an array or a literal string
:returns: An HTML dropdown multiselect field tag
:returns: An HTML select element with multiple attribute
:rtype: string

Lets you create a standard multiselect field. The first parameter will
Expand Down Expand Up @@ -326,7 +331,7 @@ The following functions are available:
:param string $value: Field value
:param bool $checked: Whether to mark the checkbox as being *checked*
:param mixed $extra: Extra attributes to be added to the tag either as an array or a literal string
:returns: An HTML checkbox input tag
:returns: An HTML checkbox input element
:rtype: string

Lets you generate a checkbox field. Simple example:
Expand Down Expand Up @@ -357,7 +362,7 @@ The following functions are available:
:param string $value: Field value
:param bool $checked: Whether to mark the radio button as being *checked*
:param mixed $extra: Extra attributes to be added to the tag either as an array or a literal string
:returns: An HTML radio input tag
:returns: An HTML radio input element
:rtype: string

This function is identical in all respects to the :php:func:`form_checkbox()`
Expand All @@ -368,7 +373,7 @@ The following functions are available:
:param string $label_text: Text to put in the <label> tag
:param string $id: ID of the form element that we're making a label for
:param string $attributes: HTML attributes
:returns: An HTML field label tag
:returns: An HTML label element
:rtype: string

Lets you generate a <label>. Simple example:
Expand All @@ -387,7 +392,7 @@ The following functions are available:
:param string $data: Button name
:param string $value: Button value
:param mixed $extra: Extra attributes to be added to the tag either as an array or a literal string
:returns: An HTML input submit tag
:returns: An HTML input submit element
:rtype: string

Lets you generate a standard submit button. Simple example:
Expand All @@ -403,7 +408,7 @@ The following functions are available:
:param string $data: Button name
:param string $value: Button value
:param mixed $extra: Extra attributes to be added to the tag either as an array or a literal string
:returns: An HTML input reset button tag
:returns: An HTML input reset element
:rtype: string

Lets you generate a standard reset button. Use is identical to
Expand All @@ -414,7 +419,7 @@ The following functions are available:
:param string $data: Button name
:param string $content: Button label
:param mixed $extra: Extra attributes to be added to the tag either as an array or a literal string
:returns: An HTML button tag
:returns: An HTML button element
:rtype: string

Lets you generate a standard button element. You can minimally pass the
Expand Down Expand Up @@ -452,10 +457,10 @@ The following functions are available:
:returns: Field value
:rtype: string

Permits you to set the value of an input form or textarea. You must
Permits you to set the value of an input or textarea element. You must
supply the field name via the first parameter of the function. The
second (optional) parameter allows you to set a default value for the
form. The third (optional) parameter allows you to turn off HTML escaping
field value. The third (optional) parameter allows you to turn off HTML escaping
of the value, in case you need to use this function in combination with
i.e., :php:func:`form_input()` and avoid double-escaping.

Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/incoming/content_negotiation/001.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

$negotiate = \Config\Services::negotiator();
$negotiate = service('negotiator');
2 changes: 2 additions & 0 deletions user_guide_src/source/incoming/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ Filters can be specified by adding their alias to either the ``before`` or ``aft

.. literalinclude:: filters/013.php

.. _filters-globals:

$globals
--------

Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/incoming/incomingrequest/002.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

$request = \Config\Services::request();
$request = service('request');
2 changes: 1 addition & 1 deletion user_guide_src/source/incoming/incomingrequest/003.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public function __construct(RequestInterface $request)
}
}

$someClass = new SomeClass(\Config\Services::request());
$someClass = new SomeClass(service('request'));
2 changes: 1 addition & 1 deletion user_guide_src/source/libraries/caching.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The following example shows a common usage pattern within your controllers.

.. literalinclude:: caching/001.php

You can grab an instance of the cache engine directly through the Services class:
You can grab an instance of the cache engine directly through the global function ``service()``:

.. literalinclude:: caching/002.php

Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/libraries/caching/002.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

$cache = \Config\Services::cache();
$cache = service('cache');

$foo = $cache->get('foo');
2 changes: 1 addition & 1 deletion user_guide_src/source/libraries/cookies/002.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Config\Cookie as CookieConfig;

// pass in a Config\Cookie instance before constructing a Cookie class
Cookie::setDefaults(new CookieConfig());
Cookie::setDefaults(config(CookieConfig::class));
$cookie = new Cookie('login_token');

// pass in an array of defaults
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/libraries/cookies/003.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use CodeIgniter\Cookie\Cookie;
use Config\Cookie as CookieConfig;

$oldDefaults = Cookie::setDefaults(new CookieConfig());
$oldDefaults = Cookie::setDefaults(config(CookieConfig::class));
$cookie = new Cookie('my_token', 'muffins');

// return the old defaults
Expand Down
4 changes: 1 addition & 3 deletions user_guide_src/source/libraries/cookies/007.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<?php

use Config\Services;

$cookieStore = Services::response()->getCookieStore();
$cookieStore = service('response')->getCookieStore();
Loading

0 comments on commit d883f86

Please sign in to comment.