I have a library /Web/ such as follows

/Web/
    Builder/
        Builder.php
    Exceptions/
        ExceptionHandler.php
    JSONSerializer/
        CategoryJSONSerializer.php
        ProjectJSONSerializer.php 
        ....
    Module/
        Category/
            Controller/
                Category.php
            Service/
                Category.php

I want to create a new class that will prepare any strings for insertion when being passed through the controller before being passed to the model for insertion.

public function putIndex()
{
    try {
        try {
            $this->category->edit(
                new CategoryVO($this->request->getParam('id'), $this->request->getParam('original')),
                new CategoryVO($this->request->getParam('id'), $this->request->getParam('edited'))
            );
            http_response_code(204);
        } catch (CategoryValidation $validationException) {
            echo json_encode($this->service->prepareErrors(
                $validationException
            ));
        }
    } catch(\Exception $e) {
        http_response_code(500);
    }
}

$this->request->getParam('id') is called, at which point the HTTP request is checked and value for id key is retrieved from the request header. I want to cleanse these strings, but I want this process to be testable (which my controllers currently aren't) and mockable if required.

I figured my Service/Category class could offer this service however as I mention later I require it across all controllers.

So I need to create a new class to escape strings, remove any special characters and remove any html. This to me sounds like multiple responsibilities, and was wondering if anybody could suggest a suitable way of achieving this.

Straight up I'm having difficulty even naming these class(es).

This service needs to be usable across all controllers, too. So I'd prefer to keep this class(es) in the /Web/ namespace.

  • It's unclear what your question is. – Mike Harris Mar 24 '16 at 20:25

You want to create utility class or helper class. Some possible example names would be StringUtils, StringHelper, RequestUtils or RequestHelper. That class should be static and have static method that would be called escape for example. Another possibility is to add that method into your Request class.

  • I'll take your advice with the naming, though for the case of phpunit/mocking I don't think I can mock static methods – Jack hardcastle Mar 24 '16 at 18:04
  • You don't need to mock static methods, they are just functions like json_encode(). – imel96 Mar 24 '16 at 19:27

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.