.*)/is'): array
+ {
+ if (file_exists($filenameWithPath)) {
+ $file = file_get_contents($filenameWithPath);
+ if (preg_match($generalPattern, $file, $matches)) {
+ if (preg_match_all($specificPattern, $matches['main_part'], $attributMatches)) {
+ for ($i = 0; $i < count($attributMatches['name']); $i++) {
+ if (array_search($this->getChoice(), self::ALL_OPTIONS) < 2)
+ $this->setChoice(null);
+ if (!isset($newParamsArray[$attributMatches['name'][$i]])) {
+ $newParamsArray[$attributMatches['name'][$i]] = '';
+ }
+ $attr = $newParamsArray[$attributMatches['name'][$i]];
+
+ if (preg_replace('/\s+/', '', $attr) === preg_replace('/\s+/', '', $attributMatches[0][$i])) {
+ $command->info("There is no diffrence between '" . $attributMatches['name'][$i] . "' ");
+ } else {
+ $command->warn("WARN: '" . $attributMatches['name'][$i] . "'s are not the same");
+ if (is_null($this->getChoice()) && array_search($this->getChoice(), self::ALL_OPTIONS) < 2) {
+ // $command->table( ['Current','new'], [['Current'=>trim($attributMatches[0][$i]),'New'=>trim($attr)]],'default');
+ $command->line("######################## CURRENT #########################", 'fg=magenta');
+ $command->line(trim($attributMatches[0][$i]), 'fg=magenta');
+ $command->line("##########################################################", 'fg=magenta');
+ $command->line(" ", 'fg=magenta');
+ $command->line("########################## NEW ###########################", 'fg=cyan');
+ $command->line(trim($attr), 'fg=cyan');
+ $command->line("##########################################################", 'fg=cyan');
+ $this->setChoice($command->choice('Choose one version', self::ALL_OPTIONS, 0));
+ if (array_search($this->getChoice(), self::ALL_OPTIONS) % 2 == 0) {
+ $newParamsArray[$attributMatches['name'][$i]] = trim($attributMatches[0][$i]) . PHP_EOL;
+ $command->warn("Action: Current version selected for '" . $attributMatches['name'][$i] . "', ");
+ }
+ } elseif (array_search($this->getChoice(), self::ALL_OPTIONS) == 2) {
+ $newParamsArray[$attributMatches['name'][$i]] = trim($attributMatches[0][$i]) . PHP_EOL;
+ $command->warn("Action: Current version selected for '" . $attributMatches['name'][$i] . "', ");
+ } else {
+ $command->warn("Action: New version replaced for '" . $attributMatches['name'][$i] . "', ");
+ }
+ }
+ }
+ }
+ }
+ }
+ return $newParamsArray;
+ }
+
+ private function setCacheTag(BaseCommand $command)
+ {
+ return (isset($command->strategyName) && in_array($command->strategyName, [self::QUERY_CACHE_STRATEGY, self::SINGLE_KEY_CACHE_STRATEGY])) ? "'$command->tableName'" : "''";
+ }
+}
diff --git a/src/Creators/CreatorEntity.php b/src/Creators/CreatorEntity.php
new file mode 100644
index 0000000..d6c2195
--- /dev/null
+++ b/src/Creators/CreatorEntity.php
@@ -0,0 +1,138 @@
+columns as $_column) {
+
+ $dataType = $this->getDataType($_column->COLUMN_TYPE, $_column->DATA_TYPE);
+
+ $defaultValue = null;
+ if ($_column->COLUMN_DEFAULT !== null) {
+ $defaultValue = $_column->COLUMN_DEFAULT;
+
+ if ($dataType === DataTypeEnum::INTEGER_TYPE) {
+ $defaultValue = intval($defaultValue);
+ }
+
+ if ($dataType === self::BOOL_TYPE) {
+ if (in_array($defaultValue, [0, '', "''"])) {
+ $defaultValue = 'false';
+ } elseif (in_array($defaultValue, [1, '1'])) {
+ $defaultValue = 'true';
+ }
+ }
+ }
+
+ $columnString = $_column->COLUMN_NAME;
+
+ if (!in_array($_column->COLUMN_DEFAULT, [null, 'NULL'])) {
+ $columnString .= ' = ' . $defaultValue;
+ }
+
+ if ($_column->IS_NULLABLE === 'YES') {
+ $columnString .= ' = null';
+ }
+
+ $attributes[$_column->COLUMN_NAME] =
+ $this->writeAttribute(
+ $this->entityStubsPath,
+ $columnString,
+ ($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType
+ );
+ }
+
+ if ($this->detectForeignKeys) {
+ $foreignKeys = $this->extractForeignKeys($this->tableName);
+
+ // Create Additional Attributes from Foreign Keys
+ foreach ($foreignKeys as $_foreignKey) {
+ $attributes[$_column->COLUMN_NAME] =
+ $this->writeAttribute(
+ $this->entityStubsPath,
+ $_foreignKey->VARIABLE_NAME,
+ $_foreignKey->ENTITY_DATA_TYPE
+ );
+ }
+ }
+
+ return $attributes;
+ }
+
+ public function createUses(): array
+ {
+ return ['use Eghamat24\DatabaseRepository\Models\Entity\Entity;'];
+ }
+
+ public function createFunctions(): array
+ {
+ return [];
+ }
+
+ private function writeAttribute(string $entityStubsPath, string $attributeName, string $attributeType): string
+ {
+ $attributeStub = file_get_contents($entityStubsPath . 'attribute.stub');
+
+ $replaceMapping = [
+ '{{ AttributeType }}' => $attributeType,
+ '{{ AttributeName }}' => $attributeName,
+ ];
+
+ return str_replace(array_keys($replaceMapping), array_values($replaceMapping), $attributeStub);
+ }
+
+ private function writeAccessors(string $entityStubsPath, string $attributeName, string $attributeType, string $type): string
+ {
+ $accessorStub = file_get_contents($entityStubsPath . $type . '.stub');
+
+ $replaceMapping = [
+ '{{ AttributeType }}' => $attributeType,
+ '{{ AttributeName }}' => $attributeName,
+ '{{ GetterName }}' => ucfirst($attributeName),
+ '{{ SetterName }}' => ucfirst($attributeName)
+ ];
+
+ return str_replace(array_keys($replaceMapping), array_values($replaceMapping), $accessorStub);
+ }
+
+ public function getNameSpace(): string
+ {
+ return $this->entityNamespace;
+ }
+
+ public function getClassName(): string
+ {
+ return $this->entityName;
+ }
+}
diff --git a/src/Creators/CreatorEnum.php b/src/Creators/CreatorEnum.php
new file mode 100644
index 0000000..f03c3c1
--- /dev/null
+++ b/src/Creators/CreatorEnum.php
@@ -0,0 +1,69 @@
+enum as $_enum) {
+
+ $attributes[strtoupper($_enum)] = $this->writeAttribute(
+ $this->attributeStub,
+ strtoupper($_enum),
+ $_enum
+ );
+ }
+
+ return $attributes;
+ }
+
+ public function createFunctions(): array
+ {
+ return [];
+ }
+
+ public function createUses(): array
+ {
+ return [];
+ }
+
+ public function getExtendSection(): string
+ {
+ return '';
+ }
+
+ public function getNameSpace(): string
+ {
+ return $this->enumNamespace;
+ }
+
+ public function getClassName(): string
+ {
+ return $this->enumName . ' : string';
+ }
+
+ private function writeAttribute(string $attributeStub, string $attributeName, string $attributeString): string
+ {
+ $replaceMapping = [
+ '{{ AttributeName }}' => $attributeName,
+ '{{ AttributeString }}' => $attributeString,
+ ];
+
+ return str_replace(array_keys($replaceMapping), array_values($replaceMapping), $attributeStub);
+ }
+}
diff --git a/src/Creators/CreatorFactory.php b/src/Creators/CreatorFactory.php
new file mode 100644
index 0000000..f7d7e6e
--- /dev/null
+++ b/src/Creators/CreatorFactory.php
@@ -0,0 +1,74 @@
+factoryNamespace;
+ }
+
+ public function createAttributes(): array
+ {
+ $setStub = file_get_contents($this->factoryStubsPath . 'set.stub');
+ $sets = '';
+ foreach ($this->columns as $_column) {
+ $replacementTokens = [
+ '{{ AttributeName }}' => Str::camel($_column->COLUMN_NAME),
+ '{{ DatabaseAttributeName }}' => Str::snake($_column->COLUMN_NAME)
+ ];
+
+ $sets .= str_replace(array_keys($replacementTokens), array_values($replacementTokens), $setStub) . "\t\t";
+ }
+
+ return ['makeEntityFromStdClass' =>
+ str_replace(['{{ Sets }}', '{{ EntityName }}', '{{ EntityVariableName }}'],
+ [$sets, $this->entityName, $this->entityVariableName],
+ $this->baseContent)
+ ];
+ return [];
+ }
+
+ public function createFunctions(): array
+ {
+ return [];
+ }
+
+ public function createUses(): array
+ {
+ return [
+ "use $this->entityNamespace\\$this->entityName;",
+ 'use Eghamat24\DatabaseRepository\Models\Factories\Factory;',
+ 'use stdClass;'
+ ];
+
+ }
+
+ public function getExtendSection(): string
+ {
+ return 'extends ' . self::PARENT_NAME;
+ }
+
+ public function getClassName(): string
+ {
+ return $this->factoryName;
+ }
+}
diff --git a/src/Creators/CreatorMySqlRepository.php b/src/Creators/CreatorMySqlRepository.php
new file mode 100644
index 0000000..d1e94c4
--- /dev/null
+++ b/src/Creators/CreatorMySqlRepository.php
@@ -0,0 +1,222 @@
+repositoryNamespace . '\\' . $this->entityName;
+ }
+
+ public function createUses(): array
+ {
+ return [
+ "use $this->entityNamespace\\$this->entityName;",
+ "use $this->factoryNamespace\\$this->factoryName;",
+ 'use Illuminate\Support\Collection;',
+ 'use Eghamat24\DatabaseRepository\Models\Repositories\MySqlRepository;'
+ ];
+ }
+
+ public function getClassName(): string
+ {
+ return $this->mysqlRepositoryName;
+ }
+
+ public function getExtendSection(): string
+ {
+ return 'extends MySqlRepository implements ' . $this->interfaceName;
+ }
+
+ public function createAttributes(): array
+ {
+ return [];
+ }
+
+ public function createFunctions(): array
+ {
+
+ $stubList = [
+ 'baseContent' => 'class.stub',
+ 'constructContent' => 'construct.stub',
+ 'getOneStub' => 'getOneBy.stub',
+ 'getAllStub' => 'getAllBy.stub',
+ 'createFunctionStub' => 'create.stub',
+ 'updateFunctionStub' => 'update.stub',
+ 'deleteStub' => 'delete.stub',
+ 'undeleteStub' => 'undelete.stub',
+ 'getterStub' => 'getter.stub',
+ 'setterStub' => 'setter.stub',
+ 'timeFieldStub' => 'timeField.stub',
+ ];
+
+ $stubContent = [];
+ foreach ($stubList as $stubKey => $stubName) {
+ $stubContent[$stubKey] = file_get_contents($this->mysqlRepositoryStubsPath . $stubName);
+ }
+
+ $hasSoftDelete = in_array('deleted_at', $this->columns->pluck('COLUMN_NAME')->toArray(), true);
+
+ $functions = [];
+ $functions['__construct'] = $this->getConstruct($this->tableName, $this->factoryName, $hasSoftDelete, $stubContent['constructContent']);
+ $functions['getOneById'] = $this->writeGetOneFunction($stubContent['getOneStub'], 'id', DataTypeEnum::INTEGER_TYPE);
+ $functions['getAllByIds'] = $this->writeGetAllFunction($stubContent['getAllStub'], 'id', DataTypeEnum::INTEGER_TYPE);
+ $columnsInfo = $this->getAllColumnsInTable($this->tableName);
+
+ $indexes = $this->extractIndexes($this->tableName);
+ foreach ($indexes as $index) {
+ $columnInfo = collect($columnsInfo)->where('COLUMN_NAME', $index->COLUMN_NAME)->first();
+ $indx = 'getOneBy' . ucfirst(Str::camel($index->COLUMN_NAME));
+ $functions[$indx] = $this->writeGetOneFunction(
+ $stubContent['getOneStub'],
+ $index->COLUMN_NAME,
+ $this->getDataType($columnInfo->COLUMN_TYPE, $columnInfo->DATA_TYPE)
+ );
+
+ if ($index->Non_unique == 1) {
+ $indx = 'getAllBy' . ucfirst(Str::plural(Str::camel($index->COLUMN_NAME)));
+ $functions[$indx] = $this->writeGetAllFunction($stubContent['getAllStub'], $index->COLUMN_NAME, $this->entityName);
+ }
+ }
+
+ if ($this->detectForeignKeys) {
+ $foreignKeys = $this->extractForeignKeys($this->tableName);
+ foreach ($foreignKeys as $_foreignKey) {
+ $indx = 'getOneBy' . ucfirst(Str::camel($_foreignKey->COLUMN_NAME));
+ $functions[$indx] = $this->writeGetOneFunction($stubContent['getOneStub'], $_foreignKey->COLUMN_NAME, $this->entityName);
+ $indx = 'getAllBy' . ucfirst(Str::plural(Str::camel($_foreignKey->COLUMN_NAME)));
+ $functions[$indx] = $this->writeGetAllFunction($stubContent['getAllStub'], $_foreignKey->COLUMN_NAME, $this->entityName);
+ }
+ }
+
+ $getterFunctions = '';
+ $setterFunctions = '';
+ $functions = $this->makeCreateFunction($stubContent, $getterFunctions, $setterFunctions, $functions);
+
+ $getterFunctions = '';
+ $setterFunctions = '';
+ $functions = $this->makeUpdateFunction($stubContent, $getterFunctions, $setterFunctions, $functions);
+
+ // Create "delete" and "undelete" functions if necessary
+ if ($hasSoftDelete) {
+ $functions['remove'] = $stubContent['deleteStub'];
+ $functions['restore'] = $stubContent['undeleteStub'];
+ }
+
+ foreach ($functions as &$func) {
+ $func = str_replace(['{{ EntityName }}', '{{ EntityVariableName }}'],
+ [$this->entityName, $this->entityVariableName],
+ $func
+ );
+ }
+
+ return $functions;
+ }
+
+ private function writeGetOneFunction(string $getOneStub, string $columnName, string $attributeType): string
+ {
+ return str_replace(['{{ FunctionName }}', '{{ ColumnName }}', '{{ AttributeType }}', '{{ AttributeName }}'],
+ [ucfirst(Str::camel($columnName)), $columnName, $attributeType, Str::camel($columnName)],
+ $getOneStub);
+ }
+
+ private function writeGetAllFunction(string $getOneStub, string $columnName, string $attributeType): string
+ {
+ return str_replace(['{{ FunctionNamePlural }}', '{{ ColumnName }}', '{{ AttributeType }}', '{{ AttributeNamePlural }}'],
+ [ucfirst(Str::plural(Str::camel($columnName))), $columnName, $attributeType, Str::plural(Str::camel($columnName))],
+ $getOneStub);
+ }
+
+ private function getConstruct(string $tableName, string $factoryName, bool $hasSoftDelete, string $constructContent)
+ {
+ return str_replace(
+ ['{{ TableName }}', '{{ FactoryName }}', '{{ HasSoftDelete }}'],
+ [$tableName, $factoryName, $hasSoftDelete ? 'true' : 'false'],
+ $constructContent);
+ }
+
+ /**
+ * @param array $stubContent
+ * @param string $getterFunctions
+ * @param string $setterFunctions
+ * @param array $functions
+ * @return array
+ */
+ public function makeCreateFunction(array &$stubContent, string &$getterFunctions, string &$setterFunctions, array &$functions): array
+ {
+ foreach ($this->columns as $_column) {
+ if (!in_array($_column->COLUMN_NAME, ['id', 'deleted_at'])) {
+ $getterFunctions .= trim(str_replace(['{{ ColumnName }}', '{{ AttributeName }}'], [$_column->COLUMN_NAME, Str::camel($_column->COLUMN_NAME)], $stubContent['getterStub'])) . "\n\t\t\t\t";
+ }
+
+ if (in_array($_column->COLUMN_NAME, ['created_at', 'updated_at'], true)) {
+ $setterFunctions .= trim(str_replace('{{ AttributeName }}', Str::camel($_column->COLUMN_NAME), $stubContent['setterStub'])) . "\n\t\t";
+ }
+ }
+
+ $createFunctionStub = str_replace(["{{ GetterFunctions }}", "{{ SetterFunctions }}"],
+ [trim(substr($getterFunctions, 0, -1)), trim(substr($setterFunctions, 0, -1))],
+ $stubContent['createFunctionStub']
+ );
+
+ $functions['create'] = $createFunctionStub;
+
+ return $functions;
+ }
+
+ /**
+ * @param array $stubContent
+ * @param string $getterFunctions
+ * @param string $setterFunctions
+ * @param array $functions
+ * @return array
+ */
+ public function makeUpdateFunction(array &$stubContent, string &$getterFunctions, string &$setterFunctions, array &$functions): array
+ {
+ foreach ($this->columns as $_column) {
+
+ if (!in_array($_column->COLUMN_NAME, ['id', 'created_at', 'deleted_at'])) {
+ $getterFunctions .= trim(str_replace(['{{ ColumnName }}', '{{ AttributeName }}'], [$_column->COLUMN_NAME, Str::camel($_column->COLUMN_NAME)], $stubContent['getterStub'])) . "\n\t\t\t\t";;
+ }
+
+ if ($_column->COLUMN_NAME === 'updated_at') {
+ $setterFunctions .= trim(str_replace('{{ AttributeName }}', Str::camel($_column->COLUMN_NAME), $stubContent['setterStub'])). "\n\t\t";;
+ }
+ }
+
+ $updateFunctionStub = str_replace(['{{ GetterFunctions }}', '{{ UpdateFieldSetter }}'],
+ [trim(substr($getterFunctions, 0, -1)), trim(substr($setterFunctions, 0, -1))],
+ $stubContent['updateFunctionStub']
+ );
+
+ $functions['update'] = $updateFunctionStub;
+
+ return $functions;
+ }
+}
diff --git a/src/Creators/CreatorRedisRepository.php b/src/Creators/CreatorRedisRepository.php
new file mode 100644
index 0000000..a9f7f94
--- /dev/null
+++ b/src/Creators/CreatorRedisRepository.php
@@ -0,0 +1,59 @@
+redisRepositoryNamespace . '\\' . $this->entityName;
+ }
+
+ public function createUses(): array
+ {
+ return [
+ 'use Eghamat24\DatabaseRepository\Models\Repositories\RedisRepository;',
+ 'use Eghamat24\DatabaseRepository\Models\Repositories\CacheStrategies\\' . $this->strategyName . ';'
+ ];
+ }
+
+ public function getClassName(): string
+ {
+ return $this->redisRepositoryName;
+ }
+
+ public function getExtendSection(): string
+ {
+ return 'extends RedisRepository';
+ }
+
+ public function createAttributes(): array
+ {
+ return [];
+ }
+
+ public function createFunctions(): array
+ {
+ $constructStub = file_get_contents($this->repositoryStubsPath . 'construct_redis.stub');
+ $functions = [];
+ $functions['__construct'] = $this->getConstructRedis($constructStub);
+ return $functions;
+ }
+
+ public function getConstructRedis(string $constructStub): array|string
+ {
+ return str_replace('{{Strategy}}', $this->strategyName, $constructStub);
+ }
+
+}
diff --git a/src/Creators/CreatorRepository.php b/src/Creators/CreatorRepository.php
new file mode 100644
index 0000000..d81f307
--- /dev/null
+++ b/src/Creators/CreatorRepository.php
@@ -0,0 +1,222 @@
+getRedisCashFunctionGetOneBy($this->strategyName);
+ $returnResult = 'return $entity;';
+ } elseif ($functionName === 'getAllBy') {
+ $functionReturnType = 'Collection';
+ $functionName .= ucfirst(Str::plural(Str::camel($columnName)));
+ $columnName = Str::plural(Str::camel($columnName));
+ $redisCashFunction = $this->getRedisCashFunctionGetAllBy($this->strategyName);
+ $returnResult = 'return $entities;';
+ } elseif ($functionName === 'create') {
+ $functionReturnType = $attributeType;
+ $redisCashFunction = $this->getRedisCashFunctionCreate($this->strategyName);
+ $returnResult = 'return $this->{{ SqlRepositoryVariable }}->{{ FunctionName }}(${{ AttributeName }});';
+ } elseif (in_array($functionName, ['update', 'remove', 'restore'])) {
+ $functionReturnType = 'int';
+ $redisCashFunction = $this->getRedisCashFunctionUpdate($this->strategyName);
+ $returnResult = 'return $this->{{ SqlRepositoryVariable }}->{{ FunctionName }}(${{ AttributeName }});';
+ }
+
+ $redisCashFunction = str_replace(['{{ FunctionName }}', '{{ ColumnName }}', '{{ ColumnNameSingle }}'], [$functionName, $columnName, $columnNameSingle], $redisCashFunction);
+
+ $returnResult = str_replace(['{{ SqlRepositoryVariable }}', '{{ FunctionName }}', '{{ AttributeName }}'], [$this->sqlRepositoryVariable, $functionName, Str::camel($columnName)], $returnResult);
+
+ return str_replace(['{{ FunctionName }}', '{{ AttributeType }}', '{{ AttributeName }}', '{{ FunctionReturnType }}', '{{redisFunction}}', '{{returnResult}}'],
+ [$functionName, $attributeType, Str::camel($columnName), $functionReturnType, $redisCashFunction, $returnResult],
+ $functionStub);
+ }
+
+ private function writeSqlAttribute(string $attributeStub, string $sqlRepositoryVariable, string $sqlRepositoryName): string
+ {
+ return str_replace(['{{ SqlRepositoryVariable }}', '{{ SqlRepositoryName }}'],
+ [$sqlRepositoryVariable, $sqlRepositoryName],
+ $attributeStub);
+ }
+
+ public function writeRedisAttribute(string $attributeStub, string $redisRepositoryVariable, string $redisRepositoryName): string
+ {
+ return str_replace(['{{ RedisRepositoryVariable }}', '{{ RedisRepositoryName }}'],
+ [$redisRepositoryVariable, $redisRepositoryName],
+ $attributeStub);
+ }
+
+ public function getNameSpace(): string
+ {
+ return $this->repositoryNamespace . '\\' . $this->entityName;
+ }
+
+ public function createUses(): array
+ {
+ return [
+ "use $this->entityNamespace\\$this->entityName;",
+ "use Illuminate\Support\Collection;"
+ ];
+ }
+
+ public function getClassName(): string
+ {
+ return $this->repositoryName;
+ }
+
+ public function getExtendSection(): string
+ {
+ return 'implements ' . $this->interfaceName;
+ }
+
+ public function createAttributes(): array
+ {
+ $attributeSqlStub = file_get_contents($this->repositoryStubsPath . 'attribute.sql.stub');
+ $attributes = [];
+ $attributes['repository'] = 'private ' . $this->interfaceName . ' $repository;';
+ $attributes['redisRepository'] = 'private ' . $this->redisRepositoryName . ' $redisRepository;';
+ return $attributes;
+ }
+
+ public function createFunctions(): array
+ {
+ $constructStub = file_get_contents($this->repositoryStubsPath . 'construct.stub');
+ $functionStub = file_get_contents($this->repositoryStubsPath . 'function.stub');
+ $setterSqlStub = file_get_contents($this->repositoryStubsPath . 'setter.sql.stub');
+ $functions = [];
+ $functions['__construct'] = $this->getConstruct($setterSqlStub, $constructStub);
+ $functions['__construct'] = $this->getConstructRedis($setterSqlStub, $constructStub);
+ $functions['getOneById'] = $this->writeFunction($functionStub, 'getOneBy', 'id', 'int');
+ $functions['getAllByIds'] = $this->writeFunction($functionStub, 'getAllBy', 'id', 'array');
+ $columnsInfo = $this->getAllColumnsInTable($this->tableName);
+
+ $indexes = $this->extractIndexes($this->tableName);
+ foreach ($indexes as $index) {
+ $columnInfo = collect($columnsInfo)->where('COLUMN_NAME', $index->COLUMN_NAME)->first();
+ $fun_name = ucfirst(Str::camel($index->COLUMN_NAME));
+ $functions['getOneBy' . $fun_name] = $this->writeFunction($functionStub, 'getOneBy', $index->COLUMN_NAME, $this->getDataType($columnInfo->COLUMN_TYPE, $columnInfo->DATA_TYPE));
+
+ if ($index->Non_unique == 1) {
+ $fun_name = ucfirst(Str::plural(Str::camel($index->COLUMN_NAME)));
+ $functions['getAllBy' . $fun_name] = $this->writeFunction($functionStub, 'getAllBy', $index->COLUMN_NAME, 'array');
+ }
+ }
+ if ($this->detectForeignKeys) {
+ $foreignKeys = $this->extractForeignKeys($this->tableName);
+
+ foreach ($foreignKeys as $_foreignKey) {
+ $fun_name = ucfirst(Str::camel($_foreignKey->COLUMN_NAME));
+ $functions['getOneBy' . $fun_name] = $this->writeFunction($functionStub, 'getOneBy', $_foreignKey->COLUMN_NAME, 'int');
+ $fun_name = ucfirst(Str::plural(Str::camel($_foreignKey->COLUMN_NAME)));
+ $functions['getAllBy' . $fun_name] = $this->writeFunction($functionStub, 'getAllBy', $_foreignKey->COLUMN_NAME, 'array');
+ }
+ }
+ $functions['create'] = $this->writeFunction($functionStub, 'create', $this->entityVariableName, $this->entityName);
+ $functions['update'] = $this->writeFunction($functionStub, 'update', $this->entityVariableName, $this->entityName);
+ if (in_array('deleted_at', $this->columns->pluck('COLUMN_NAME')->toArray(), true)) {
+ $functions['remove'] = $this->writeFunction($functionStub, 'remove', $this->entityVariableName, $this->entityName);
+ $functions['restore'] = $this->writeFunction($functionStub, 'restore', $this->entityVariableName, $this->entityName);
+ }
+ foreach ($functions as &$func) {
+ $func = str_replace(["{{ SqlRepositoryVariable }}", '{{ SqlRepositoryName }}', '{{ EntityName }}'],
+ [$this->sqlRepositoryVariable, $this->sqlRepositoryName, $this->entityName],
+ $func
+ );
+ }
+ return $functions;
+ }
+
+ public function getConstruct(string $setterSqlStub, string $constructStub)
+ {
+ return str_replace("{{ Setters }}", trim($this->writeSqlAttribute($setterSqlStub, $this->sqlRepositoryVariable, $this->sqlRepositoryName, $this->redisRepositoryVariable, $this->redisRepositoryName)), $constructStub);
+ }
+
+ public function getConstructRedis(string $setterSqlStub, string $constructStub)
+ {
+ return str_replace("{{ Setters }}", trim($this->writeRedisAttribute($setterSqlStub, $this->redisRepositoryVariable, $this->redisRepositoryName)), $constructStub);
+ }
+
+ private function getRedisCashFunctionGetOneBy($strategyName)
+ {
+ $repositoryRedisStubsPath = __DIR__ . '/../../' . 'stubs/Repositories/Redis/getOneBy/base.';
+ return match ($strategyName) {
+ 'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
+ 'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
+ 'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
+ 'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'temporary_cache_strategy.stub'),
+ };
+ }
+
+ private function getRedisCashFunctionGetAllBy($strategyName)
+ {
+ $repositoryRedisStubsPath = __DIR__ . '/../../' . 'stubs/Repositories/Redis/getAllBy/base.';
+ return match ($strategyName) {
+ 'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
+ 'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
+ 'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
+ 'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'temporary_cache_strategy.stub'),
+ };
+ }
+
+ private function getRedisCashFunctionCreate($strategyName)
+ {
+ $repositoryRedisStubsPath = __DIR__ . '/../../' . 'stubs/Repositories/Redis/create/base.';
+ return match ($strategyName) {
+ 'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
+ 'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
+ 'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
+ 'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'temporary_cache_strategy.stub'),
+ };
+ }
+
+ private function getRedisCashFunctionUpdate($strategyName)
+ {
+ $repositoryRedisStubsPath = __DIR__ . '/../../' . 'stubs/Repositories/Redis/update/base.';
+ return match ($strategyName) {
+ 'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
+ 'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
+ 'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
+ 'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'temporary_cache_strategy.stub'),
+ };
+ }
+}
diff --git a/src/Creators/CreatorResource.php b/src/Creators/CreatorResource.php
new file mode 100644
index 0000000..3641637
--- /dev/null
+++ b/src/Creators/CreatorResource.php
@@ -0,0 +1,105 @@
+resourceNamespace;
+ }
+
+ public function createUses(): array
+ {
+ return [
+ "use $this->entityNamespace\\$this->entityName;",
+ 'use Eghamat24\DatabaseRepository\Models\Entity\Entity;',
+ 'use Eghamat24\DatabaseRepository\Models\Resources\Resource;'
+ ];
+ }
+
+ public function getClassName(): string
+ {
+ return $this->resourceName;
+ }
+
+ public function getExtendSection(): string
+ {
+ return 'extends Resource';
+ }
+
+ public function createAttributes(): array
+ {
+ return [];
+ }
+
+ public function createFunctions(): array
+ {
+ $getsStub = file_get_contents($this->resourceStubsPath . 'getter.default.stub');
+ $foreignGetterStub = file_get_contents($this->resourceStubsPath . 'getter.foreign.stub');
+ $foreignFunStub = file_get_contents($this->resourceStubsPath . 'function.foreign.stub');
+ $getterFunStub = file_get_contents($this->resourceStubsPath . 'function.getter.stub');
+
+ $getters = '';
+ foreach ($this->columns as $_column) {
+ $getters .= $this->writeGet($getsStub, $_column->COLUMN_NAME, Str::camel($_column->COLUMN_NAME)) . "\t\t\t";
+ }
+
+ $foreignGetterFunctions = '';
+ if ($this->detectForeignKeys) {
+ $foreignKeys = $this->extractForeignKeys($this->tableName);
+ foreach ($foreignKeys as $_foreignKey) {
+ $foreignGetterFunctions .= $this->writeForeignGetter($foreignGetterStub, $_foreignKey->VARIABLE_NAME, $_foreignKey->ENTITY_DATA_TYPE);
+ }
+ }
+
+ $functions = [];
+ $functions['toArray'] = str_replace(['{{ Getters }}'], [$getters], $getterFunStub);
+ $functions['toArray'] = str_replace(['{{ EntityVariableName }}'], [$this->entityVariableName], $functions['toArray']);
+ $functions['toArrayWithForeignKeys'] = str_replace(['{{ ForeignGetterFunctions }}'], [$foreignGetterFunctions], $foreignFunStub);
+ $functions['toArrayWithForeignKeys'] = str_replace(['{{ EntityVariableName }}'], [$this->entityVariableName], $functions['toArrayWithForeignKeys']);
+
+ return $functions;
+ }
+
+ public function writeGet(string $getterStub, string $columnName, string $attributeName): array|string
+ {
+ $replaceMapping = [
+ '{{ ColumnName }}' => $columnName,
+ '{{ AttributeName }}' => Str::camel($attributeName),
+ ];
+
+ return str_replace(array_keys($replaceMapping), array_values($replaceMapping), $getterStub);
+ }
+
+ public function writeForeignGetter(string $foreignGetterStub, string $columnName, string $attributeName): array|string
+ {
+ $replaceMapping = [
+ '{{ AttributeName }}' => Str::snake($columnName),
+ '{{ GetterName }}' => ucfirst($columnName),
+ '{{ AttributeType }}' => ucfirst($attributeName)
+ ];
+
+ return str_replace(array_keys($replaceMapping), array_values($replaceMapping), $foreignGetterStub);
+ }
+}
diff --git a/src/Creators/IClassCreator.php b/src/Creators/IClassCreator.php
new file mode 100644
index 0000000..2099cc8
--- /dev/null
+++ b/src/Creators/IClassCreator.php
@@ -0,0 +1,22 @@
+ 'bool',
'boolean' => 'bool',
'bit' => 'string',
@@ -18,6 +19,7 @@ trait CustomMySqlQueries
'mediumint' => 'int',
'bigint' => 'int',
'float' => 'float',
+ 'decimal' => 'float',
'double' => 'float',
'json' => 'string',
'char' => 'string',
@@ -37,12 +39,15 @@ trait CustomMySqlQueries
'time' => 'string',
'datetime' => 'string',
'timestamp' => 'string',
+ 'point' => 'string',
+ ];
+
+ protected array $columnTypes = [
+ 'tinyint(1)' => 'bool'
];
/**
* Extract all columns from a given table.
- * @param string $tableName
- * @return Collection
*/
public function getAllColumnsInTable(string $tableName): Collection
{
@@ -53,10 +58,20 @@ public function getAllColumnsInTable(string $tableName): Collection
->get();
}
+ /**
+ * Extract all table names.
+ */
+ public function getAllTableNames(): Collection
+ {
+ return DB::table('INFORMATION_SCHEMA.TABLES')
+ ->select('TABLE_NAME')
+ ->where('TABLE_SCHEMA', config('database.connections.mysql.database'))
+ ->where('TABLE_NAME', '<>', 'migrations')
+ ->get();
+ }
+
/**
* Extract all foreign keys from a given table. Foreign key's relations must define in MySql!
- * @param string $tableName
- * @return Collection
*/
public function extractForeignKeys(string $tableName): Collection
{
@@ -68,10 +83,43 @@ public function extractForeignKeys(string $tableName): Collection
->get();
$foreignKeys->each(function ($foreignKey) {
- $foreignKey->VARIABLE_NAME = camel_case(str_replace('_id', '', $foreignKey->COLUMN_NAME));
- $foreignKey->ENTITY_DATA_TYPE = ucfirst(camel_case(str_singular($foreignKey->REFERENCED_TABLE_NAME)));
+ $foreignKey->VARIABLE_NAME = Str::camel(str_replace('_id', '', $foreignKey->COLUMN_NAME));
+ $foreignKey->ENTITY_DATA_TYPE = ucfirst(Str::camel(Str::singular($foreignKey->REFERENCED_TABLE_NAME)));
});
return $foreignKeys;
}
+
+ /**
+ * Extract all indexes from a given table!
+ */
+ public function extractIndexes(string $tableName): Collection
+ {
+ $indexes = DB::table('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
+ ->where('TABLE_SCHEMA', config('database.connections.mysql.database'))
+ ->where('TABLE_NAME', $tableName)
+ ->where('CONSTRAINT_NAME', '!=', 'PRIMARY')
+ ->whereNull('REFERENCED_TABLE_NAME')
+ ->orderBy('ORDINAL_POSITION')
+ ->get();
+
+ $indexesData = DB::select("SHOW INDEX FROM $tableName WHERE Key_name != 'PRIMARY'");
+
+ collect($indexes)->each(function ($index) use ($indexesData) {
+ $indexesData = collect($indexesData)->where('Column_name', $index->COLUMN_NAME)->first();
+ $index->Non_unique = $indexesData->Non_unique;
+ $index->Index_type = $indexesData->Index_type;
+ });
+
+ return $indexes;
+ }
+
+ public function getDataType(string $columnType, string $dataType): string
+ {
+ if (array_key_exists($columnType, $this->columnTypes)) {
+ return $this->columnTypes[$columnType];
+ }
+
+ return $this->dataTypes[$dataType];
+ }
}
diff --git a/src/DatabaseRepositoryServiceProvider.php b/src/DatabaseRepositoryServiceProvider.php
index 7c9eeee..22f4700 100644
--- a/src/DatabaseRepositoryServiceProvider.php
+++ b/src/DatabaseRepositoryServiceProvider.php
@@ -1,15 +1,17 @@
baseModelStubPath = __DIR__ . '/../stubs/Models/PHP' .env('REPOSITORY_PHP_VERSION', '8.0');
}
/**
@@ -46,77 +44,11 @@ public function offerPublishing(): void
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/repository.php' => $this->app->configPath('repository.php'),
- ], 'repository-config');
+ ], 'database-repository-config');
- $this->publishEnums();
- $this->publishEntities();
- $this->publishFactories();
- $this->publishResources();
- $this->publishRepositories();
}
}
- private function publishEnums(): void
- {
- $publishPath = $this->app->basePath(config('repository.path.relative.enums', 'app/Models/Enums/'));
-
- $this->publishes([
- $this->baseModelStubPath . '/Enums/Enum.stub' => $publishPath . 'Enum.php',
- ], ['repository-base-classes', 'repository-base-enum']);
-
- $this->publishes([
- $this->baseModelStubPath . '/Enums/GriewFilterOperator.stub' => $publishPath . 'GriewFilterOperator.php',
- ], ['repository-base-classes', 'repository-griew-enums']);
- }
-
- private function publishEntities(): void
- {
- $publishPath = $this->app->basePath(config('repository.path.relative.entities', 'app/Models/Entities/'));
-
- $this->publishes([
- $this->baseModelStubPath . '/Entity/Entity.stub' => $publishPath . 'Entity.php',
- ], ['repository-base-classes', 'repository-base-entity']);
- }
-
- private function publishFactories(): void
- {
- $publishPath = $this->app->basePath(config('repository.path.relative.factories', 'app/Models/Factories/'));
-
- $this->publishes([
- $this->baseModelStubPath . '/Factory/Factory.stub' => $publishPath . 'Factory.php',
- ], ['repository-base-classes', 'repository-base-factory']);
-
- $this->publishes([
- $this->baseModelStubPath . '/Factory/IFactory.stub' => $publishPath . 'IFactory.php',
- ], ['repository-base-classes', 'repository-base-factory']);
- }
-
- private function publishResources(): void
- {
- $publishPath = $this->app->basePath(config('repository.path.relative.resources', 'app/Models/Resources/'));
-
- $this->publishes([
- $this->baseModelStubPath . '/Resource/Resource.stub' => $publishPath . 'Resource.php',
- ], ['repository-base-classes', 'repository-base-resource']);
-
- $this->publishes([
- $this->baseModelStubPath . '/Resource/IResource.stub' => $publishPath . 'IResource.php',
- ], ['repository-base-classes', 'repository-base-resource']);
- }
-
- private function publishRepositories(): void
- {
- $publishPath = $this->app->basePath(config('repository.path.relative.repositories', 'app/Models/Repositories/'));
-
- $this->publishes([
- $this->baseModelStubPath . '/Repository/MySqlRepository.stub' => $publishPath . 'MySqlRepository.php',
- ], ['repository-base-classes', 'repository-base-mysql-repository']);
-
- $this->publishes([
- $this->baseModelStubPath . '/Repository/RedisRepository.stub' => $publishPath . 'RedisRepository.php',
- ], ['repository-base-classes', 'repository-base-redis-repository']);
- }
-
/**
* Register custom commands.
*/
@@ -131,6 +63,10 @@ private function registerCommands(): void
return new MakeEntity();
});
+ $this->app->singleton('repository.make-enum', function () {
+ return new MakeEnum();
+ });
+
$this->app->singleton('repository.make-factory', function () {
return new MakeFactory();
});
@@ -158,12 +94,15 @@ private function registerCommands(): void
$this->commands([
MakeAll::class,
MakeEntity::class,
+ MakeEnum::class,
MakeFactory::class,
MakeInterfaceRepository::class,
MakeMySqlRepository::class,
MakeRedisRepository::class,
MakeRepository::class,
- MakeResource::class
+ MakeResource::class,
+
+
]);
}
}
diff --git a/stubs/Models/PHP8.0/Entity/Entity.stub b/src/Models/Entity/Entity.php
similarity index 77%
rename from stubs/Models/PHP8.0/Entity/Entity.stub
rename to src/Models/Entity/Entity.php
index 08af3bf..05c31ad 100644
--- a/stubs/Models/PHP8.0/Entity/Entity.stub
+++ b/src/Models/Entity/Entity.php
@@ -1,8 +1,9 @@
$function($value);
- }
- }
-
- public function __get($name)
- {
- if (property_exists($this, $name)) {
- $function = camel_case('get_' . snake_case($name));
- return $this->$function();
- }
- }
-
public function __isset($name)
{
return property_exists($this, $name);
@@ -43,7 +23,6 @@ public function __isset($name)
/**
* Make all variables of the object as null
- * @return $this
*/
public function clearVariables(): self
{
@@ -54,9 +33,6 @@ public function clearVariables(): self
return $this;
}
- /**
- * @return int
- */
public function getPrimaryKey(): int
{
return $this->getId();
@@ -90,16 +66,23 @@ public function storeOriginals()
$this->originals = $this->toArray();
}
+ /**
+ * empty an array of attributes original value
+ */
+ public function emptyOriginals()
+ {
+ $this->originals = [];
+ }
+
/**
* get an Array of Changed Attributes
- * @return array
*/
public function changedAttributesName(): array
{
$changedAttributes = [];
$attributes = $this->toArray();
foreach ($attributes as $key => $value) {
- if (isset($this->originals[$key]) && $value !== $this->originals[$key] && ! ((is_array($this->originals[$key]) || is_object($this->originals[$key])))) {
+ if (isset($this->originals[$key]) && $value !== $this->originals[$key] && !((is_array($this->originals[$key]) || is_object($this->originals[$key])))) {
$changedAttributes[] = $key;
}
}
@@ -108,7 +91,6 @@ public function changedAttributesName(): array
/**
* get an Array of Changed Attributes with new values
- * @return array
*/
public function getDirty(): array
{
@@ -124,7 +106,6 @@ public function getDirty(): array
/**
* get an Array of Changed Attributes with original values
- * @return array
*/
public function getChanges(): array
{
@@ -139,7 +120,6 @@ public function getChanges(): array
/**
* is any attribute changed?
- * @return bool
*/
public function isDirty(): bool
{
@@ -150,4 +130,4 @@ public function jsonSerialize()
{
return $this->toArray();
}
-}
\ No newline at end of file
+}
diff --git a/src/Models/Enums/DataTypeEnum.php b/src/Models/Enums/DataTypeEnum.php
new file mode 100644
index 0000000..6de9165
--- /dev/null
+++ b/src/Models/Enums/DataTypeEnum.php
@@ -0,0 +1,82 @@
+ self::BOOLEAN_TYPE,
+
+ self::BIT,
+ self::JSON,
+ self::CHAR,
+ self::VARCHAR,
+ self::BINARY,
+ self::VARBINARY,
+ self::DATETIME,
+ self::TIME,
+ self::DATE,
+ self::ENUM,
+ self::LONGBLOB,
+ self::LONGTEXT,
+ self::MEDIUMBLOB,
+ self::MEDIUMTEXT,
+ self::BLOB,
+ self::TEXT,
+ self::TINYTEXT,
+ self::TINYBLOB,
+ self::TIMESTAMP,
+ self::POINT => self::STRING_TYPE,
+
+ self::INT,
+ self::INTEGER,
+ self::TINYINT,
+ self::SMALLINT,
+ self::MEDIUMINT,
+ self::BIGINT => self::INTEGER_TYPE,
+
+ self::FLOAT,
+ self::DOUBLE => self::FLOAT_TYPE,
+ };
+ }
+}
diff --git a/stubs/Models/PHP8.0/Enums/Enum.stub b/src/Models/Enums/Enum.php
similarity index 85%
rename from stubs/Models/PHP8.0/Enums/Enum.stub
rename to src/Models/Enums/Enum.php
index 291fe58..000c6fc 100644
--- a/stubs/Models/PHP8.0/Enums/Enum.stub
+++ b/src/Models/Enums/Enum.php
@@ -1,6 +1,6 @@
getConstants();
}
- public function getValue(int|string $key): ?string
+ public function getValue(int|string $key): null|string
{
$list = $this->getList();
$keys = array_keys($list);
@@ -33,4 +33,4 @@ public function indexOf(int|string $value): false|int|string
return array_search($value, $values, true);
}
-}
\ No newline at end of file
+}
diff --git a/stubs/Models/PHP8.0/Enums/GriewFilterOperator.stub b/src/Models/Enums/GriewFilterOperator.php
similarity index 95%
rename from stubs/Models/PHP8.0/Enums/GriewFilterOperator.stub
rename to src/Models/Enums/GriewFilterOperator.php
index a9c5439..a973d89 100644
--- a/stubs/Models/PHP8.0/Enums/GriewFilterOperator.stub
+++ b/src/Models/Enums/GriewFilterOperator.php
@@ -1,6 +1,6 @@
getCache()->tags($this->cacheTag)->get($cacheKey);
+ }
+
+ /**
+ * @param string $cacheKey
+ * @param mixed $data
+ * @param $time
+ * @return mixed
+ */
+ public function put(string $cacheKey, $data, $seconds)
+ {
+ $this->getCache()->tags($this->cacheTag)->put($cacheKey, $data, $seconds);
+ }
+
+ /**
+ * @return mixed
+ */
+ public function clear($cacheKey)
+ {
+ return $this->getCache()->tags($this->cacheTag)->forget($cacheKey);
+ }
+}
diff --git a/src/Models/Repositories/CacheStrategies/QueryCacheStrategy.php b/src/Models/Repositories/CacheStrategies/QueryCacheStrategy.php
new file mode 100644
index 0000000..29baf04
--- /dev/null
+++ b/src/Models/Repositories/CacheStrategies/QueryCacheStrategy.php
@@ -0,0 +1,45 @@
+getCache()->tags($this->cacheTag)->get($cacheKey);
+ }
+
+ /**
+ * @param string $cacheKey
+ * @param mixed $data
+ * @return mixed
+ */
+ public function put(string $cacheKey, $data)
+ {
+ return $this->getCache()->tags($this->cacheTag)->forever($cacheKey, $data);
+ }
+
+ /**
+ * @return mixed
+ */
+ public function clear()
+ {
+ return $this->getCache()->tags($this->cacheTag)->flush();
+ }
+}
\ No newline at end of file
diff --git a/src/Models/Repositories/CacheStrategies/SingleKeyCacheStrategy.php b/src/Models/Repositories/CacheStrategies/SingleKeyCacheStrategy.php
new file mode 100644
index 0000000..9f9b025
--- /dev/null
+++ b/src/Models/Repositories/CacheStrategies/SingleKeyCacheStrategy.php
@@ -0,0 +1,35 @@
+getCache()->forever($this->cacheKey, $entities);
+ }
+
+ /**
+ * @return mixed
+ */
+ public function get()
+ {
+ return $this->getCache()->get($this->cacheKey);
+ }
+
+ /**
+ * @return mixed
+ */
+ public function clear()
+ {
+ return $this->getCache()->forget($this->cacheKey);
+ }
+}
\ No newline at end of file
diff --git a/src/Models/Repositories/CacheStrategies/TemporaryCacheStrategy.php b/src/Models/Repositories/CacheStrategies/TemporaryCacheStrategy.php
new file mode 100644
index 0000000..dff9af7
--- /dev/null
+++ b/src/Models/Repositories/CacheStrategies/TemporaryCacheStrategy.php
@@ -0,0 +1,40 @@
+getCache()->tags($this->cacheTag)->get($cacheKey);
+ }
+
+ /**
+ * @param string $cacheKey
+ * @param mixed $data
+ * @param DateInterval|DateTimeInterface|int $ttl
+ * @return bool
+ */
+ public function put(string $cacheKey, mixed $data, DateInterval|DateTimeInterface|int $ttl): bool
+ {
+ return $this->getCache()->tags($this->cacheTag)->put($cacheKey, $data, $ttl);
+ }
+}
diff --git a/stubs/Models/PHP7.4/Repository/MySqlRepository.stub b/src/Models/Repositories/MySqlRepository.php
similarity index 85%
rename from stubs/Models/PHP7.4/Repository/MySqlRepository.stub
rename to src/Models/Repositories/MySqlRepository.php
index 78a62b8..366b2df 100644
--- a/stubs/Models/PHP7.4/Repository/MySqlRepository.stub
+++ b/src/Models/Repositories/MySqlRepository.php
@@ -1,18 +1,18 @@
newQuery();
@@ -99,10 +90,6 @@ public function exists($columnValue, $columnName = null)
/**
* this is for validation purpose look at AppServiceProvider
- * @param $attribute
- * @param $value
- * @param null $ignoredPrimaryKey
- * @return bool
*/
public function valueExists($attribute, $value, $ignoredPrimaryKey = null)
{
@@ -121,10 +108,7 @@ public function valueExists($attribute, $value, $ignoredPrimaryKey = null)
return $query->exists();
}
- /**
- * @param Entity $model
- */
- public function updateOrCreate($model)
+ public function updateOrCreate(Entity $model)
{
if ($this->exists($model->getPrimaryKey())) {
$this->update($model);
@@ -136,7 +120,7 @@ public function updateOrCreate($model)
/**
* @param Entity $model
*/
- public function createIfNotExists($model)
+ public function createIfNotExists(Entity $model)
{
if (!$this->exists($model->getPrimaryKey())) {
$this->create($model);
@@ -157,16 +141,7 @@ public function getMaxId()
}
}
- /**
- * @param Builder $query
- * @param int $offset
- * @param int $count
- * @param int|null $total
- * @param array $orders
- * @param array $filters
- * @return Builder
- */
- protected function processGridViewQuery(Builder $query, ?int &$total, int $offset = 0, int $count = 0, array $orders = [], array $filters = []): Builder
+ protected function processGridViewQuery(Builder $query, null|int &$total, int $offset = 0, int $count = 0, array $orders = [], array $filters = []): Builder
{
if ($orders) {
$query = $this->processOrder($query, $orders);
@@ -207,7 +182,7 @@ protected function processOrder(Builder $query, array $orders): Builder
protected function processFilter(Builder $query, array $filters): Builder
{
foreach ($filters as $filter) {
- switch (strtolower(snake_case($filter->operator))) {
+ switch (strtolower(Str::snake($filter->operator))) {
case GriewFilterOperator::IS_NULL:
$query->whereNull($filter->name);
break;
diff --git a/src/Models/Repositories/RedisRepository.php b/src/Models/Repositories/RedisRepository.php
new file mode 100644
index 0000000..6e16c97
--- /dev/null
+++ b/src/Models/Repositories/RedisRepository.php
@@ -0,0 +1,128 @@
+cache = app('cache');
+ }
+
+ protected function getCache(): CacheManager
+ {
+ return $this->cache;
+ }
+
+ /**
+ * @param Collection $entities
+ * @param Filter[]|Collection $filters
+ * @return Collection
+ */
+ protected function processFilterWithCollection($entities, $filters)
+ {
+ foreach ($filters as $filter) {
+ $columnName = camel_case($filter->getColumnName());
+ $value = $filter->getValue();
+
+ switch ($filter->getOperand()) {
+ case 'IsEqualTo':
+ $entities = $entities->where($columnName, '=', $value);
+ break;
+ case 'IsEqualToOrNull':
+ $entities = $entities->filter(function ($entity, $key) use ($columnName, $value) {
+ return ($entity->$columnName == $value || empty($entity->$columnName));
+ });
+ break;
+ case 'IsNull':
+ $entities = $entities->whereNull($columnName);
+ break;
+ case 'IsNotEqualTo':
+ $entities = $entities->where($columnName, '<>', $value);
+ break;
+ case 'IsNotNull':
+ $entities = $entities->whereNotNull($columnName);
+ break;
+ case 'StartWith':
+ $entities = $entities->filter(function ($entity) use ($columnName, $value) {
+ return false !== Str::startsWith($entity->$columnName, $value);
+ });
+ break;
+ case 'DoesNotContains':
+ $entities = $entities->filter(function ($entity) use ($columnName, $value) {
+ return false === Str::contains($entity->$columnName, $value);
+ });
+ break;
+ case 'Contains':
+ $entities = $entities->filter(function ($entity) use ($columnName, $value) {
+ return false !== Str::contains($entity->$columnName, $value);
+ });
+ break;
+ case 'EndsWith':
+ $entities = $entities->filter(function ($entity) use ($columnName, $value) {
+ return false !== Str::endsWith($entity->$columnName, $value);
+ });
+ break;
+ case 'In':
+ $entities = $entities->whereIn($columnName, $value);
+ break;
+ case 'NotIn':
+ $entities = $entities->whereNotIn($columnName, $value);
+ break;
+ case 'Between':
+ $entities = $entities->whereBetween($columnName, $value);
+ break;
+ case 'IsGreaterThanOrEqualTo':
+ $entities = $entities->where($columnName, '>=', $value);
+ break;
+ case 'IsGreaterThanOrNull':
+ $entities = $entities->filter(function ($entity) use ($columnName, $value) {
+ return ($entity->$columnName > $value || empty($entity->$columnName));
+ });
+ break;
+ case 'IsGreaterThan':
+ $entities = $entities->where($columnName, '>', $value);
+ break;
+ case 'IsLessThanOrEqualTo':
+ $entities = $entities->where($columnName, '<=', $value);
+ break;
+ case 'IsLessThan':
+ $entities = $entities->where($columnName, '<', $value);
+ break;
+ case 'IsAfterThanOrEqualTo':
+ $entities = $entities->where($columnName, '>=', $value);
+ break;
+ case 'IsAfterThan':
+ $entities = $entities->where($columnName, '>', $value);
+ break;
+ case 'IsBeforeThanOrEqualTo':
+ $entities = $entities->where($columnName, '<=', $value);
+ break;
+ case 'IsBeforeThan':
+ $entities = $entities->where($columnName, '<', $value);
+ break;
+ }
+ }
+
+ return $entities;
+ }
+
+ /**
+ * @param Collection $entities
+ * @param Order[]|Collection $orders
+ * @return Collection
+ */
+ protected function processOrderWithCollection($entities, $orders)
+ {
+ $sortBy = [];
+ foreach ($orders as $order) {
+ $sortBy[$order->getColumnName()] = $order->getValue();
+ }
+
+ return $entities->sortBy($sortBy);
+ }
+}
diff --git a/stubs/Models/PHP8.0/Resource/IResource.stub b/src/Models/Resources/IResource.php
similarity index 62%
rename from stubs/Models/PHP8.0/Resource/IResource.stub
rename to src/Models/Resources/IResource.php
index a44a374..22a39d8 100644
--- a/stubs/Models/PHP8.0/Resource/IResource.stub
+++ b/src/Models/Resources/IResource.php
@@ -1,9 +1,9 @@
{{ AttributeName }} = $entity->{{ DatabaseAttributeName }};
diff --git a/stubs/Models/PHP7.0/Entity/Entity.stub b/stubs/Models/PHP7.0/Entity/Entity.stub
deleted file mode 100644
index 86999b0..0000000
--- a/stubs/Models/PHP7.0/Entity/Entity.stub
+++ /dev/null
@@ -1,158 +0,0 @@
-$function($value);
- }
- }
-
- public function __get($name)
- {
- if (property_exists($this, $name)) {
- $function = camel_case('get_' . snake_case($name));
- return $this->$function();
- }
- }
-
- public function __isset($name)
- {
- return property_exists($this, $name);
- }
-
- /**
- * Make all variables of the object as null
- * @return $this
- */
- public function clearVariables()
- {
- $attributes = get_object_vars($this);
- foreach ($attributes as $attributeName => $attributeValue) {
- $this->$attributeName = null;
- }
- return $this;
- }
-
- /**
- * @return int
- */
- public function getPrimaryKey(): int
- {
- return $this->getId();
- }
-
- /**
- * Fill the model
- */
- public function fill()
- {
-
- }
-
- /**
- * get an Array of current Attributes value
- * @return array
- */
- public function toArray(): array
- {
- $attributes = get_object_vars($this);
-
- unset($attributes['originals']);
-
- return $attributes;
- }
-
- /**
- * store an array of attributes original value
- */
- public function storeOriginals()
- {
- $this->originals = $this->toArray();
- }
-
- /**
- * get an Array of Changed Attributes
- * @return array
- */
- public function changedAttributesName()
- {
- $changedAttributes = [];
- $attributes = $this->toArray();
- foreach ($attributes as $key => $value) {
- if (isset($this->originals[$key])) {
- if ($value != $this->originals[$key] && !((is_array($this->originals[$key]) || is_object($this->originals[$key])))) {
- $changedAttributes[] = $key;
- }
- }
- }
- return $changedAttributes;
- }
-
- /**
- * get an Array of Changed Attributes with new values
- * @return array
- */
- public function getDirty()
- {
- $dirty = [];
- $attributes = $this->toArray();
-
- foreach ($this->changedAttributesName() as $key) {
- $dirty[$key] = $attributes[$key];
- }
-
- return $dirty;
- }
-
- /**
- * get an Array of Changed Attributes with original values
- * @return array
- */
- public function getChanges()
- {
- $changes = [];
-
- foreach ($this->changedAttributesName() as $key) {
- $changes[$key] = $this->originals[$key];
- }
-
- return $changes;
- }
-
- /**
- * is any attribute changed?
- * @return bool
- */
- public function isDirty()
- {
- if (count($this->changedAttributesName()) > 0) return true;
-
- return false;
- }
-
- public function jsonSerialize(): array
- {
- return $this->toArray();
- }
-}
\ No newline at end of file
diff --git a/stubs/Models/PHP7.0/Factory/Factory.stub b/stubs/Models/PHP7.0/Factory/Factory.stub
deleted file mode 100644
index 8f9e681..0000000
--- a/stubs/Models/PHP7.0/Factory/Factory.stub
+++ /dev/null
@@ -1,26 +0,0 @@
-push($this->makeEntityFromStdClass($_entity));
- }
-
- return $entityCollection;
- }
-}
\ No newline at end of file
diff --git a/stubs/Models/PHP7.0/Factory/IFactory.stub b/stubs/Models/PHP7.0/Factory/IFactory.stub
deleted file mode 100644
index 923cd1b..0000000
--- a/stubs/Models/PHP7.0/Factory/IFactory.stub
+++ /dev/null
@@ -1,22 +0,0 @@
-$function($value);
- }
- }
-
- public function __get($name)
- {
- if (property_exists($this, $name)) {
- $function = camel_case('get_' . snake_case($name));
- return $this->$function();
- }
- }
-
- public function __isset($name)
- {
- return property_exists($this, $name);
- }
-
- /**
- * Make all variables of the object as null
- * @return $this
- */
- public function clearVariables()
- {
- $attributes = get_object_vars($this);
- foreach ($attributes as $attributeName => $attributeValue) {
- $this->$attributeName = null;
- }
- return $this;
- }
-
- /**
- * @return int
- */
- public function getPrimaryKey()
- {
- return $this->getId();
- }
-
- /**
- * Fill the model
- */
- public function fill()
- {
-
- }
-
- /**
- * get an Array of current Attributes value
- */
- public function toArray(): array
- {
- $attributes = get_object_vars($this);
-
- unset($attributes['originals']);
-
- return $attributes;
- }
-
- /**
- * store an array of attributes original value
- */
- public function storeOriginals()
- {
- $this->originals = $this->toArray();
- }
-
- /**
- * get an Array of Changed Attributes
- * @return array
- */
- public function changedAttributesName()
- {
- $changedAttributes = [];
- $attributes = $this->toArray();
- foreach ($attributes as $key => $value) {
- if (isset($this->originals[$key])) {
- if ($value != $this->originals[$key] && !((is_array($this->originals[$key]) || is_object($this->originals[$key])))) {
- $changedAttributes[] = $key;
- }
- }
- }
- return $changedAttributes;
- }
-
- /**
- * get an Array of Changed Attributes with new values
- * @return array
- */
- public function getDirty()
- {
- $dirty = [];
- $attributes = $this->toArray();
-
- foreach ($this->changedAttributesName() as $key) {
- $dirty[$key] = $attributes[$key];
- }
-
- return $dirty;
- }
-
- /**
- * get an Array of Changed Attributes with original values
- * @return array
- */
- public function getChanges()
- {
- $changes = [];
-
- foreach ($this->changedAttributesName() as $key) {
- $changes[$key] = $this->originals[$key];
- }
-
- return $changes;
- }
-
- /**
- * is any attribute changed?
- * @return bool
- */
- public function isDirty()
- {
- if (count($this->changedAttributesName()) > 0) return true;
-
- return false;
- }
-
- public function jsonSerialize()
- {
- return $this->toArray();
- }
-}
\ No newline at end of file
diff --git a/stubs/Models/PHP7.4/Enums/Enum.stub b/stubs/Models/PHP7.4/Enums/Enum.stub
deleted file mode 100644
index 8a27bf7..0000000
--- a/stubs/Models/PHP7.4/Enums/Enum.stub
+++ /dev/null
@@ -1,44 +0,0 @@
-getConstants();
- }
-
- /**
- * @param int|string $key
- * @return string|null
- */
- public function getValue($key): ?string
- {
- $list = $this->getList();
- $keys = array_keys($list);
- $key = is_numeric($key) ? (int)$key : $key;
-
- if (is_int($key) && $key < count($keys)) {
- $value = $list[$keys[$key]];
- } else {
- $value = $list[strtoupper($key)];
- }
-
- return $value;
- }
-
- /**
- * @param int|string $value
- * @return false|int|string
- */
- public function indexOf($value)
- {
- $list = $this->getList();
- $values = array_values($list);
-
- return array_search($value, $values, true);
- }
-}
\ No newline at end of file
diff --git a/stubs/Models/PHP7.4/Enums/GriewFilterOperator.stub b/stubs/Models/PHP7.4/Enums/GriewFilterOperator.stub
deleted file mode 100644
index a9c5439..0000000
--- a/stubs/Models/PHP7.4/Enums/GriewFilterOperator.stub
+++ /dev/null
@@ -1,29 +0,0 @@
-push($this->makeEntityFromStdClass($_entity));
- }
-
- return $entityCollection;
- }
-}
\ No newline at end of file
diff --git a/stubs/Models/PHP7.4/Factory/IFactory.stub b/stubs/Models/PHP7.4/Factory/IFactory.stub
deleted file mode 100644
index 923cd1b..0000000
--- a/stubs/Models/PHP7.4/Factory/IFactory.stub
+++ /dev/null
@@ -1,22 +0,0 @@
-alternativeDbConnection = null;
- }
-
- /**
- * Notice: this function cannot be used in async jobs because the connection is not serializable!
- * @param ConnectionInterface $connection
- */
- public function changeDatabaseConnection($connection)
- {
- $this->alternativeDbConnection = $connection;
- }
-
- public function newQuery(): Builder
- {
- if (is_null($this->alternativeDbConnection)) {
- $query = app('db')->table($this->table);
- } else {
- $query = $this->alternativeDbConnection->table($this->table);
- }
-
-// if ($this->softDelete) {
-// if (!$this->withTrashed) {
-// $query = $query->whereNull('deleted_at');
-// } else {
-// $this->withTrashed = false;
-// }
-// }
-
- return $query;
- }
-
-// public function withTrashed(): MySqlRepository
-// {
-// $this->withTrashed = true;
-//
-// return $this;
-// }
-
- /**
- * @param int|null $total
- * @param int $offset
- * @param int $count
- * @param array $orders
- * @param array $filters
- * @return Collection
- */
- public function getAllForGridView(?int &$total, int $offset = 0, int $count = 0, array $orders = [], array $filters = []): Collection
- {
- $query = $this->newQuery();
-
- $result = $this->processGridViewQuery($query, $total, $offset, $count, $orders, $filters)->get();
-
- return $this->factory->makeCollectionOfEntities($result);
- }
-
- public function raw($str)
- {
- if (is_null($this->alternativeDbConnection)) {
- return app('db')->raw($str);
- }
- return $this->alternativeDbConnection->raw($str);
- }
-
- public function exists($columnValue, $columnName = null)
- {
- if (is_null($columnName)) {
- $columnName = $this->primaryKey;
- }
- return $this->newQuery()->where($columnName, $columnValue)->exists();
- }
-
- /**
- * this is for validation purpose look at AppServiceProvider
- * @param $attribute
- * @param $value
- * @param null $ignoredPrimaryKey
- * @return bool
- */
- public function valueExists($attribute, $value, $ignoredPrimaryKey = null)
- {
- $query = $this->newQuery();
-
- if ($this->softDelete) {
- $query->whereNull('deleted_at');
- }
-
- $query->where($attribute, $value);
-
- if (!is_null($ignoredPrimaryKey)) {
- $query->where($this->primaryKey, '<>', $ignoredPrimaryKey);
- }
-
- return $query->exists();
- }
-
- /**
- * @param Entity $model
- */
- public function updateOrCreate($model)
- {
- if ($this->exists($model->getPrimaryKey())) {
- $this->update($model);
- } else {
- $this->create($model);
- }
- }
-
- /**
- * @param Entity $model
- */
- public function createIfNotExists($model)
- {
- if (!$this->exists($model->getPrimaryKey())) {
- $this->create($model);
- }
- }
-
- /**
- * It returns maximum row Id
- * @return int|mixed
- */
- public function getMaxId()
- {
- $entity = $this->newQuery()->orderByDesc($this->primaryKey)->first();
- if ($entity) {
- return $entity->id;
- } else {
- return 0;
- }
- }
-
- /**
- * @param Builder $query
- * @param int $offset
- * @param int $count
- * @param int|null $total
- * @param array $orders
- * @param array $filters
- * @return Builder
- */
- protected function processGridViewQuery(Builder $query, ?int &$total, int $offset = 0, int $count = 0, array $orders = [], array $filters = []): Builder
- {
- if ($orders) {
- $query = $this->processOrder($query, $orders);
- }
-
- if ($filters) {
- $query = $this->processFilter($query, $filters);
- }
-
- $total = $query->count();
-
- if ($count) {
- $query->offset($offset);
- $query->limit($count);
- }
-
- return $query;
- }
-
- /**
- * @param Builder $query
- * @param array $orders
- * @return Builder
- */
- protected function processOrder(Builder $query, array $orders): Builder
- {
- foreach ($orders as $order) {
- $query->orderBy($order->name, $order->type);
- }
- return $query;
- }
-
- /**
- * @param Builder $query
- * @param array $filters
- * @return Builder
- */
- protected function processFilter(Builder $query, array $filters): Builder
- {
- foreach ($filters as $filter) {
- switch (strtolower(snake_case($filter->operator))) {
- case GriewFilterOperator::IS_NULL:
- $query->whereNull($filter->name);
- break;
- case GriewFilterOperator::IS_NOT_NULL:
- $query->whereNotNull($filter->name);
- break;
- case GriewFilterOperator::IS_EQUAL_TO:
- if (is_string($filter->operand1) && Str::contains($filter->operand1, '|')) {
- // create in functionality with equal string
- $arr = array_filter(explode('|', $filter->operand1));
- $query->whereIn($filter->name, $arr);
- } else {
- $query->where($filter->name, '=', $filter->operand1);
- }
- break;
- case GriewFilterOperator::IS_NOT_EQUAL_TO:
- if (is_string($filter->operand1) && Str::contains($filter->operand1, '|')) {
- // create in functionality with equal string
- $arr = array_filter(explode('|', $filter->operand1));
- $query->whereNotIn($filter->name, $arr);
- } else {
- $query->where($filter->name, '<>', $filter->operand1);
- }
- break;
- case GriewFilterOperator::START_WITH:
- $query->where($filter->name, 'LIKE', $filter->operand1 . '%');
- break;
- case GriewFilterOperator::DOES_NOT_CONTAINS:
- $query->where($filter->name, 'NOT LIKE', '%' . $filter->operand1 . '%');
- break;
- case GriewFilterOperator::CONTAINS:
- $query->where($filter->name, 'LIKE', '%' . $filter->operand1 . '%');
- break;
- case GriewFilterOperator::ENDS_WITH:
- $query->where($filter->name, 'LIKE', '%' . $filter->operand1);
- break;
- case GriewFilterOperator::IN:
- $query->whereIn($filter->name, $filter->operand1);
- break;
- case GriewFilterOperator::NOT_IN:
- $query->whereNotIn($filter->name, $filter->operand1);
- break;
- case GriewFilterOperator::BETWEEN:
- $query->whereBetween($filter->name, array($filter->operand1, $filter->operand2));
- break;
- case GriewFilterOperator::IS_AFTER_THAN_OR_EQUAL_TO:
- case GriewFilterOperator::IS_GREATER_THAN_OR_EQUAL_TO:
- $query->where($filter->name, '>=', $filter->operand1);
- break;
- case GriewFilterOperator::IS_AFTER_THAN:
- case GriewFilterOperator::IS_GREATER_THAN:
- $query->where($filter->name, '>', $filter->operand1);
- break;
- case GriewFilterOperator::IS_LESS_THAN_OR_EQUAL_TO:
- case GriewFilterOperator::IS_BEFORE_THAN_OR_EQUAL_TO:
- $query->where($filter->name, '<=', $filter->operand1);
- break;
- case GriewFilterOperator::IS_LESS_THAN:
- case GriewFilterOperator::IS_BEFORE_THAN:
- $query->where($filter->name, '<', $filter->operand1);
- break;
- }
- }
-
- return $query;
- }
-}
diff --git a/stubs/Models/PHP8.0/Repository/RedisRepository.stub b/stubs/Models/PHP8.0/Repository/RedisRepository.stub
deleted file mode 100644
index 7fa67b1..0000000
--- a/stubs/Models/PHP8.0/Repository/RedisRepository.stub
+++ /dev/null
@@ -1,20 +0,0 @@
-cache = app('cache');
- }
-
- protected function getCache(): CacheManager
- {
- return $this->cache;
- }
-}
\ No newline at end of file
diff --git a/stubs/Models/PHP8.0/Resource/Resource.stub b/stubs/Models/PHP8.0/Resource/Resource.stub
deleted file mode 100644
index 87bf3b0..0000000
--- a/stubs/Models/PHP8.0/Resource/Resource.stub
+++ /dev/null
@@ -1,30 +0,0 @@
-toArray($_entity);
- }
-
- return $entityArray;
- }
-}
diff --git a/stubs/PHP7.0/repository.entity.accessors.stub b/stubs/PHP7.0/repository.entity.accessors.stub
deleted file mode 100644
index 936ae81..0000000
--- a/stubs/PHP7.0/repository.entity.accessors.stub
+++ /dev/null
@@ -1,16 +0,0 @@
-
- /**
- * @return {{ AttributeType }}
- */
- public function get{{ GetterName }}()
- {
- return $this->{{ AttributeName }};
- }
-
- /**
- * @param {{ AttributeType }} ${{ AttributeName }}
- */
- public function set{{ SetterName }}(${{ AttributeName }})
- {
- $this->{{ AttributeName }} = ${{ AttributeName }};
- }
diff --git a/stubs/PHP7.0/repository.entity.attribute.stub b/stubs/PHP7.0/repository.entity.attribute.stub
deleted file mode 100644
index 19c5df3..0000000
--- a/stubs/PHP7.0/repository.entity.attribute.stub
+++ /dev/null
@@ -1 +0,0 @@
- protected ${{ AttributeName }};
diff --git a/stubs/PHP7.0/repository.entity.class.stub b/stubs/PHP7.0/repository.entity.class.stub
deleted file mode 100644
index 375e887..0000000
--- a/stubs/PHP7.0/repository.entity.class.stub
+++ /dev/null
@@ -1,7 +0,0 @@
-set{{ SetterName }}($entity->{{ AttributeName }});
diff --git a/stubs/PHP7.0/repository.mysql.getAllBy.stub b/stubs/PHP7.0/repository.mysql.getAllBy.stub
deleted file mode 100644
index 710504d..0000000
--- a/stubs/PHP7.0/repository.mysql.getAllBy.stub
+++ /dev/null
@@ -1,13 +0,0 @@
-
- /**
- * @param array ${{ AttributeNamePlural }}
- * @return Collection
- */
- public function getAllBy{{ FunctionNamePlural }}(${{ AttributeNamePlural }})
- {
- ${{ EntityVariableName }} = $this->newQuery()
- ->whereIn('{{ AttributeName }}', ${{ AttributeNamePlural }})
- ->get();
-
- return $this->factory->makeCollectionOfEntities(${{ EntityVariableName }});
- }
diff --git a/stubs/PHP7.0/repository.mysql.getOneBy.stub b/stubs/PHP7.0/repository.mysql.getOneBy.stub
deleted file mode 100644
index 38e18bb..0000000
--- a/stubs/PHP7.0/repository.mysql.getOneBy.stub
+++ /dev/null
@@ -1,13 +0,0 @@
-
- /**
- * @param {{ AttributeType }} ${{ AttributeName }}
- * @return {{ EntityName }}|null
- */
- public function getOneBy{{ FunctionName }}(${{ AttributeName }})
- {
- ${{ EntityVariableName }} = $this->newQuery()
- ->where('{{ AttributeName }}', ${{ AttributeName }})
- ->first();
-
- return ${{ EntityVariableName }} ? $this->factory->makeEntityFromStdClass(${{ EntityVariableName }}) : null;
- }
diff --git a/stubs/PHP7.4/repository.entity.accessors.stub b/stubs/PHP7.4/repository.entity.accessors.stub
deleted file mode 100644
index 91814b6..0000000
--- a/stubs/PHP7.4/repository.entity.accessors.stub
+++ /dev/null
@@ -1,16 +0,0 @@
-
- /**
- * @return {{ AttributeType }}
- */
- public function get{{ GetterName }}(): {{ AttributeType }}
- {
- return $this->{{ AttributeName }};
- }
-
- /**
- * @param {{ AttributeType }} ${{ AttributeName }}
- */
- public function set{{ SetterName }}({{ AttributeType }} ${{ AttributeName }}): void
- {
- $this->{{ AttributeName }} = ${{ AttributeName }};
- }
diff --git a/stubs/PHP7.4/repository.entity.attribute.stub b/stubs/PHP7.4/repository.entity.attribute.stub
deleted file mode 100644
index 6df2dec..0000000
--- a/stubs/PHP7.4/repository.entity.attribute.stub
+++ /dev/null
@@ -1 +0,0 @@
- protected {{ AttributeType }} ${{ AttributeName }};
diff --git a/stubs/PHP7.4/repository.entity.class.stub b/stubs/PHP7.4/repository.entity.class.stub
deleted file mode 100644
index f01693a..0000000
--- a/stubs/PHP7.4/repository.entity.class.stub
+++ /dev/null
@@ -1,8 +0,0 @@
-set{{ SetterName }}($entity->{{ AttributeName }});
diff --git a/stubs/PHP7.4/repository.interface.deleteAndUndelete.stub b/stubs/PHP7.4/repository.interface.deleteAndUndelete.stub
deleted file mode 100644
index 6ab2f28..0000000
--- a/stubs/PHP7.4/repository.interface.deleteAndUndelete.stub
+++ /dev/null
@@ -1,4 +0,0 @@
-
- public function delete({{ EntityName }} ${{ EntityVariableName }}): int;
-
- public function undelete({{ EntityName }} ${{ EntityVariableName }}): int;
diff --git a/stubs/PHP7.4/repository.interface.getOneBy.stub b/stubs/PHP7.4/repository.interface.getOneBy.stub
deleted file mode 100644
index b0ffcfa..0000000
--- a/stubs/PHP7.4/repository.interface.getOneBy.stub
+++ /dev/null
@@ -1,2 +0,0 @@
-
- public function getOneBy{{ FunctionName }}(int ${{ AttributeName }}): ?{{ EntityName }};
diff --git a/stubs/PHP7.4/repository.mysql.create.stub b/stubs/PHP7.4/repository.mysql.create.stub
deleted file mode 100644
index c164a23..0000000
--- a/stubs/PHP7.4/repository.mysql.create.stub
+++ /dev/null
@@ -1,15 +0,0 @@
-
- /**
- * @param {{ EntityName }} ${{ EntityVariableName }}
- * @return {{ EntityName }}
- */
- public function create({{ EntityName }} ${{ EntityVariableName }}): {{ EntityName }}
- {
- $id = $this->newQuery()
- ->insertGetId([
- ]);
-
- ${{ EntityVariableName }}->setId($id);
-
- return ${{ EntityVariableName }};
- }
diff --git a/stubs/PHP7.4/repository.mysql.deleteAndUndelete.stub b/stubs/PHP7.4/repository.mysql.deleteAndUndelete.stub
deleted file mode 100644
index 4bafb41..0000000
--- a/stubs/PHP7.4/repository.mysql.deleteAndUndelete.stub
+++ /dev/null
@@ -1,26 +0,0 @@
-
- /**
- * @param {{ EntityName }} ${{ EntityVariableName }}
- * @return int
- */
- public function delete({{ EntityName }} ${{ EntityVariableName }}): int
- {
- return $this->newQuery()
- ->where($this->primaryKey, ${{ EntityVariableName }}->getPrimaryKey())
- ->update([
- 'deleted_at' => date('Y-m-d H:i:s'),
- ]);
- }
-
- /**
- * @param {{ EntityName }} ${{ EntityVariableName }}
- * @return int
- */
- public function undelete({{ EntityName }} ${{ EntityVariableName }}): int
- {
- return $this->newQuery()
- ->where($this->primaryKey, ${{ EntityVariableName }}->getPrimaryKey())
- ->update([
- 'deleted_at' => null,
- ]);
- }
diff --git a/stubs/PHP7.4/repository.mysql.getAllBy.stub b/stubs/PHP7.4/repository.mysql.getAllBy.stub
deleted file mode 100644
index c63e3da..0000000
--- a/stubs/PHP7.4/repository.mysql.getAllBy.stub
+++ /dev/null
@@ -1,13 +0,0 @@
-
- /**
- * @param array ${{ AttributeNamePlural }}
- * @return Collection
- */
- public function getAllBy{{ FunctionNamePlural }}(array ${{ AttributeNamePlural }}): Collection
- {
- ${{ EntityVariableName }} = $this->newQuery()
- ->whereIn('{{ ColumnName }}', ${{ AttributeNamePlural }})
- ->get();
-
- return $this->factory->makeCollectionOfEntities(${{ EntityVariableName }});
- }
diff --git a/stubs/PHP7.4/repository.mysql.getOneBy.stub b/stubs/PHP7.4/repository.mysql.getOneBy.stub
deleted file mode 100644
index ca63bee..0000000
--- a/stubs/PHP7.4/repository.mysql.getOneBy.stub
+++ /dev/null
@@ -1,13 +0,0 @@
-
- /**
- * @param {{ AttributeType }} ${{ AttributeName }}
- * @return {{ EntityName }}|null
- */
- public function getOneBy{{ FunctionName }}(int ${{ AttributeName }}): ?{{ EntityName }}
- {
- ${{ EntityVariableName }} = $this->newQuery()
- ->where('{{ ColumnName }}', ${{ AttributeName }})
- ->first();
-
- return ${{ EntityVariableName }} ? $this->factory->makeEntityFromStdClass(${{ EntityVariableName }}) : null;
- }
diff --git a/stubs/PHP7.4/repository.mysql.getter.stub b/stubs/PHP7.4/repository.mysql.getter.stub
deleted file mode 100644
index 01985b3..0000000
--- a/stubs/PHP7.4/repository.mysql.getter.stub
+++ /dev/null
@@ -1 +0,0 @@
- '{{ ColumnName }}' => ${{ EntityVariableName }}->get{{ GetterName }}(),
diff --git a/stubs/PHP7.4/repository.mysql.timeField.stub b/stubs/PHP7.4/repository.mysql.timeField.stub
deleted file mode 100644
index 1f24763..0000000
--- a/stubs/PHP7.4/repository.mysql.timeField.stub
+++ /dev/null
@@ -1 +0,0 @@
- '{{ ColumnName }}' => date('Y-m-d H:i:s'),
diff --git a/stubs/PHP7.4/repository.mysql.update.stub b/stubs/PHP7.4/repository.mysql.update.stub
deleted file mode 100644
index 048c88d..0000000
--- a/stubs/PHP7.4/repository.mysql.update.stub
+++ /dev/null
@@ -1,12 +0,0 @@
-
- /**
- * @param {{ EntityName }} ${{ EntityVariableName }}
- * @return int
- */
- public function update({{ EntityName }} ${{ EntityVariableName }}): int
- {
- return $this->newQuery()
- ->where($this->primaryKey, ${{ EntityVariableName }}->getPrimaryKey())
- ->update([
- ]);
- }
diff --git a/stubs/PHP7.4/repository.resource.class.stub b/stubs/PHP7.4/repository.resource.class.stub
deleted file mode 100644
index 02ebe56..0000000
--- a/stubs/PHP7.4/repository.resource.class.stub
+++ /dev/null
@@ -1,31 +0,0 @@
-toArray(${{ EntityVariableName }}) + [
-{{ ForeignGetterFunctions }}
- ];
- }
-}
\ No newline at end of file
diff --git a/stubs/PHP7.4/repository.resource.getter.default.stub b/stubs/PHP7.4/repository.resource.getter.default.stub
deleted file mode 100644
index 7c12036..0000000
--- a/stubs/PHP7.4/repository.resource.getter.default.stub
+++ /dev/null
@@ -1 +0,0 @@
- '{{ ColumnName }}' => ${{ EntityVariableName }}->get{{ GetterName }}(),
diff --git a/stubs/PHP7.4/repository.resource.getter.foreign.stub b/stubs/PHP7.4/repository.resource.getter.foreign.stub
deleted file mode 100644
index 42c3769..0000000
--- a/stubs/PHP7.4/repository.resource.getter.foreign.stub
+++ /dev/null
@@ -1 +0,0 @@
- '{{ AttributeName }}' => ${{ EntityVariableName }}->get{{ GetterName }}() ? (new {{ AttributeType }}Resource())->toArray(${{ EntityVariableName }}->get{{ GetterName }}()) : null,
diff --git a/stubs/PHP8.0/repository.base.attribute.sql.stub b/stubs/PHP8.0/repository.base.attribute.sql.stub
deleted file mode 100644
index 99fe387..0000000
--- a/stubs/PHP8.0/repository.base.attribute.sql.stub
+++ /dev/null
@@ -1 +0,0 @@
- private {{ SqlRepositoryName }} ${{ SqlRepositoryVariable }};
diff --git a/stubs/PHP8.0/repository.base.class.stub b/stubs/PHP8.0/repository.base.class.stub
deleted file mode 100644
index 31fd83a..0000000
--- a/stubs/PHP8.0/repository.base.class.stub
+++ /dev/null
@@ -1,16 +0,0 @@
-{{ SqlRepositoryVariable }}->{{ FunctionName }}(${{ AttributeName }});
- }
diff --git a/stubs/PHP8.0/repository.base.setter.sql.stub b/stubs/PHP8.0/repository.base.setter.sql.stub
deleted file mode 100644
index 97331ca..0000000
--- a/stubs/PHP8.0/repository.base.setter.sql.stub
+++ /dev/null
@@ -1 +0,0 @@
- $this->{{ SqlRepositoryVariable }} = new {{ SqlRepositoryName }}();
\ No newline at end of file
diff --git a/stubs/PHP8.0/repository.entity.accessors.stub b/stubs/PHP8.0/repository.entity.accessors.stub
deleted file mode 100644
index 91814b6..0000000
--- a/stubs/PHP8.0/repository.entity.accessors.stub
+++ /dev/null
@@ -1,16 +0,0 @@
-
- /**
- * @return {{ AttributeType }}
- */
- public function get{{ GetterName }}(): {{ AttributeType }}
- {
- return $this->{{ AttributeName }};
- }
-
- /**
- * @param {{ AttributeType }} ${{ AttributeName }}
- */
- public function set{{ SetterName }}({{ AttributeType }} ${{ AttributeName }}): void
- {
- $this->{{ AttributeName }} = ${{ AttributeName }};
- }
diff --git a/stubs/PHP8.0/repository.entity.attribute.stub b/stubs/PHP8.0/repository.entity.attribute.stub
deleted file mode 100644
index 6df2dec..0000000
--- a/stubs/PHP8.0/repository.entity.attribute.stub
+++ /dev/null
@@ -1 +0,0 @@
- protected {{ AttributeType }} ${{ AttributeName }};
diff --git a/stubs/PHP8.0/repository.entity.class.stub b/stubs/PHP8.0/repository.entity.class.stub
deleted file mode 100644
index f01693a..0000000
--- a/stubs/PHP8.0/repository.entity.class.stub
+++ /dev/null
@@ -1,8 +0,0 @@
-set{{ SetterName }}($entity->{{ AttributeName }});
diff --git a/stubs/PHP8.0/repository.interface.class.stub b/stubs/PHP8.0/repository.interface.class.stub
deleted file mode 100644
index cb9a94d..0000000
--- a/stubs/PHP8.0/repository.interface.class.stub
+++ /dev/null
@@ -1,9 +0,0 @@
-table = '{{ TableName }}';
- $this->primaryKey = 'id';
- $this->softDelete = {{ HasSoftDelete }};
- $this->factory = new {{ FactoryName }}();
-
- parent::__construct();
- }
-{{ Functions }}
-}
\ No newline at end of file
diff --git a/stubs/PHP8.0/repository.mysql.create.stub b/stubs/PHP8.0/repository.mysql.create.stub
deleted file mode 100644
index e9ffbc4..0000000
--- a/stubs/PHP8.0/repository.mysql.create.stub
+++ /dev/null
@@ -1,18 +0,0 @@
-
- /**
- * @param {{ EntityName }} ${{ EntityVariableName }}
- * @return {{ EntityName }}
- */
- public function create({{ EntityName }} ${{ EntityVariableName }}): {{ EntityName }}
- {
-{{ SetterFunctions }}
-
- $id = $this->newQuery()
- ->insertGetId([
-{{ GetterFunctions }}
- ]);
-
- ${{ EntityVariableName }}->setId($id);
-
- return ${{ EntityVariableName }};
- }
diff --git a/stubs/PHP8.0/repository.mysql.deleteAndUndelete.stub b/stubs/PHP8.0/repository.mysql.deleteAndUndelete.stub
deleted file mode 100644
index bdfdff8..0000000
--- a/stubs/PHP8.0/repository.mysql.deleteAndUndelete.stub
+++ /dev/null
@@ -1,26 +0,0 @@
-
- /**
- * @param {{ EntityName }} ${{ EntityVariableName }}
- * @return int
- */
- public function remove({{ EntityName }} ${{ EntityVariableName }}): int
- {
- return $this->newQuery()
- ->where($this->primaryKey, ${{ EntityVariableName }}->getPrimaryKey())
- ->update([
- 'deleted_at' => date('Y-m-d H:i:s'),
- ]);
- }
-
- /**
- * @param {{ EntityName }} ${{ EntityVariableName }}
- * @return int
- */
- public function restore({{ EntityName }} ${{ EntityVariableName }}): int
- {
- return $this->newQuery()
- ->where($this->primaryKey, ${{ EntityVariableName }}->getPrimaryKey())
- ->update([
- 'deleted_at' => null,
- ]);
- }
diff --git a/stubs/PHP8.0/repository.mysql.getAllBy.stub b/stubs/PHP8.0/repository.mysql.getAllBy.stub
deleted file mode 100644
index c63e3da..0000000
--- a/stubs/PHP8.0/repository.mysql.getAllBy.stub
+++ /dev/null
@@ -1,13 +0,0 @@
-
- /**
- * @param array ${{ AttributeNamePlural }}
- * @return Collection
- */
- public function getAllBy{{ FunctionNamePlural }}(array ${{ AttributeNamePlural }}): Collection
- {
- ${{ EntityVariableName }} = $this->newQuery()
- ->whereIn('{{ ColumnName }}', ${{ AttributeNamePlural }})
- ->get();
-
- return $this->factory->makeCollectionOfEntities(${{ EntityVariableName }});
- }
diff --git a/stubs/PHP8.0/repository.mysql.getOneBy.stub b/stubs/PHP8.0/repository.mysql.getOneBy.stub
deleted file mode 100644
index 1091972..0000000
--- a/stubs/PHP8.0/repository.mysql.getOneBy.stub
+++ /dev/null
@@ -1,13 +0,0 @@
-
- /**
- * @param int ${{ AttributeName }}
- * @return {{ EntityName }}|null
- */
- public function getOneBy{{ FunctionName }}(int ${{ AttributeName }}): ?{{ EntityName }}
- {
- ${{ EntityVariableName }} = $this->newQuery()
- ->where('{{ ColumnName }}', ${{ AttributeName }})
- ->first();
-
- return ${{ EntityVariableName }} ? $this->factory->makeEntityFromStdClass(${{ EntityVariableName }}) : null;
- }
diff --git a/stubs/PHP8.0/repository.mysql.getter.stub b/stubs/PHP8.0/repository.mysql.getter.stub
deleted file mode 100644
index 3886dce..0000000
--- a/stubs/PHP8.0/repository.mysql.getter.stub
+++ /dev/null
@@ -1 +0,0 @@
- '{{ ColumnName }}' => ${{ EntityVariableName }}->get{{ GetterName }}(),
diff --git a/stubs/PHP8.0/repository.mysql.setter.stub b/stubs/PHP8.0/repository.mysql.setter.stub
deleted file mode 100644
index 5bdf5ba..0000000
--- a/stubs/PHP8.0/repository.mysql.setter.stub
+++ /dev/null
@@ -1 +0,0 @@
- ${{ EntityVariableName }}->set{{ SetterName }}(date('Y-m-d H:i:s'));
diff --git a/stubs/PHP8.0/repository.mysql.timeField.stub b/stubs/PHP8.0/repository.mysql.timeField.stub
deleted file mode 100644
index 1f24763..0000000
--- a/stubs/PHP8.0/repository.mysql.timeField.stub
+++ /dev/null
@@ -1 +0,0 @@
- '{{ ColumnName }}' => date('Y-m-d H:i:s'),
diff --git a/stubs/PHP8.0/repository.mysql.update.stub b/stubs/PHP8.0/repository.mysql.update.stub
deleted file mode 100644
index 1a694fe..0000000
--- a/stubs/PHP8.0/repository.mysql.update.stub
+++ /dev/null
@@ -1,15 +0,0 @@
-
- /**
- * @param {{ EntityName }} ${{ EntityVariableName }}
- * @return int
- */
- public function update({{ EntityName }} ${{ EntityVariableName }}): int
- {
-{{ UpdateFieldSetter }}
-
- return $this->newQuery()
- ->where($this->primaryKey, ${{ EntityVariableName }}->getPrimaryKey())
- ->update([
-{{ GetterFunctions }}
- ]);
- }
diff --git a/stubs/PHP8.0/repository.resource.getter.default.stub b/stubs/PHP8.0/repository.resource.getter.default.stub
deleted file mode 100644
index 7c12036..0000000
--- a/stubs/PHP8.0/repository.resource.getter.default.stub
+++ /dev/null
@@ -1 +0,0 @@
- '{{ ColumnName }}' => ${{ EntityVariableName }}->get{{ GetterName }}(),
diff --git a/stubs/PHP8.0/repository.resource.getter.foreign.stub b/stubs/PHP8.0/repository.resource.getter.foreign.stub
deleted file mode 100644
index 42c3769..0000000
--- a/stubs/PHP8.0/repository.resource.getter.foreign.stub
+++ /dev/null
@@ -1 +0,0 @@
- '{{ AttributeName }}' => ${{ EntityVariableName }}->get{{ GetterName }}() ? (new {{ AttributeType }}Resource())->toArray(${{ EntityVariableName }}->get{{ GetterName }}()) : null,
diff --git a/stubs/Repositories/Base/base.attribute.sql.stub b/stubs/Repositories/Base/base.attribute.sql.stub
new file mode 100644
index 0000000..2ef4512
--- /dev/null
+++ b/stubs/Repositories/Base/base.attribute.sql.stub
@@ -0,0 +1,2 @@
+private {{ SqlRepositoryName }} ${{ SqlRepositoryVariable }};
+private {{ RedisRepositoryVariable }} ${{ RedisRepositoryName }};
diff --git a/stubs/Repositories/Base/base.construct.stub b/stubs/Repositories/Base/base.construct.stub
new file mode 100644
index 0000000..414e264
--- /dev/null
+++ b/stubs/Repositories/Base/base.construct.stub
@@ -0,0 +1,4 @@
+ public function __construct()
+ {
+ {{ Setters }}
+ }
diff --git a/stubs/Repositories/Base/base.construct_redis.stub b/stubs/Repositories/Base/base.construct_redis.stub
new file mode 100644
index 0000000..23d3b45
--- /dev/null
+++ b/stubs/Repositories/Base/base.construct_redis.stub
@@ -0,0 +1,7 @@
+use {{Strategy}};
+
+ public function __construct()
+ {
+ $this->cacheTag = {{ CacheTag }};
+ parent::__construct();
+ }
diff --git a/stubs/Repositories/Base/base.function.stub b/stubs/Repositories/Base/base.function.stub
new file mode 100644
index 0000000..f0002e5
--- /dev/null
+++ b/stubs/Repositories/Base/base.function.stub
@@ -0,0 +1,5 @@
+ public function {{ FunctionName }}({{ AttributeType }} ${{ AttributeName }}): {{ FunctionReturnType }}
+ {
+ {{redisFunction}}
+ {{returnResult}}
+ }
diff --git a/stubs/Repositories/Base/base.setter.sql.stub b/stubs/Repositories/Base/base.setter.sql.stub
new file mode 100644
index 0000000..94901a9
--- /dev/null
+++ b/stubs/Repositories/Base/base.setter.sql.stub
@@ -0,0 +1,2 @@
+$this->{{ SqlRepositoryVariable }} = new {{ SqlRepositoryName }}();
+ $this->{{ RedisRepositoryVariable }} = new {{ RedisRepositoryName }}();
diff --git a/stubs/PHP7.4/repository.interface.class.stub b/stubs/Repositories/Interface/interface.class.stub
similarity index 98%
rename from stubs/PHP7.4/repository.interface.class.stub
rename to stubs/Repositories/Interface/interface.class.stub
index cb9a94d..9ca3e56 100644
--- a/stubs/PHP7.4/repository.interface.class.stub
+++ b/stubs/Repositories/Interface/interface.class.stub
@@ -6,4 +6,5 @@ use {{ EntityNamespace }}\{{ EntityName }};
use Illuminate\Support\Collection;
interface {{ InterfaceRepositoryName }}
-{}
\ No newline at end of file
+{
+}
\ No newline at end of file
diff --git a/stubs/PHP7.4/repository.interface.create.stub b/stubs/Repositories/Interface/interface.create.stub
similarity index 100%
rename from stubs/PHP7.4/repository.interface.create.stub
rename to stubs/Repositories/Interface/interface.create.stub
diff --git a/stubs/Repositories/Interface/interface.deleteAndUndelete.stub b/stubs/Repositories/Interface/interface.deleteAndUndelete.stub
new file mode 100644
index 0000000..314223d
--- /dev/null
+++ b/stubs/Repositories/Interface/interface.deleteAndUndelete.stub
@@ -0,0 +1,4 @@
+
+ public function remove({{ EntityName }} ${{ EntityVariableName }}): int;
+
+ public function restore({{ EntityName }} ${{ EntityVariableName }}): int;
diff --git a/stubs/PHP7.4/repository.interface.getAllBy.stub b/stubs/Repositories/Interface/interface.getAllBy.stub
similarity index 100%
rename from stubs/PHP7.4/repository.interface.getAllBy.stub
rename to stubs/Repositories/Interface/interface.getAllBy.stub
diff --git a/stubs/Repositories/Interface/interface.getOneBy.stub b/stubs/Repositories/Interface/interface.getOneBy.stub
new file mode 100644
index 0000000..a3ff008
--- /dev/null
+++ b/stubs/Repositories/Interface/interface.getOneBy.stub
@@ -0,0 +1,2 @@
+
+ public function getOneBy{{ FunctionName }}({{ AttributeType }} ${{ AttributeName }}): null|{{ EntityName }};
diff --git a/stubs/PHP7.4/repository.interface.update.stub b/stubs/Repositories/Interface/interface.update.stub
similarity index 100%
rename from stubs/PHP7.4/repository.interface.update.stub
rename to stubs/Repositories/Interface/interface.update.stub
diff --git a/stubs/PHP7.4/repository.mysql.class.stub b/stubs/Repositories/Mysql/mysql.class.stub
similarity index 84%
rename from stubs/PHP7.4/repository.mysql.class.stub
rename to stubs/Repositories/Mysql/mysql.class.stub
index 3a48556..60f3415 100644
--- a/stubs/PHP7.4/repository.mysql.class.stub
+++ b/stubs/Repositories/Mysql/mysql.class.stub
@@ -4,8 +4,8 @@ namespace {{ RepositoryNamespace }}\{{ EntityName }};
use {{ EntityNamespace }}\{{ EntityName }};
use {{ FactoryNamespace }}\{{ FactoryName }};
-use {{ RepositoryNamespace }}\MySqlRepository;
use Illuminate\Support\Collection;
+use Eghamat24\DatabaseRepository\Models\Repositories\MySqlRepository;
class {{ MySqlRepositoryName }} extends MySqlRepository implements {{ RepositoryInterfaceName }}
{
@@ -18,4 +18,6 @@ class {{ MySqlRepositoryName }} extends MySqlRepository implements {{ Repository
parent::__construct();
}
-}
\ No newline at end of file
+
+ {{ Functions }}
+}
diff --git a/stubs/Repositories/Mysql/mysql.construct.stub b/stubs/Repositories/Mysql/mysql.construct.stub
new file mode 100644
index 0000000..406fe80
--- /dev/null
+++ b/stubs/Repositories/Mysql/mysql.construct.stub
@@ -0,0 +1,9 @@
+public function __construct()
+ {
+ $this->table = '{{ TableName }}';
+ $this->primaryKey = 'id';
+ $this->softDelete = {{ HasSoftDelete }};
+ $this->factory = new {{ FactoryName }}();
+
+ parent::__construct();
+ }
diff --git a/stubs/Repositories/Mysql/mysql.create.stub b/stubs/Repositories/Mysql/mysql.create.stub
new file mode 100644
index 0000000..f7113c9
--- /dev/null
+++ b/stubs/Repositories/Mysql/mysql.create.stub
@@ -0,0 +1,13 @@
+ public function create({{ EntityName }} ${{ EntityVariableName }}): {{ EntityName }}
+ {
+ {{ SetterFunctions }}
+
+ $id = $this->newQuery()
+ ->insertGetId([
+ {{ GetterFunctions }}
+ ]);
+
+ ${{ EntityVariableName }}->id = $id;
+
+ return ${{ EntityVariableName }};
+ }
diff --git a/stubs/Repositories/Mysql/mysql.delete.stub b/stubs/Repositories/Mysql/mysql.delete.stub
new file mode 100644
index 0000000..5a99837
--- /dev/null
+++ b/stubs/Repositories/Mysql/mysql.delete.stub
@@ -0,0 +1,8 @@
+ public function remove({{ EntityName }} ${{ EntityVariableName }}): int
+ {
+ return $this->newQuery()
+ ->where($this->primaryKey, ${{ EntityVariableName }}->getPrimaryKey())
+ ->update([
+ 'deleted_at' => date('Y-m-d H:i:s'),
+ ]);
+ }
diff --git a/stubs/Repositories/Mysql/mysql.deleteAndUndelete.stub b/stubs/Repositories/Mysql/mysql.deleteAndUndelete.stub
new file mode 100644
index 0000000..725ee00
--- /dev/null
+++ b/stubs/Repositories/Mysql/mysql.deleteAndUndelete.stub
@@ -0,0 +1,17 @@
+ public function remove({{ EntityName }} ${{ EntityVariableName }}): int
+ {
+ return $this->newQuery()
+ ->where($this->primaryKey, ${{ EntityVariableName }}->getPrimaryKey())
+ ->update([
+ 'deleted_at' => date('Y-m-d H:i:s'),
+ ]);
+ }
+
+ public function restore({{ EntityName }} ${{ EntityVariableName }}): int
+ {
+ return $this->newQuery()
+ ->where($this->primaryKey, ${{ EntityVariableName }}->getPrimaryKey())
+ ->update([
+ 'deleted_at' => null,
+ ]);
+ }
diff --git a/stubs/Repositories/Mysql/mysql.getAllBy.stub b/stubs/Repositories/Mysql/mysql.getAllBy.stub
new file mode 100644
index 0000000..0c6d435
--- /dev/null
+++ b/stubs/Repositories/Mysql/mysql.getAllBy.stub
@@ -0,0 +1,8 @@
+ public function getAllBy{{ FunctionNamePlural }}(array ${{ AttributeNamePlural }}): Collection
+ {
+ ${{ EntityVariableName }} = $this->newQuery()
+ ->whereIn('{{ ColumnName }}', ${{ AttributeNamePlural }})
+ ->get();
+
+ return $this->factory->makeCollectionOfEntities(${{ EntityVariableName }});
+ }
diff --git a/stubs/Repositories/Mysql/mysql.getOneBy.stub b/stubs/Repositories/Mysql/mysql.getOneBy.stub
new file mode 100644
index 0000000..f8e44e2
--- /dev/null
+++ b/stubs/Repositories/Mysql/mysql.getOneBy.stub
@@ -0,0 +1,8 @@
+ public function getOneBy{{ FunctionName }}({{ AttributeType }} ${{ AttributeName }}): null|{{ EntityName }}
+ {
+ ${{ EntityVariableName }} = $this->newQuery()
+ ->where('{{ ColumnName }}', ${{ AttributeName }})
+ ->first();
+
+ return ${{ EntityVariableName }} ? $this->factory->makeEntityFromStdClass(${{ EntityVariableName }}) : null;
+ }
diff --git a/stubs/Repositories/Mysql/mysql.getter.stub b/stubs/Repositories/Mysql/mysql.getter.stub
new file mode 100644
index 0000000..8862049
--- /dev/null
+++ b/stubs/Repositories/Mysql/mysql.getter.stub
@@ -0,0 +1 @@
+ '{{ ColumnName }}' => ${{ EntityVariableName }}->{{ AttributeName }},
diff --git a/stubs/Repositories/Mysql/mysql.setter.stub b/stubs/Repositories/Mysql/mysql.setter.stub
new file mode 100644
index 0000000..419d6c1
--- /dev/null
+++ b/stubs/Repositories/Mysql/mysql.setter.stub
@@ -0,0 +1 @@
+ ${{ EntityVariableName }}->{{ AttributeName }} = date('Y-m-d H:i:s');
diff --git a/stubs/Repositories/Mysql/mysql.timeField.stub b/stubs/Repositories/Mysql/mysql.timeField.stub
new file mode 100644
index 0000000..f5b27ac
--- /dev/null
+++ b/stubs/Repositories/Mysql/mysql.timeField.stub
@@ -0,0 +1 @@
+ '{{ ColumnName }}' => date('Y-m-d H:i:s'),
diff --git a/stubs/Repositories/Mysql/mysql.undelete.stub b/stubs/Repositories/Mysql/mysql.undelete.stub
new file mode 100644
index 0000000..54238af
--- /dev/null
+++ b/stubs/Repositories/Mysql/mysql.undelete.stub
@@ -0,0 +1,8 @@
+ public function restore({{ EntityName }} ${{ EntityVariableName }}): int
+ {
+ return $this->newQuery()
+ ->where($this->primaryKey, ${{ EntityVariableName }}->getPrimaryKey())
+ ->update([
+ 'deleted_at' => null,
+ ]);
+ }
diff --git a/stubs/Repositories/Mysql/mysql.update.stub b/stubs/Repositories/Mysql/mysql.update.stub
new file mode 100644
index 0000000..7671962
--- /dev/null
+++ b/stubs/Repositories/Mysql/mysql.update.stub
@@ -0,0 +1,10 @@
+ public function update({{ EntityName }} ${{ EntityVariableName }}): int
+ {
+ {{ UpdateFieldSetter }}
+
+ return $this->newQuery()
+ ->where($this->primaryKey, ${{ EntityVariableName }}->getPrimaryKey())
+ ->update([
+ {{ GetterFunctions }}
+ ]);
+ }
diff --git a/stubs/Repositories/Redis/base.clear.stub b/stubs/Repositories/Redis/base.clear.stub
new file mode 100644
index 0000000..e67fa83
--- /dev/null
+++ b/stubs/Repositories/Redis/base.clear.stub
@@ -0,0 +1 @@
+$this->redisRepository->clear();
diff --git a/stubs/Repositories/Redis/create/base.clearable_temporary_cache_strategy.stub b/stubs/Repositories/Redis/create/base.clearable_temporary_cache_strategy.stub
new file mode 100644
index 0000000..6eac59f
--- /dev/null
+++ b/stubs/Repositories/Redis/create/base.clearable_temporary_cache_strategy.stub
@@ -0,0 +1,6 @@
+ $cacheKey = $this->redisRepository->makeKey([
+ 'function_name' => 'create',
+ 'id' => $id,
+ ]);
+
+ $this->redisRepository->clear($cacheKey);
diff --git a/stubs/Repositories/Redis/create/base.query_cache_strategy.stub b/stubs/Repositories/Redis/create/base.query_cache_strategy.stub
new file mode 100644
index 0000000..e67fa83
--- /dev/null
+++ b/stubs/Repositories/Redis/create/base.query_cache_strategy.stub
@@ -0,0 +1 @@
+$this->redisRepository->clear();
diff --git a/stubs/Repositories/Redis/create/base.single_key_cache_strategy.stub b/stubs/Repositories/Redis/create/base.single_key_cache_strategy.stub
new file mode 100644
index 0000000..e67fa83
--- /dev/null
+++ b/stubs/Repositories/Redis/create/base.single_key_cache_strategy.stub
@@ -0,0 +1 @@
+$this->redisRepository->clear();
diff --git a/stubs/Repositories/Redis/create/base.temporary_cache_strategy.stub b/stubs/Repositories/Redis/create/base.temporary_cache_strategy.stub
new file mode 100644
index 0000000..e69de29
diff --git a/stubs/Repositories/Redis/getAllBy/base.clearable_temporary_cache_strategy.stub b/stubs/Repositories/Redis/getAllBy/base.clearable_temporary_cache_strategy.stub
new file mode 100644
index 0000000..05b3d7a
--- /dev/null
+++ b/stubs/Repositories/Redis/getAllBy/base.clearable_temporary_cache_strategy.stub
@@ -0,0 +1,11 @@
+$cacheKey = $this->redisRepository->makeKey([
+ 'function_name' => '{{ FunctionName }}',
+ '{{ ColumnNameSingle }}' => ${{ ColumnName }},
+ ]);
+
+ $data = $this->redisRepository->get($cacheKey);
+
+ if (is_null($data)) {
+ $data = $this->repository->{{ FunctionName }}(${{ ColumnName }});
+ $this->redisRepository->put($cacheKey, $data, Time::HALF_HOUR_BY_SECOND);
+ }
diff --git a/stubs/Repositories/Redis/getAllBy/base.query_cache_strategy.stub b/stubs/Repositories/Redis/getAllBy/base.query_cache_strategy.stub
new file mode 100644
index 0000000..c6cd353
--- /dev/null
+++ b/stubs/Repositories/Redis/getAllBy/base.query_cache_strategy.stub
@@ -0,0 +1,11 @@
+$cacheKey = $this->redisRepository->makeKey([
+ 'function_name' => '{{ FunctionName }}',
+ '{{ ColumnNameSingle }}' => ${{ ColumnName }},
+ ]);
+
+ $entities = $this->redisRepository->get($cacheKey);
+
+ if ($entities === null) {
+ $entities = $this->repository->{{ FunctionName }}(${{ ColumnName }});
+ $this->redisRepository->put($cacheKey, $entities);
+ }
diff --git a/stubs/Repositories/Redis/getAllBy/base.single_key_cache_strategy.stub b/stubs/Repositories/Redis/getAllBy/base.single_key_cache_strategy.stub
new file mode 100644
index 0000000..e653fb5
--- /dev/null
+++ b/stubs/Repositories/Redis/getAllBy/base.single_key_cache_strategy.stub
@@ -0,0 +1,6 @@
+$entity = $this->redisRepository->get();
+
+ if ($entity === null) {
+ $entities = $this->repository->{{ FunctionName }}(${{ ColumnName }});
+ $this->redisRepository->put($entities);
+ }
diff --git a/stubs/Repositories/Redis/getAllBy/base.temporary_cache_strategy.stub b/stubs/Repositories/Redis/getAllBy/base.temporary_cache_strategy.stub
new file mode 100644
index 0000000..0a45aff
--- /dev/null
+++ b/stubs/Repositories/Redis/getAllBy/base.temporary_cache_strategy.stub
@@ -0,0 +1,11 @@
+$cacheKey = $this->redisRepository->makeKey([
+ 'function_name' => '{{ FunctionName }}',
+ '{{ ColumnNameSingle }}' => ${{ ColumnName }},
+ ]);
+
+ $data = $this->redisRepository->get($cacheKey);
+
+ if (is_null($data)) {
+ $data = $this->repository->{{ FunctionName }}(${{ ColumnName }});
+ $this->redisRepository->put($cacheKey, $data, Time::HALF_HOUR_BY_SECOND);
+ }
diff --git a/stubs/Repositories/Redis/getOneBy/base.clearable_temporary_cache_strategy.stub b/stubs/Repositories/Redis/getOneBy/base.clearable_temporary_cache_strategy.stub
new file mode 100644
index 0000000..6f55216
--- /dev/null
+++ b/stubs/Repositories/Redis/getOneBy/base.clearable_temporary_cache_strategy.stub
@@ -0,0 +1,11 @@
+ $cacheKey = $this->redisRepository->makeKey([
+ 'function_name' => '{{ FunctionName }}',
+ '{{ ColumnName }}' => ${{ ColumnName }},
+ ]);
+
+ $data = $this->redisRepository->get($cacheKey);
+
+ if (is_null($data)) {
+ $data = $this->repository->{{ FunctionName }}(${{ ColumnName }});
+ $this->redisRepository->put($cacheKey, $data, Time::HALF_HOUR_BY_SECOND);
+ }
diff --git a/stubs/Repositories/Redis/getOneBy/base.query_cache_strategy.stub b/stubs/Repositories/Redis/getOneBy/base.query_cache_strategy.stub
new file mode 100644
index 0000000..0641280
--- /dev/null
+++ b/stubs/Repositories/Redis/getOneBy/base.query_cache_strategy.stub
@@ -0,0 +1,11 @@
+$cacheKey = $this->redisRepository->makeKey([
+ 'function_name' => '{{ FunctionName }}',
+ '{{ ColumnName }}' => ${{ ColumnName }},
+ ]);
+
+ $entity = $this->redisRepository->get($cacheKey);
+
+ if ($entity === null) {
+ $entity = $this->repository->{{ FunctionName }}(${{ ColumnName }});
+ $this->redisRepository->put($cacheKey, $entity);
+ }
diff --git a/stubs/Repositories/Redis/getOneBy/base.single_key_cache_strategy.stub b/stubs/Repositories/Redis/getOneBy/base.single_key_cache_strategy.stub
new file mode 100644
index 0000000..a9e94d5
--- /dev/null
+++ b/stubs/Repositories/Redis/getOneBy/base.single_key_cache_strategy.stub
@@ -0,0 +1,6 @@
+ $entity = $this->redisRepository->get();
+
+ if ($entity === null) {
+ $entity = $this->repository->{{ FunctionName }}(${{ ColumnName }});
+ $this->redisRepository->put($entity);
+ }
diff --git a/stubs/Repositories/Redis/getOneBy/base.temporary_cache_strategy.stub b/stubs/Repositories/Redis/getOneBy/base.temporary_cache_strategy.stub
new file mode 100644
index 0000000..2565c67
--- /dev/null
+++ b/stubs/Repositories/Redis/getOneBy/base.temporary_cache_strategy.stub
@@ -0,0 +1,11 @@
+$cacheKey = $this->redisRepository->makeKey([
+ 'function_name' => '{{ FunctionName }}',
+ '{{ ColumnName }}' => ${{ ColumnName }},
+ ]);
+
+ $data = $this->redisRepository->get($cacheKey);
+
+ if (is_null($data)) {
+ $data = $this->repository->{{ FunctionName }}(${{ ColumnName }});
+ $this->redisRepository->put($cacheKey, $data, Time::HALF_HOUR_BY_SECOND);
+ }
diff --git a/stubs/Repositories/Redis/update/base.clearable_temporary_cache_strategy.stub b/stubs/Repositories/Redis/update/base.clearable_temporary_cache_strategy.stub
new file mode 100644
index 0000000..ead52a7
--- /dev/null
+++ b/stubs/Repositories/Redis/update/base.clearable_temporary_cache_strategy.stub
@@ -0,0 +1,6 @@
+$cacheKey = $this->redisRepository->makeKey([
+ 'function_name' => 'update',
+ 'id' => $id,
+]);
+
+$this->redisRepository->clear($cacheKey);
diff --git a/stubs/Repositories/Redis/update/base.query_cache_strategy.stub b/stubs/Repositories/Redis/update/base.query_cache_strategy.stub
new file mode 100644
index 0000000..e67fa83
--- /dev/null
+++ b/stubs/Repositories/Redis/update/base.query_cache_strategy.stub
@@ -0,0 +1 @@
+$this->redisRepository->clear();
diff --git a/stubs/Repositories/Redis/update/base.single_key_cache_strategy.stub b/stubs/Repositories/Redis/update/base.single_key_cache_strategy.stub
new file mode 100644
index 0000000..e67fa83
--- /dev/null
+++ b/stubs/Repositories/Redis/update/base.single_key_cache_strategy.stub
@@ -0,0 +1 @@
+$this->redisRepository->clear();
diff --git a/stubs/Repositories/Redis/update/base.temporary_cache_strategy.stub b/stubs/Repositories/Redis/update/base.temporary_cache_strategy.stub
new file mode 100644
index 0000000..e69de29
diff --git a/stubs/PHP8.0/repository.resource.class.stub b/stubs/Resources/resource.class.stub
similarity index 57%
rename from stubs/PHP8.0/repository.resource.class.stub
rename to stubs/Resources/resource.class.stub
index 02ebe56..2526ab3 100644
--- a/stubs/PHP8.0/repository.resource.class.stub
+++ b/stubs/Resources/resource.class.stub
@@ -2,30 +2,23 @@
namespace {{ ResourceNamespace }};
-use {{ EntityNamespace }}\Entity;
use {{ EntityNamespace }}\{{ EntityName }};
+use Eghamat24\DatabaseRepository\Models\Entity\Entity;
+use Eghamat24\DatabaseRepository\Models\Resources\Resource;
class {{ ResourceName }} extends Resource
{
- /**
- * @param Entity|{{ EntityName }} ${{ EntityVariableName }}
- * @return array
- */
public function toArray(${{ EntityVariableName }}): array
{
return [
-{{ GetterFunctions }}
+ {{ GetterFunctions }}
];
}
- /**
- * @param Entity|{{ EntityName }} ${{ EntityVariableName }}
- * @return array
- */
public function toArrayWithForeignKeys(${{ EntityVariableName }}): array
{
return $this->toArray(${{ EntityVariableName }}) + [
-{{ ForeignGetterFunctions }}
+ {{ ForeignGetterFunctions }}
];
}
-}
\ No newline at end of file
+}
diff --git a/stubs/Resources/resource.function.foreign.stub b/stubs/Resources/resource.function.foreign.stub
new file mode 100644
index 0000000..4e0825d
--- /dev/null
+++ b/stubs/Resources/resource.function.foreign.stub
@@ -0,0 +1,6 @@
+ public function toArrayWithForeignKeys(${{ EntityVariableName }}): array
+ {
+ return $this->toArray(${{ EntityVariableName }}) + [
+ {{ ForeignGetterFunctions }}
+ ];
+ }
diff --git a/stubs/Resources/resource.function.getter.stub b/stubs/Resources/resource.function.getter.stub
new file mode 100644
index 0000000..ced9819
--- /dev/null
+++ b/stubs/Resources/resource.function.getter.stub
@@ -0,0 +1,6 @@
+ public function toArray(${{ EntityVariableName }}): array
+ {
+ return [
+ {{ Getters }}
+ ];
+ }
diff --git a/stubs/Resources/resource.getter.default.stub b/stubs/Resources/resource.getter.default.stub
new file mode 100644
index 0000000..ed6b4fa
--- /dev/null
+++ b/stubs/Resources/resource.getter.default.stub
@@ -0,0 +1 @@
+'{{ ColumnName }}' => ${{ EntityVariableName }}->{{ AttributeName }},
diff --git a/stubs/Resources/resource.getter.foreign.stub b/stubs/Resources/resource.getter.foreign.stub
new file mode 100644
index 0000000..abb8705
--- /dev/null
+++ b/stubs/Resources/resource.getter.foreign.stub
@@ -0,0 +1 @@
+ '{{ AttributeName }}' => ${{ EntityVariableName }}->get{{ GetterName }}() ? (new {{ AttributeType }}Resource())->toArray(${{ EntityVariableName }}->get{{ GetterName }}()) : null,
diff --git a/stubs/base.class.stub b/stubs/base.class.stub
new file mode 100644
index 0000000..618ccc1
--- /dev/null
+++ b/stubs/base.class.stub
@@ -0,0 +1,10 @@
+
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy