Skip to content

Commit

Permalink
Update Repository.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Jan 26, 2025
1 parent 143a50f commit 7e76882
Showing 1 changed file with 1 addition and 37 deletions.
38 changes: 1 addition & 37 deletions src/Abstract/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,45 +44,9 @@ public function shouldEagerLoadIncludes(): bool
return false;
}

/**
* This function relies on strict conventions:
* - Repository name should be same as it's model name (model: Foo -> repository: FooRepository).
* - If the container contains Models with names different from the container name, the repository class must
* implement model() method and return the FQCN e.g., Role::class
*/
public function model(): string
{
$className = $this->getClassName(); // e.g. UserRepository
$modelName = $this->getModelName($className); // e.g. User

return $this->getModelNamespace($modelName);
}

public function getClassName(): string
{
$fullName = static::class;

return substr($fullName, strrpos($fullName, '\\') + 1);
}

public function getModelName(string $className): string|array
{
return str_replace('Repository', '', $className);
}

public function getModelNamespace(array|string $modelName): string
{
return 'App\\Containers\\' . $this->getCurrentSection() . '\\' . $this->getCurrentContainer() . '\\Models\\' . $modelName;
}

public function getCurrentSection(): string
{
return explode('\\', static::class)[2];
}

public function getCurrentContainer(): string
{
return explode('\\', static::class)[3];
return apiato()->repository()->resolveModelName(static::class);
}

/**
Expand Down

0 comments on commit 7e76882

Please sign in to comment.