Last active 1740030098

For those who like perversions

php

anthony's Avatar anthony revised this gist 1740030098. Go to revision

2 files changed, 117 insertions

coalesce.php(file created)

@@ -0,0 +1,49 @@
1 + <?php
2 +
3 + /**
4 + * Simple php equivalent of Oracle's coalesce()
5 + *
6 + * It can be used as simple oneline-alternative to switch or if operators in many
7 + * cases without difficult logic. For example, get first non-empty value from bunch of vars:
8 + *
9 + * echo coalesce($var1, $var2, $var3, ...);
10 + *
11 + * is alternative to:
12 + *
13 + * if ($var1) {
14 + * return $var1;
15 + * } elseif ($var2) {
16 + * return $var2;
17 + * } elseif ....
18 + *
19 + * or
20 + *
21 + * return $var1 ?? $var2 ?? $var3 ...
22 + *
23 + * @see https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions023.htm
24 + * @author Anthony Axenov
25 + * @return mixed|null
26 + */
27 + function coalesce() {
28 + foreach (func_get_args() as $arg) {
29 + if (is_callable($arg)) {
30 + $arg = call_user_func($arg);
31 + }
32 + if ($arg !== null) {
33 + return $arg;
34 + }
35 + }
36 + return null;
37 + }
38 +
39 + //usage:
40 +
41 + $var1 = null;
42 + $var2 = function() use ($var1) {
43 + return $var1;
44 + };
45 + $var3 = 3;
46 + $var4 = '4';
47 +
48 + $result = coalesce($var1, $var2, $var3, $var4);
49 + var_dump($result);

decode.php(file created)

@@ -0,0 +1,68 @@
1 + <?php
2 +
3 + /**
4 + * Simple php equivalent of Oracle's decode()
5 + *
6 + * It can be used as simple oneline-alternative to switch or if operators in many
7 + * cases without difficult logic. For example, get string mnemocode of some value:
8 + *
9 + * echo 'State: '.decode($state, 0, 'disabled', 1, 'enabled', 'unknown');
10 + *
11 + * will output:
12 + * 'State: disabled' if $state === 0
13 + * 'State: enabled' if $state === 1
14 + * 'State: unknown' in other cases
15 + *
16 + * This is how decode() works:
17 + * 1) Store first argument separately from other ones to compare them with it
18 + * 2) Reduce and reindex array of arguments from 0
19 + * 3) Detect default value:
20 + * 3.1) If count of fresh args array is odd (which means that decode() was called
21 + * with even args count) then the last known argument is default result.
22 + * We also need to reduce and reindex args array here.
23 + * 3.2) If count is even, then default result === null.
24 + * 4) The rest of args processing this way:
25 + * - every even argument (n, n+2, n+4,...) is possible value;
26 + * - every odd arg (n+1, n+3, n+5,...) is result for previous arg.
27 + *
28 + * @see https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions040.htm
29 + * @author Anthony Axenov
30 + * @return mixed
31 + */
32 + function decode()
33 + {
34 + $args = func_get_args();
35 + $expression = $args[0];
36 + unset($args[0]);
37 + $args = array_values($args);
38 + if (count($args) % 2 !== 0) {
39 + $default = $args[count($args) - 1];
40 + unset($args[count($args) - 1]);
41 + $args = array_values($args);
42 + } else {
43 + $default = null;
44 + }
45 + for ($i = 0; $i < count($args); $i += 2) {
46 + if ($expression === $args[$i] && isset($args[$i + 1])) {
47 + return is_callable($args[$i + 1]) ? call_user_func($args[$i + 1]) : $args[$i + 1];
48 + }
49 + }
50 + return $default;
51 + }
52 +
53 + // usage:
54 + $somevar = 'function';
55 + $anything = 'Lorem ipsum dolor sit amet';
56 + $result = decode($somevar, // 0
57 + true , 'decode(): boolean true', // 1
58 + 0 , 'decode(): int zero', // 2
59 + '0' , 'decode(): string zero', // 3
60 + 'string' , 'decode(): just string', // 4
61 + 'function', function() use ($anything) { // 5
62 + // bla-bla-bla some logic here
63 + $anything = explode(' ', $anything);
64 + return 'Hey, this is '.print_r($anything, true);
65 + },
66 + 'decode(): default result' // 6 -- comment this line to get null
67 + );
68 + var_dump($result);
Newer Older