SoliDry /
api-generator
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace SoliDry\Blocks; |
||||||
| 4 | |||||||
| 5 | use Illuminate\Database\Schema\Blueprint; |
||||||
| 6 | use Illuminate\Support\Facades\Schema; |
||||||
| 7 | use SoliDry\Exceptions\AttributesException; |
||||||
| 8 | use SoliDry\Helpers\Classes; |
||||||
| 9 | use SoliDry\Types\ErrorsInterface; |
||||||
| 10 | use SoliDry\Types\ModelsInterface; |
||||||
| 11 | use SoliDry\Types\PhpInterface; |
||||||
| 12 | use SoliDry\Types\ApiInterface; |
||||||
| 13 | |||||||
| 14 | trait MigrationsTrait |
||||||
| 15 | { |
||||||
| 16 | /** |
||||||
| 17 | * @param string $entity |
||||||
| 18 | * @param string $schemaMethod |
||||||
| 19 | * @throws \ReflectionException |
||||||
| 20 | */ |
||||||
| 21 | public function openSchema(string $entity, $schemaMethod = ModelsInterface::MIGRATION_CREATE) : void |
||||||
| 22 | { |
||||||
| 23 | $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4 . ModelsInterface::MIGRATION_SCHEMA |
||||||
| 24 | . PhpInterface::DOUBLE_COLON . $schemaMethod |
||||||
| 25 | . PhpInterface::OPEN_PARENTHESES . PhpInterface::QUOTES . strtolower($entity) . PhpInterface::QUOTES |
||||||
| 26 | . PhpInterface::COMMA . PhpInterface::SPACE . PhpInterface::PHP_FUNCTION |
||||||
| 27 | . PhpInterface::OPEN_PARENTHESES . Classes::getName(Blueprint::class) . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN |
||||||
| 28 | . ModelsInterface::MIGRATION_TABLE . PhpInterface::CLOSE_PARENTHESES . PhpInterface::SPACE; |
||||||
| 29 | |||||||
| 30 | $this->sourceCode .= PhpInterface::OPEN_BRACE . PHP_EOL; |
||||||
| 31 | } |
||||||
| 32 | |||||||
| 33 | public function closeSchema() : void |
||||||
| 34 | { |
||||||
| 35 | $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4 . PhpInterface::CLOSE_BRACE . PhpInterface::CLOSE_PARENTHESES |
||||||
| 36 | . PhpInterface::SEMICOLON . PHP_EOL; |
||||||
| 37 | } |
||||||
| 38 | |||||||
| 39 | /** |
||||||
| 40 | * Creates table migration schema |
||||||
| 41 | * |
||||||
| 42 | * @param string $method |
||||||
| 43 | * @param string $entity |
||||||
| 44 | * @throws \ReflectionException |
||||||
| 45 | */ |
||||||
| 46 | public function createSchema(string $method, string $entity) : void |
||||||
| 47 | { |
||||||
| 48 | $this->sourceCode .= $this->setTabs(2) . Classes::getName(Schema::class) . PhpInterface::DOUBLE_COLON |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 49 | . $method . PhpInterface::OPEN_PARENTHESES . PhpInterface::QUOTES . $entity |
||||||
| 50 | . PhpInterface::QUOTES . PhpInterface::CLOSE_PARENTHESES . PhpInterface::SEMICOLON . PHP_EOL; |
||||||
| 51 | } |
||||||
| 52 | |||||||
| 53 | /** |
||||||
| 54 | * Writes row to migration with type and params |
||||||
| 55 | * |
||||||
| 56 | * @param string $method |
||||||
| 57 | * @param string|null $property |
||||||
| 58 | * @param null $opts |
||||||
|
0 ignored issues
–
show
|
|||||||
| 59 | * @param array|null $build |
||||||
| 60 | * @param bool $quoteProperty |
||||||
| 61 | */ |
||||||
| 62 | public function setRow(string $method, string $property = '', $opts = null, array $build = null, $quoteProperty = true) : void |
||||||
| 63 | { |
||||||
| 64 | $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4 |
||||||
| 65 | . PhpInterface::DOLLAR_SIGN . ModelsInterface::MIGRATION_TABLE |
||||||
| 66 | . PhpInterface::ARROW . $method . PhpInterface::OPEN_PARENTHESES; |
||||||
| 67 | if ($quoteProperty === true) { |
||||||
| 68 | $this->sourceCode .= PhpInterface::QUOTES . $property . PhpInterface::QUOTES; |
||||||
| 69 | } else { // smth like array |
||||||
| 70 | $this->sourceCode .= $property; |
||||||
| 71 | } |
||||||
| 72 | $this->sourceCode .= (($opts === null) ? '' : PhpInterface::COMMA . PhpInterface::SPACE . $opts) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 73 | . PhpInterface::CLOSE_PARENTHESES; |
||||||
| 74 | if ($build !== null) { |
||||||
| 75 | foreach ($build as $m => $param) { |
||||||
| 76 | $this->sourceCode .= PhpInterface::ARROW . $m . PhpInterface::OPEN_PARENTHESES |
||||||
| 77 | . $param . PhpInterface::CLOSE_PARENTHESES; |
||||||
| 78 | } |
||||||
| 79 | } |
||||||
| 80 | |||||||
| 81 | $this->sourceCode .= PhpInterface::SEMICOLON . PHP_EOL; |
||||||
| 82 | } |
||||||
| 83 | |||||||
| 84 | /** |
||||||
| 85 | * Sets an int type for table row |
||||||
| 86 | * |
||||||
| 87 | * @param string $attrKey |
||||||
| 88 | * @param array $attrVal |
||||||
| 89 | * @param $type |
||||||
| 90 | */ |
||||||
| 91 | private function setIntegerRow(string $attrKey, array $attrVal, $type) : void |
||||||
| 92 | { |
||||||
| 93 | if ($attrKey === ApiInterface::RAML_ID) { |
||||||
| 94 | $this->setId($attrVal, $attrKey, $type); |
||||||
|
0 ignored issues
–
show
It seems like
setId() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 95 | } else { |
||||||
| 96 | $min = empty($attrVal[ApiInterface::RAML_INTEGER_MIN]) ? null : $attrVal[ApiInterface::RAML_INTEGER_MIN]; |
||||||
| 97 | $max = empty($attrVal[ApiInterface::RAML_INTEGER_MAX]) ? null : $attrVal[ApiInterface::RAML_INTEGER_MAX]; |
||||||
| 98 | $this->setIntegerDigit($attrKey, $max, $min < 0); |
||||||
|
0 ignored issues
–
show
The method
setIntegerDigit() does not exist on SoliDry\Blocks\MigrationsTrait. Did you maybe mean setIntegerRow()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 99 | } |
||||||
| 100 | } |
||||||
| 101 | |||||||
| 102 | /** |
||||||
| 103 | * Sets string type for table row |
||||||
| 104 | * |
||||||
| 105 | * @param string $attrKey |
||||||
| 106 | * @param array $attrVal |
||||||
| 107 | */ |
||||||
| 108 | private function setStringRow(string $attrKey, array $attrVal) : void |
||||||
| 109 | { |
||||||
| 110 | $length = empty($attrVal[ApiInterface::RAML_STRING_MAX]) ? null : $attrVal[ApiInterface::RAML_STRING_MAX]; |
||||||
| 111 | $build = empty($attrVal[ApiInterface::RAML_KEY_DEFAULT]) ? null |
||||||
| 112 | : [ApiInterface::RAML_KEY_DEFAULT => $this->quoteParam($attrVal[ApiInterface::RAML_KEY_DEFAULT])]; |
||||||
|
0 ignored issues
–
show
It seems like
quoteParam() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 113 | $this->setRow(ModelsInterface::MIGRATION_METHOD_STRING, $attrKey, $length, $build); |
||||||
| 114 | } |
||||||
| 115 | |||||||
| 116 | /** |
||||||
| 117 | * @param array $attrVal |
||||||
| 118 | * @param string $attrKey |
||||||
| 119 | */ |
||||||
| 120 | private function setNumberRow(array $attrVal, string $attrKey): void |
||||||
| 121 | { |
||||||
| 122 | if (empty($attrVal[ApiInterface::RAML_TYPE_FORMAT]) === false |
||||||
| 123 | && ($attrVal[ApiInterface::RAML_TYPE_FORMAT] === ModelsInterface::MIGRATION_METHOD_DOUBLE |
||||||
| 124 | || $attrVal[ApiInterface::RAML_TYPE_FORMAT] === ModelsInterface::MIGRATION_METHOD_FLOAT) |
||||||
| 125 | ) { |
||||||
| 126 | $max = empty($attrVal[ApiInterface::RAML_INTEGER_MAX]) ? ModelsInterface::DEFAULT_DOUBLE_M : $attrVal[ApiInterface::RAML_INTEGER_MAX]; |
||||||
| 127 | $min = empty($attrVal[ApiInterface::RAML_INTEGER_MIN]) ? ModelsInterface::DEFAULT_DOUBLE_D : $attrVal[ApiInterface::RAML_INTEGER_MIN]; |
||||||
| 128 | |||||||
| 129 | $this->setRow($attrVal[ApiInterface::RAML_TYPE_FORMAT], $attrKey, $max . PhpInterface::COMMA |
||||||
| 130 | . PhpInterface::SPACE . $min); |
||||||
| 131 | } |
||||||
| 132 | } |
||||||
| 133 | |||||||
| 134 | /** |
||||||
| 135 | * Sets foreign key on table row |
||||||
| 136 | * |
||||||
| 137 | * @param array $facets |
||||||
| 138 | * @param string $attrKey |
||||||
| 139 | * @param $k |
||||||
| 140 | * @throws AttributesException |
||||||
| 141 | */ |
||||||
| 142 | private function setForeignIndex(array $facets, string $attrKey, $k) : void |
||||||
| 143 | { |
||||||
| 144 | if (empty($facets[ModelsInterface::INDEX_REFERENCES]) || empty($facets[ModelsInterface::INDEX_ON])) { |
||||||
| 145 | throw new AttributesException(ErrorsInterface::CONSOLE_ERRORS[ErrorsInterface::CODE_FOREIGN_KEY], ErrorsInterface::CODE_FOREIGN_KEY); |
||||||
| 146 | } |
||||||
| 147 | |||||||
| 148 | $build = [ |
||||||
| 149 | ModelsInterface::INDEX_REFERENCES => $this->quoteParam($facets[ModelsInterface::INDEX_REFERENCES]), |
||||||
| 150 | ModelsInterface::INDEX_ON => $this->quoteParam($facets[ModelsInterface::INDEX_ON]), |
||||||
| 151 | ]; |
||||||
| 152 | |||||||
| 153 | if (empty($facets[ModelsInterface::INDEX_ON_DELETE]) === false) { |
||||||
| 154 | $build[ModelsInterface::INDEX_ON_DELETE] = $this->quoteParam($facets[ModelsInterface::INDEX_ON_DELETE]); |
||||||
| 155 | } |
||||||
| 156 | |||||||
| 157 | if (empty($facets[ModelsInterface::INDEX_ON_UPDATE]) === false) { |
||||||
| 158 | $build[ModelsInterface::INDEX_ON_UPDATE] = $this->quoteParam($facets[ModelsInterface::INDEX_ON_UPDATE]); |
||||||
| 159 | } |
||||||
| 160 | |||||||
| 161 | $this->setRow(ModelsInterface::INDEX_TYPE_FOREIGN, $attrKey, $this->quoteParam($k), $build); |
||||||
| 162 | } |
||||||
| 163 | |||||||
| 164 | /** |
||||||
| 165 | * Creates composite index via facets |
||||||
| 166 | * @param array $attrVal |
||||||
| 167 | * @throws AttributesException |
||||||
| 168 | */ |
||||||
| 169 | private function setCompositeIndex(array $attrVal): void |
||||||
| 170 | { |
||||||
| 171 | if (empty($attrVal[ApiInterface::RAML_FACETS][ApiInterface::RAML_COMPOSITE_INDEX]) === false) { |
||||||
| 172 | |||||||
| 173 | $facets = $attrVal[ApiInterface::RAML_FACETS][ApiInterface::RAML_COMPOSITE_INDEX]; |
||||||
| 174 | if (empty($facets[ModelsInterface::INDEX_TYPE_FOREIGN]) === false) { |
||||||
| 175 | if (empty($facets[ModelsInterface::INDEX_REFERENCES]) || empty($facets[ModelsInterface::INDEX_ON])) { |
||||||
| 176 | throw new AttributesException(ErrorsInterface::CONSOLE_ERRORS[ErrorsInterface::CODE_FOREIGN_KEY], ErrorsInterface::CODE_FOREIGN_KEY); |
||||||
| 177 | } |
||||||
| 178 | |||||||
| 179 | $build = [ |
||||||
| 180 | ModelsInterface::INDEX_REFERENCES => $this->getArrayParam($facets[ModelsInterface::INDEX_REFERENCES]), |
||||||
|
0 ignored issues
–
show
It seems like
getArrayParam() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 181 | ModelsInterface::INDEX_ON => $this->quoteParam($facets[ModelsInterface::INDEX_ON]), |
||||||
| 182 | ]; |
||||||
| 183 | |||||||
| 184 | if (empty($facets[ModelsInterface::INDEX_ON_DELETE]) === false) { |
||||||
| 185 | $build[ModelsInterface::INDEX_ON_DELETE] = $this->quoteParam($facets[ModelsInterface::INDEX_ON_DELETE]); |
||||||
| 186 | } |
||||||
| 187 | |||||||
| 188 | if (empty($facets[ModelsInterface::INDEX_ON_UPDATE]) === false) { |
||||||
| 189 | $build[ModelsInterface::INDEX_ON_UPDATE] = $this->quoteParam($facets[ModelsInterface::INDEX_ON_UPDATE]); |
||||||
| 190 | } |
||||||
| 191 | |||||||
| 192 | $this->setRow(ModelsInterface::INDEX_TYPE_FOREIGN, $this->getArrayParam($facets[ModelsInterface::INDEX_TYPE_FOREIGN]), null, $build, false); |
||||||
| 193 | } else { |
||||||
| 194 | foreach ($facets as $k => $v) { |
||||||
| 195 | $this->setRow($k, $this->getArrayParam($v), null, null, false); |
||||||
| 196 | } |
||||||
| 197 | } |
||||||
| 198 | } |
||||||
| 199 | } |
||||||
| 200 | } |