Document clamp function#4814
Conversation
b4c8f1f to
ddab889
Compare
ddab889 to
19f924c
Compare
19f924c to
1b1ac7b
Compare
|
LGTM, clear, consistent with the RFC, the implementation, and the tests. Thanks for adding this @kylekatarnls |
This PR was merged into the 1.x branch. Discussion ---------- [8.6] Add `clamp` function RFC: https://wiki.php.net/rfc/clamp_v2 Original implementation: php/php-src#19434 Documentation: php/doc-en#4814 Commits ------- 2b4c47d [8.6] Add `clamp` function
jordikroon
left a comment
There was a problem hiding this comment.
We cannot merge this yet as 8.6 isn't released yet.
Mostly doc style comments.
Since date comparison is a big part of this RFC. We should document this explicitly here as well. Not only within the examples section.
| @@ -0,0 +1,34 @@ | |||
| <?xml version="1.0" encoding="utf-8"?> | |||
There was a problem hiding this comment.
Please don't add file this (yet). While I appreciate your thought, we will have to create migration guides anyway. This would ultimately create a merge conflict when the time is there. And then it would be more easy to merge this.
| <para> | ||
| Return the given value if in range of <parameter>min</parameter> and <parameter>max</parameter>. | ||
| Else if it's lower than <parameter>min</parameter>, return <parameter>min</parameter>. | ||
| Else return <parameter>max</parameter>. | ||
| </para> |
There was a problem hiding this comment.
| <para> | |
| Return the given value if in range of <parameter>min</parameter> and <parameter>max</parameter>. | |
| Else if it's lower than <parameter>min</parameter>, return <parameter>min</parameter>. | |
| Else return <parameter>max</parameter>. | |
| </para> | |
| <simpara> | |
| Return the given value if in range of <parameter>min</parameter> and <parameter>max</parameter>. | |
| Else if it's lower than <parameter>min</parameter>, return <parameter>min</parameter>. | |
| Else return <parameter>max</parameter>. | |
| </simpara> |
| <para> | ||
| Values of different types will be compared using the <link linkend="language.operators.comparison"> | ||
| standard comparison rules</link>. For instance, a non-numeric <type>string</type> will be | ||
| compared to an <type>int</type> as though it were <literal>0</literal>, but multiple non-numeric | ||
| <type>string</type> values will be compared alphanumerically. The actual value returned will be of the | ||
| original type with no conversion applied. | ||
| </para> |
There was a problem hiding this comment.
| <para> | |
| Values of different types will be compared using the <link linkend="language.operators.comparison"> | |
| standard comparison rules</link>. For instance, a non-numeric <type>string</type> will be | |
| compared to an <type>int</type> as though it were <literal>0</literal>, but multiple non-numeric | |
| <type>string</type> values will be compared alphanumerically. The actual value returned will be of the | |
| original type with no conversion applied. | |
| </para> | |
| <simpara> | |
| Values of different types will be compared using the <link linkend="language.operators.comparison"> | |
| standard comparison rules</link>. For instance, a non-numeric <type>string</type> will be | |
| compared to an <type>int</type> as though it were <literal>0</literal>, but multiple non-numeric | |
| <type>string</type> values will be compared alphanumerically. The actual value returned will be of the | |
| original type with no conversion applied. | |
| </simpara> |
| </refsect1> | ||
| <refsect1 role="parameters"> | ||
| &reftitle.parameters; | ||
| <para> |
There was a problem hiding this comment.
<variablelist> doesn't need to be wrapped in <para>. Could you remove this container?
| <para> | ||
| Any <link linkend="language.operators.comparison">comparable</link> | ||
| value to be clamped between <parameter>min</parameter> and <parameter>max</parameter>. | ||
| </para> |
There was a problem hiding this comment.
| <para> | |
| Any <link linkend="language.operators.comparison">comparable</link> | |
| value to be clamped between <parameter>min</parameter> and <parameter>max</parameter>. | |
| </para> | |
| <simpara> | |
| Any <link linkend="language.operators.comparison">comparable</link> | |
| value to be clamped between <parameter>min</parameter> and <parameter>max</parameter>. | |
| </simpara> |
| <para> | ||
| If <parameter>value</parameter> is <constant>NAN</constant>, then the returned value will also be | ||
| <constant>NAN</constant>. | ||
| </para> |
There was a problem hiding this comment.
| <para> | |
| If <parameter>value</parameter> is <constant>NAN</constant>, then the returned value will also be | |
| <constant>NAN</constant>. | |
| </para> | |
| <simpara> | |
| If <parameter>value</parameter> is <constant>NAN</constant>, then the returned value will also be | |
| <constant>NAN</constant>. | |
| </simpara> |
| <para> | ||
| If <parameter>min</parameter> is greater than <parameter>max</parameter>, | ||
| <function>clamp</function> throws a <classname>ValueError</classname>. | ||
| </para> |
There was a problem hiding this comment.
| <para> | |
| If <parameter>min</parameter> is greater than <parameter>max</parameter>, | |
| <function>clamp</function> throws a <classname>ValueError</classname>. | |
| </para> | |
| <simpara> | |
| If <parameter>min</parameter> is greater than <parameter>max</parameter>, | |
| <function>clamp</function> throws a <exceptionname>ValueError</exceptionname>. | |
| </simpara> |
| <para> | ||
| If <parameter>min</parameter> or <parameter>max</parameter> is <constant>NAN</constant>, | ||
| <function>clamp</function> throws a <classname>ValueError</classname>. | ||
| </para> |
There was a problem hiding this comment.
| <para> | |
| If <parameter>min</parameter> or <parameter>max</parameter> is <constant>NAN</constant>, | |
| <function>clamp</function> throws a <classname>ValueError</classname>. | |
| </para> | |
| <simpara> | |
| If <parameter>min</parameter> or <parameter>max</parameter> is <constant>NAN</constant>, | |
| <function>clamp</function> throws a <exceptionname>ValueError</exceptionname>. | |
| </simpara> |
| <para> | ||
| <example> | ||
| <title>Example uses of <function>clamp</function></title> | ||
| <programlisting role="php"> | ||
| <![CDATA[ | ||
| <?php | ||
| echo clamp(-5, min: 0, max: 100), PHP_EOL; // 0 | ||
| echo clamp(55, min: 0, max: 100), PHP_EOL; // 55 | ||
| echo clamp(103, min: 0, max: 100), PHP_EOL; // 100 | ||
|
|
||
| echo clamp("J", min: "A", max: "F"), PHP_EOL; // "F" | ||
|
|
||
| clamp( | ||
| new \DateTimeImmutable('2025-08-01'), | ||
| min: new \DateTimeImmutable('2025-08-15'), | ||
| max: new \DateTimeImmutable('2025-09-15'), | ||
| )->format('Y-m-d'), PHP_EOL; // 2025-08-15 | ||
| ?> | ||
| ]]> | ||
| </programlisting> | ||
| </example> | ||
| </para> |
There was a problem hiding this comment.
| <para> | |
| <example> | |
| <title>Example uses of <function>clamp</function></title> | |
| <programlisting role="php"> | |
| <![CDATA[ | |
| <?php | |
| echo clamp(-5, min: 0, max: 100), PHP_EOL; // 0 | |
| echo clamp(55, min: 0, max: 100), PHP_EOL; // 55 | |
| echo clamp(103, min: 0, max: 100), PHP_EOL; // 100 | |
| echo clamp("J", min: "A", max: "F"), PHP_EOL; // "F" | |
| clamp( | |
| new \DateTimeImmutable('2025-08-01'), | |
| min: new \DateTimeImmutable('2025-08-15'), | |
| max: new \DateTimeImmutable('2025-09-15'), | |
| )->format('Y-m-d'), PHP_EOL; // 2025-08-15 | |
| ?> | |
| ]]> | |
| </programlisting> | |
| </example> | |
| </para> | |
| <informalexample> | |
| <programlisting role="php"> | |
| <![CDATA[ | |
| <?php | |
| echo clamp(-5, min: 0, max: 100), PHP_EOL; // 0 | |
| echo clamp(55, min: 0, max: 100), PHP_EOL; // 55 | |
| echo clamp(103, min: 0, max: 100), PHP_EOL; // 100 | |
| echo clamp("J", min: "A", max: "F"), PHP_EOL; // "F" | |
| clamp( | |
| new \DateTimeImmutable('2025-08-01'), | |
| min: new \DateTimeImmutable('2025-08-15'), | |
| max: new \DateTimeImmutable('2025-09-15'), | |
| )->format('Y-m-d'), PHP_EOL; // 2025-08-15 | |
| ?> | |
| ]]> | |
| </programlisting> | |
| </informalexample> |
| </refsect1> | ||
| <refsect1 role="seealso"> | ||
| &reftitle.seealso; | ||
| <para> |
There was a problem hiding this comment.
Could you remove this outer <para>?
See php/php-src#19434