-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the ability to auto-determine the presenter #26
Closed
+150
−8
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,24 +3,69 @@ | |||||||||||||||||||||||||||||||||||
namespace Coderflex\LaravelPresenter\Concerns; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
use Coderflex\LaravelPresenter\Exceptions\PresenterException; | ||||||||||||||||||||||||||||||||||||
use Illuminate\Support\Str; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||
* Uses Presenters Trait | ||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
trait UsesPresenters | ||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||
|-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||
| Presenters | ||||||||||||||||||||||||||||||||||||
|-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||||||||||||
| Presenters can be defined as a single item in $this->presenter or | ||||||||||||||||||||||||||||||||||||
| as an array of $this->presenters. If neither are set, then we will | ||||||||||||||||||||||||||||||||||||
| generate a default based on the Model and type (if set). | ||||||||||||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||
* Check the given presenters value exists or not | ||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||
* @param string $type | ||||||||||||||||||||||||||||||||||||
* @return mixed | ||||||||||||||||||||||||||||||||||||
* @throws PresenterException | ||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
public function present(string $type = 'default') | ||||||||||||||||||||||||||||||||||||
public function present(string $type = 'default'): mixed | ||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||
if (array_key_exists($type, $this->presenters)) { | ||||||||||||||||||||||||||||||||||||
return new $this->presenters[$type]($this); | ||||||||||||||||||||||||||||||||||||
$presenter = $this->getDefaultPresenterName($type); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see a Code smell in here, consider using aggressive condition statement, to clean the code, and understandable.
Suggested change
|
||||||||||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||||||||||
is_array($this->presenters) && | ||||||||||||||||||||||||||||||||||||
array_key_exists($type, $this->presenters ?? []) | ||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||
$presenter = $this->presenters[$type]; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if (is_string($this->presenter ?? null)) { | ||||||||||||||||||||||||||||||||||||
$presenter = $this->presenter; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if (class_exists($presenter)) { | ||||||||||||||||||||||||||||||||||||
return new $presenter($this); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
throw new PresenterException(); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||
|-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||
| Default Presenter Names | ||||||||||||||||||||||||||||||||||||
|-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||||||||||||
| By default, the presenter name is defined based on the name of the | ||||||||||||||||||||||||||||||||||||
| Model class. The default namespace is \App\Presenters. So, the Model | ||||||||||||||||||||||||||||||||||||
| Day has Presenter DayPresenter. However, if you supply a type, e.g. then it | ||||||||||||||||||||||||||||||||||||
| would be DayTypePresenter. | ||||||||||||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
private function getDefaultPresenterName($type) | ||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||
$modelNameModifier = $type === 'default' ? '' : $type; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
return Str::of(get_class()) | ||||||||||||||||||||||||||||||||||||
->replace('Models', 'Presenters') | ||||||||||||||||||||||||||||||||||||
->append(str($modelNameModifier)->ucfirst()) | ||||||||||||||||||||||||||||||||||||
->append('Presenter') | ||||||||||||||||||||||||||||||||||||
->toString(); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Coderflex\LaravelPresenter\Tests\Models; | ||
|
||
use Coderflex\LaravelPresenter\Concerns\CanPresent; | ||
use Coderflex\LaravelPresenter\Concerns\UsesPresenters; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Item extends Model implements CanPresent | ||
{ | ||
use UsesPresenters; | ||
|
||
protected $guarded = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Coderflex\LaravelPresenter\Tests\Presenters; | ||
|
||
; | ||
|
||
use Coderflex\LaravelPresenter\Presenter; | ||
use Illuminate\Support\Str; | ||
|
||
class ItemPresenter extends Presenter | ||
{ | ||
public function slug() | ||
{ | ||
return Str::of($this->model->title)->slug()->toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Coderflex\LaravelPresenter\Tests\Presenters; | ||
|
||
; | ||
|
||
use Coderflex\LaravelPresenter\Presenter; | ||
use Illuminate\Support\Str; | ||
|
||
class ItemSubdomainPresenter extends Presenter | ||
{ | ||
public function caps() | ||
{ | ||
return Str::of($this->model->title)->upper()->toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('posts', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('title'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('users'); | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you want to create an alias for the command? Something like
php artisan make:presenter
, without removing the current?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just a spelling error change.