Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

docs: replace types in View\Parser and View\Filters #6550

Merged
merged 9 commits into from
Sep 22, 2022
8 changes: 4 additions & 4 deletions system/View/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function capitalize(string $value): string
/**
* Formats a date into the given $format.
*
* @param mixed $value
* @param int|string|null $value
*/
public static function date($value, string $format): string
{
Expand All @@ -48,7 +48,7 @@ public static function date($value, string $format): string
* Example:
* my_date|date_modify(+1 day)
*
* @param string $value
* @param int|string|null $value
*
* @return false|int
*/
Expand All @@ -62,7 +62,7 @@ public static function date_modify($value, string $adjustment)
/**
* Returns the given default value if $value is empty or undefined.
*
* @param mixed $value
* @param array|bool|float|int|object|resource|string|null $value
*/
public static function default($value, string $default): string
{
Expand Down Expand Up @@ -208,7 +208,7 @@ public static function prose(string $value): string
* - ceil always rounds up
* - floor always rounds down
*
* @param mixed $precision
* @param int|string $precision
*
* @return float|string
*/
Expand Down
26 changes: 9 additions & 17 deletions system/View/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\View;

use CodeIgniter\Autoloader\FileLocator;
use CodeIgniter\View\Exceptions\ViewException;
use Config\View as ViewConfig;
use ParseError;
Expand Down Expand Up @@ -72,10 +73,7 @@ class Parser extends View
/**
* Constructor
*
* @param string $viewPath
* @param mixed $loader
* @param bool $debug
* @param LoggerInterface $logger
* @param FileLocator|null $loader
*/
public function __construct(ViewConfig $config, ?string $viewPath = null, $loader = null, ?bool $debug = null, ?LoggerInterface $logger = null)
{
Expand All @@ -90,9 +88,6 @@ public function __construct(ViewConfig $config, ?string $viewPath = null, $loade
*
* Parses pseudo-variables contained in the specified template view,
* replacing them with any data that has already been set.
*
* @param array $options
* @param bool $saveData
*/
public function render(string $view, ?array $options = null, ?bool $saveData = null): string
{
Expand Down Expand Up @@ -153,9 +148,6 @@ public function render(string $view, ?array $options = null, ?bool $saveData = n
*
* Parses pseudo-variables contained in the specified string,
* replacing them with any data that has already been set.
*
* @param array $options
* @param bool $saveData
*/
public function renderString(string $template, ?array $options = null, ?bool $saveData = null): string
{
Expand Down Expand Up @@ -187,8 +179,8 @@ public function renderString(string $template, ?array $options = null, ?bool $sa
* so that the variable is correctly handled within the
* parsing itself, and contexts (including raw) are respected.
*
* @param string $context The context to escape it for: html, css, js, url, raw
* If 'raw', no escaping will happen
* @param string|null $context The context to escape it for: html, css, js, url, raw
* If 'raw', no escaping will happen
*/
public function setData(array $data = [], ?string $context = null): RendererInterface
{
Expand Down Expand Up @@ -503,9 +495,9 @@ public function setConditionalDelimiters($leftDelimiter = '{', $rightDelimiter =
* Handles replacing a pseudo-variable with the actual content. Will double-check
* for escaping brackets.
*
* @param mixed $pattern
* @param string $content
* @param string $template
* @param array|string $pattern
* @param string $content
* @param string $template
*/
protected function replaceSingle($pattern, $content, $template, bool $escape = false): string
{
Expand Down Expand Up @@ -698,9 +690,9 @@ public function removePlugin(string $alias)
* Converts an object to an array, respecting any
* toArray() methods on an object.
*
* @param mixed $value
* @param array|bool|float|int|object|string|null $value
*
* @return mixed
* @return array|bool|float|int|string|null
*/
protected function objectToArray($value)
{
Expand Down
65 changes: 65 additions & 0 deletions tests/system/View/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,71 @@ public function testParseLoopObjectProperties()
$this->assertSame("Super Heroes\nTom Dick Henry ", $this->parser->renderString($template));
}

public function testParseIntegerPositive()
{
$data = [
'title' => 'Count:',
'amount' => 5,
];

$template = '{title} {amount}';

$this->parser->setData($data, 'html');
$this->assertSame('Count: 5', $this->parser->renderString($template));
}

public function testParseIntegerNegative()
{
$data = [
'title' => 'Count:',
'amount' => -5,
];

$template = '{title} {amount}';

$this->parser->setData($data, 'html');
$this->assertSame('Count: -5', $this->parser->renderString($template));
}

public function testParseFloatPositive()
{
$data = [
'title' => 'Rate:',
'amount' => 5.5,
];

$template = '{title} {amount}';

$this->parser->setData($data, 'html');
$this->assertSame('Rate: 5.5', $this->parser->renderString($template));
}

public function testParseFloatNegative()
{
$data = [
'title' => 'Rate:',
'amount' => -5.5,
];

$template = '{title} {amount}';

$this->parser->setData($data, 'html');
$this->assertSame('Rate: -5.5', $this->parser->renderString($template));
}

public function testParseNull()
{
$data = [
'title' => 'Ticks:',
'amount' => null,
];

$template = '{title} {amount}';

$this->parser->setData($data, 'html');
$this->assertSame('Ticks: ', $this->parser->renderString($template));
}

public function testParseLoopEntityProperties()
{
$power = new class () extends Entity {
Expand Down