An api to calculate sum of two distances with different units and return the result in selected unit
I used TDD for the challenge. At first, I create YardDistance and MeterDistance classes. Each one has two getters, getMeter and getYard and of course, they implemented DistanceInterface.
There are two different units in the request so we need a creational design pattern. I chose the static factory method because it can create MeterDistance and YardDistance objects for me in the simplest form.
Also, there is two different behaviors or strategies for calculating the sum of them, so I used Strategy pattern and defined MeterCalculatorStrategy and YardCalculatorStrategy.
- Copy .env.example file to .env and edit database credentials there
- Run composer install
- Run php artisan key:generate
- Run ./vendor/bin/sail up
- Open http://localhost/doc
./vendor/bin/sail artisan test
app/Distance/DistanceFactory.php
app/Distance/DistanceInterface.php
app/Distance/MeterDistance.php
app/Distance/Strategies/CalculatorStrategyContexts.php
app/Distance/Strategies/CalculatorStrategyInterface.php
app/Distance/Strategies/MeterCalculatorStrategy.php
app/Distance/Strategies/YardCalculatorStrategy.php
app/Http/Controllers/DistanceCalculatorController.php
app/Http/Requests/CalculateDistancesRequest.php