Skip to content

Commit

Permalink
Use app bindings for model classes (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchimp authored Oct 5, 2021
1 parent a931b5c commit ab79322
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/Contracts/Answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MattDaneshvar\Survey\Contracts;

interface Answer
{
//
}
8 changes: 8 additions & 0 deletions src/Contracts/Entry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MattDaneshvar\Survey\Contracts;

interface Entry
{
//
}
8 changes: 8 additions & 0 deletions src/Contracts/Question.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MattDaneshvar\Survey\Contracts;

interface Question
{
//
}
8 changes: 8 additions & 0 deletions src/Contracts/Section.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MattDaneshvar\Survey\Contracts;

interface Section
{
//
}
8 changes: 8 additions & 0 deletions src/Contracts/Survey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MattDaneshvar\Survey\Contracts;

interface Survey
{
//
}
9 changes: 6 additions & 3 deletions src/Models/Answer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace MattDaneshvar\Survey\Models;

use Illuminate\Database\Eloquent\Model;
use MattDaneshvar\Survey\Contracts\Answer as AnswerContract;
use MattDaneshvar\Survey\Contracts\Entry;
use MattDaneshvar\Survey\Contracts\Question;

class Answer extends Model
class Answer extends Model implements AnswerContract
{
/**
* Answer constructor.
Expand Down Expand Up @@ -34,7 +37,7 @@ public function __construct(array $attributes = [])
*/
public function entry()
{
return $this->belongsTo(Entry::class);
return $this->belongsTo(get_class(app()->make(Entry::class)));
}

/**
Expand All @@ -44,6 +47,6 @@ public function entry()
*/
public function question()
{
return $this->belongsTo(Question::class);
return $this->belongsTo(get_class(app()->make(Question::class)));
}
}
13 changes: 9 additions & 4 deletions src/Models/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User;
use MattDaneshvar\Survey\Contracts\Answer;
use MattDaneshvar\Survey\Contracts\Entry as EntryContract;
use MattDaneshvar\Survey\Contracts\Survey;
use MattDaneshvar\Survey\Exceptions\GuestEntriesNotAllowedException;
use MattDaneshvar\Survey\Exceptions\MaxEntriesPerUserLimitExceeded;

class Entry extends Model
class Entry extends Model implements EntryContract
{
/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -53,7 +56,7 @@ public function __construct(array $attributes = [])
*/
public function answers()
{
return $this->hasMany(Answer::class);
return $this->hasMany(get_class(app()->make(Answer::class)));
}

/**
Expand All @@ -63,7 +66,7 @@ public function answers()
*/
public function survey()
{
return $this->belongsTo(Survey::class);
return $this->belongsTo(get_class(app()->make(Survey::class)));
}

/**
Expand Down Expand Up @@ -115,7 +118,9 @@ public function fromArray(array $values)
continue;
}

$this->answers->add(Answer::make([
$answer_class = get_class(app()->make(Answer::class));

$this->answers->add($answer_class::make([
'question_id' => substr($key, 1),
'entry_id' => $this->id,
'value' => $value,
Expand Down
12 changes: 8 additions & 4 deletions src/Models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
namespace MattDaneshvar\Survey\Models;

use Illuminate\Database\Eloquent\Model;
use MattDaneshvar\Survey\Contracts\Answer;
use MattDaneshvar\Survey\Contracts\Question as QuestionContract;
use MattDaneshvar\Survey\Contracts\Section;
use MattDaneshvar\Survey\Contracts\Survey;

class Question extends Model
class Question extends Model implements QuestionContract
{
/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -58,7 +62,7 @@ public function __construct(array $attributes = [])
*/
public function survey()
{
return $this->belongsTo(Survey::class);
return $this->belongsTo(get_class(app()->make(Survey::class)));
}

/**
Expand All @@ -68,7 +72,7 @@ public function survey()
*/
public function section()
{
return $this->belongsTo(Section::class);
return $this->belongsTo(get_class(app()->make(Section::class)));
}

/**
Expand All @@ -78,7 +82,7 @@ public function section()
*/
public function answers()
{
return $this->hasMany(Answer::class);
return $this->hasMany(get_class(app()->make(Answer::class)));
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Models/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace MattDaneshvar\Survey\Models;

use Illuminate\Database\Eloquent\Model;
use MattDaneshvar\Survey\Contracts\Question;
use MattDaneshvar\Survey\Contracts\Section as SectionContract;

class Section extends Model
class Section extends Model implements SectionContract
{
/**
* Section constructor.
Expand Down Expand Up @@ -34,6 +36,6 @@ public function __construct(array $attributes = [])
*/
public function questions()
{
return $this->hasMany(Question::class);
return $this->hasMany(get_class(app()->make(Question::class)));
}
}
12 changes: 8 additions & 4 deletions src/Models/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
namespace MattDaneshvar\Survey\Models;

use Illuminate\Database\Eloquent\Model;
use MattDaneshvar\Survey\Contracts\Entry;
use MattDaneshvar\Survey\Contracts\Question;
use MattDaneshvar\Survey\Contracts\Section;
use MattDaneshvar\Survey\Contracts\Survey as SurveyContract;

class Survey extends Model
class Survey extends Model implements SurveyContract
{
/**
* Survey constructor.
Expand Down Expand Up @@ -43,7 +47,7 @@ public function __construct(array $attributes = [])
*/
public function sections()
{
return $this->hasMany(Section::class);
return $this->hasMany(get_class(app()->make(Section::class)));
}

/**
Expand All @@ -53,7 +57,7 @@ public function sections()
*/
public function questions()
{
return $this->hasMany(Question::class);
return $this->hasMany(get_class(app()->make(Question::class)));
}

/**
Expand All @@ -63,7 +67,7 @@ public function questions()
*/
public function entries()
{
return $this->hasMany(Entry::class);
return $this->hasMany(get_class(app()->make(Entry::class)));
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/SurveyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ public function boot(ViewFactory $viewFactory)
]);
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind(\MattDaneshvar\Survey\Contracts\Answer::class, \MattDaneshvar\Survey\Models\Answer::class);
$this->app->bind(\MattDaneshvar\Survey\Contracts\Entry::class, \MattDaneshvar\Survey\Models\Entry::class);
$this->app->bind(\MattDaneshvar\Survey\Contracts\Question::class, \MattDaneshvar\Survey\Models\Question::class);
$this->app->bind(\MattDaneshvar\Survey\Contracts\Section::class, \MattDaneshvar\Survey\Models\Section::class);
$this->app->bind(\MattDaneshvar\Survey\Contracts\Survey::class, \MattDaneshvar\Survey\Models\Survey::class);
}

/**
* Publish package migrations.
*
Expand Down

0 comments on commit ab79322

Please sign in to comment.