naykel
1 week ago
144
6
General

SOLID design principles and data manipluation

Posted 1 week ago by naykel

I am trying to adopt the SOLID design principles while keeping with the 'Single Responsibility Principle' I have moved my form validation from my controller to it own 'Form Request'. While this seemed pretty straight it some cases it raised more questions, specifically when it comes to data manipulation.

For example, I have recently discovered it is considered good practice to store money values in cents. Therefore at some point there needs to be a calculation to manipulate the value from dollars to cents or vice versa.

Should this be done in the 'UpdateProductRequest' or the 'ProductController' or somewhere else again?

// ProductController
public function update(ValidateProduct $request, Product $product) {

    $validatedData = $request->all();

    $product->update($validatedData);
    
}


// UpdateProductRequest
public function rules()
{
    $validatedData = [
        'name' => 'required|min:3',
        'price' => 'sometimes|numeric',
    ];
    
    $validatedData['price'] = $validatedData['price'] * 100;
    
    return $validatedData;
}

Please sign in or create an account to participate in this conversation.