Skip to content

Perhaps V3? #2

Description

@ShaneGlee

Improvements:

  • Defined Properties
  • Nesting is simple
  • Would simplify the transform code to a function
  • Would allow two way transform (ie. from request, handy for forms)
  • Possibility to add Mutatators / Attributes
  • Could handle Subclassing with parent::fromCar()
  • Allows a little reuse ie. for dropdown data

namespace App\ViewObject;

use FocalStrategy\ViewObjects\ViewObject;
use App\Models\Car;

class CarVo extends ViewObject
{
	protected $car_id;
	protected $make;
	protected $model;

	/* @nested(AssessmentVo) */
	protected $assessments;

    public function fromCar(Car $car)
    {
        $this->car_id = $car->id;
        $this->make = $car->marque->display_name ?? 'Not Set';
        $this->model = $car->modelBasic->display_name ?? 'Not Set';

        $this->withNested('assessments', $car->car_assessment_results);
    }

    public function fromArray($req_data)
    {
        $this->car_id = $req_data['id'];
        $this->make = $req_data['make'];
        $this->model = $req_data['model'];

        $this->withNested('assessments', $req_data['assessments']);
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions