Connection.php
2 years ago
Converter.php
2 years ago
Driver.php
2 years ago
Middleware.php
2 years ago
OptimizeFlags.php
2 years ago
Result.php
2 years ago
Statement.php
2 years ago
index.php
2 years ago
Converter.php
137 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\DBAL\Portability; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use function array_change_key_case; |
| 6 | use function array_map; |
| 7 | use function array_reduce; |
| 8 | use function is_string; |
| 9 | use function rtrim; |
| 10 | use const CASE_LOWER; |
| 11 | use const CASE_UPPER; |
| 12 | final class Converter |
| 13 | { |
| 14 | public const CASE_LOWER = CASE_LOWER; |
| 15 | public const CASE_UPPER = CASE_UPPER; |
| 16 | private $convertNumeric; |
| 17 | private $convertAssociative; |
| 18 | private $convertOne; |
| 19 | private $convertAllNumeric; |
| 20 | private $convertAllAssociative; |
| 21 | private $convertFirstColumn; |
| 22 | public function __construct(bool $convertEmptyStringToNull, bool $rightTrimString, ?int $case) |
| 23 | { |
| 24 | $convertValue = $this->createConvertValue($convertEmptyStringToNull, $rightTrimString); |
| 25 | $convertNumeric = $this->createConvertRow($convertValue, null); |
| 26 | $convertAssociative = $this->createConvertRow($convertValue, $case); |
| 27 | $this->convertNumeric = $this->createConvert($convertNumeric, [self::class, 'id']); |
| 28 | $this->convertAssociative = $this->createConvert($convertAssociative, [self::class, 'id']); |
| 29 | $this->convertOne = $this->createConvert($convertValue, [self::class, 'id']); |
| 30 | $this->convertAllNumeric = $this->createConvertAll($convertNumeric, [self::class, 'id']); |
| 31 | $this->convertAllAssociative = $this->createConvertAll($convertAssociative, [self::class, 'id']); |
| 32 | $this->convertFirstColumn = $this->createConvertAll($convertValue, [self::class, 'id']); |
| 33 | } |
| 34 | public function convertNumeric($row) |
| 35 | { |
| 36 | return ($this->convertNumeric)($row); |
| 37 | } |
| 38 | public function convertAssociative($row) |
| 39 | { |
| 40 | return ($this->convertAssociative)($row); |
| 41 | } |
| 42 | public function convertOne($value) |
| 43 | { |
| 44 | return ($this->convertOne)($value); |
| 45 | } |
| 46 | public function convertAllNumeric(array $data) : array |
| 47 | { |
| 48 | return ($this->convertAllNumeric)($data); |
| 49 | } |
| 50 | public function convertAllAssociative(array $data) : array |
| 51 | { |
| 52 | return ($this->convertAllAssociative)($data); |
| 53 | } |
| 54 | public function convertFirstColumn(array $data) : array |
| 55 | { |
| 56 | return ($this->convertFirstColumn)($data); |
| 57 | } |
| 58 | private static function id($value) |
| 59 | { |
| 60 | return $value; |
| 61 | } |
| 62 | private static function convertEmptyStringToNull($value) |
| 63 | { |
| 64 | if ($value === '') { |
| 65 | return null; |
| 66 | } |
| 67 | return $value; |
| 68 | } |
| 69 | private static function rightTrimString($value) |
| 70 | { |
| 71 | if (!is_string($value)) { |
| 72 | return $value; |
| 73 | } |
| 74 | return rtrim($value); |
| 75 | } |
| 76 | private function createConvertValue(bool $convertEmptyStringToNull, bool $rightTrimString) : ?callable |
| 77 | { |
| 78 | $functions = []; |
| 79 | if ($convertEmptyStringToNull) { |
| 80 | $functions[] = [self::class, 'convertEmptyStringToNull']; |
| 81 | } |
| 82 | if ($rightTrimString) { |
| 83 | $functions[] = [self::class, 'rightTrimString']; |
| 84 | } |
| 85 | return $this->compose(...$functions); |
| 86 | } |
| 87 | private function createConvertRow(?callable $function, ?int $case) : ?callable |
| 88 | { |
| 89 | $functions = []; |
| 90 | if ($function !== null) { |
| 91 | $functions[] = $this->createMapper($function); |
| 92 | } |
| 93 | if ($case !== null) { |
| 94 | $functions[] = static function (array $row) use($case) : array { |
| 95 | return array_change_key_case($row, $case); |
| 96 | }; |
| 97 | } |
| 98 | return $this->compose(...$functions); |
| 99 | } |
| 100 | private function createConvert(?callable $function, callable $id) : callable |
| 101 | { |
| 102 | if ($function === null) { |
| 103 | return $id; |
| 104 | } |
| 105 | return static function ($value) use($function) { |
| 106 | if ($value === \false) { |
| 107 | return \false; |
| 108 | } |
| 109 | return $function($value); |
| 110 | }; |
| 111 | } |
| 112 | private function createConvertAll(?callable $function, callable $id) : callable |
| 113 | { |
| 114 | if ($function === null) { |
| 115 | return $id; |
| 116 | } |
| 117 | return $this->createMapper($function); |
| 118 | } |
| 119 | private function createMapper(callable $function) : callable |
| 120 | { |
| 121 | return static function (array $array) use($function) : array { |
| 122 | return array_map($function, $array); |
| 123 | }; |
| 124 | } |
| 125 | private function compose(callable ...$functions) : ?callable |
| 126 | { |
| 127 | return array_reduce($functions, static function (?callable $carry, callable $item) : callable { |
| 128 | if ($carry === null) { |
| 129 | return $item; |
| 130 | } |
| 131 | return static function ($value) use($carry, $item) { |
| 132 | return $item($carry($value)); |
| 133 | }; |
| 134 | }); |
| 135 | } |
| 136 | } |
| 137 |