|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace CodeTool\Jaeger\MongoDb; |
| 6 | + |
| 7 | +use MongoDB\BSON\Type; |
| 8 | + |
| 9 | +class CommandToStringConvertor |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var int |
| 13 | + */ |
| 14 | + private $bsonEndPos; |
| 15 | + |
| 16 | + public function __construct() |
| 17 | + { |
| 18 | + $this->bsonEndPos = strrpos(Type::class, '\\') + 1; |
| 19 | + } |
| 20 | + |
| 21 | + private function transformScalar($v): string |
| 22 | + { |
| 23 | + if (\is_int($v)) { |
| 24 | + return '?'; |
| 25 | + } |
| 26 | + |
| 27 | + if (\is_array($v)) { |
| 28 | + return $this->transformQuery($v); |
| 29 | + } |
| 30 | + |
| 31 | + if (\is_object($v)) { |
| 32 | + if ($v instanceof Type) { |
| 33 | + return 'new ' . substr(\get_class($v), $this->bsonEndPos) . '(?)'; |
| 34 | + } |
| 35 | + |
| 36 | + return $this->transformQuery($v); |
| 37 | + } |
| 38 | + |
| 39 | + if (\is_string($v) && $v !== '' && $v[0] === '$') { |
| 40 | + return '"' . $v . '"'; |
| 41 | + } |
| 42 | + |
| 43 | + return '"?"'; |
| 44 | + } |
| 45 | + |
| 46 | + private function transformQuery($data): string |
| 47 | + { |
| 48 | + $isArray = \is_array($data); |
| 49 | + $result = $isArray ? '[' : '{'; |
| 50 | + |
| 51 | + foreach ($data as $k => $v) { |
| 52 | + if (false === $isArray) { |
| 53 | + $result .= $k . ': '; |
| 54 | + |
| 55 | + if ($k === '$in') { |
| 56 | + $result .= '[\'...\'], '; |
| 57 | + |
| 58 | + continue; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + $result .= $this->transformScalar($v) . ', '; |
| 63 | + } |
| 64 | + |
| 65 | + return substr($result, 0, -2) . ($isArray ? ']' : '}'); |
| 66 | + } |
| 67 | + |
| 68 | + private function simpleScalarToJson($v): string |
| 69 | + { |
| 70 | + if ($v === true) { |
| 71 | + return 'true'; |
| 72 | + } |
| 73 | + |
| 74 | + if ($v === false) { |
| 75 | + return 'true'; |
| 76 | + } |
| 77 | + |
| 78 | + if ($v === null) { |
| 79 | + return 'null'; |
| 80 | + } |
| 81 | + |
| 82 | + if (\is_int($v)) { |
| 83 | + return (string)$v; |
| 84 | + } |
| 85 | + |
| 86 | + return '"' . $v . '"'; |
| 87 | + } |
| 88 | + |
| 89 | + private function serializeWriteConcern($writeConcern): string |
| 90 | + { |
| 91 | + if (!isset($writeConcern->w)) { |
| 92 | + return ''; |
| 93 | + } |
| 94 | + |
| 95 | + $result = '{w: ' . $this->simpleScalarToJson($writeConcern->w); |
| 96 | + if (isset($writeConcern->j)) { |
| 97 | + $result .= ', j: ' . $this->simpleScalarToJson($writeConcern->wtimeout); |
| 98 | + } |
| 99 | + |
| 100 | + if (isset($writeConcern->wtimeout)) { |
| 101 | + $result .= ', wtimeout: ' . $this->simpleScalarToJson($writeConcern->wtimeout); |
| 102 | + } |
| 103 | + |
| 104 | + $result .= '}'; |
| 105 | + |
| 106 | + return $result; |
| 107 | + } |
| 108 | + |
| 109 | + private function transformUpdateDelete(object $op): string |
| 110 | + { |
| 111 | + $result = '{'; |
| 112 | + foreach ($op as $k => $v) { |
| 113 | + switch ($k) { |
| 114 | + case 'q': |
| 115 | + case 'u': |
| 116 | + $tVal = $this->transformQuery($v); |
| 117 | + break; |
| 118 | + default: |
| 119 | + $tVal = json_encode($v); |
| 120 | + } |
| 121 | + |
| 122 | + $result .= $k . ': ' . $tVal . ', '; |
| 123 | + } |
| 124 | + |
| 125 | + return substr($result, 0, -2) . '}'; |
| 126 | + } |
| 127 | + |
| 128 | + private function transformUpdatesDeletes(array $coll): string |
| 129 | + { |
| 130 | + $result = '['; |
| 131 | + foreach ($coll as $v) { |
| 132 | + $result .= $this->transformUpdateDelete($v) . ', '; |
| 133 | + } |
| 134 | + |
| 135 | + return substr($result, 0, -2) . ']'; |
| 136 | + } |
| 137 | + |
| 138 | + public function convert(object $command): string |
| 139 | + { |
| 140 | + $result = 'db.runCommand({'; |
| 141 | + foreach ($command as $k => $v) { |
| 142 | + switch ($k) { |
| 143 | + case 'updates': |
| 144 | + case 'deletes': |
| 145 | + $tVal = $this->transformUpdatesDeletes($v); |
| 146 | + break; |
| 147 | + |
| 148 | + case 'query': |
| 149 | + case 'filter': |
| 150 | + case 'documents': |
| 151 | + $tVal = $this->transformQuery($v); |
| 152 | + break; |
| 153 | + |
| 154 | + case 'writeConcern': |
| 155 | + $tVal = $this->serializeWriteConcern($v); |
| 156 | + break; |
| 157 | + default: |
| 158 | + $tVal = json_encode($v); |
| 159 | + } |
| 160 | + |
| 161 | + if ('' === $tVal) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + $result .= $k . ': ' . $tVal . ', '; |
| 165 | + } |
| 166 | + |
| 167 | + return substr($result, 0, -2) . '})'; |
| 168 | + } |
| 169 | +} |
0 commit comments