Skip to content

Conversation

andrewnester
Copy link
Contributor

Fixes #267

Copy link

@adayth adayth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems OK to me, just some comments.

* @param int $options Options to be used to encode data into JSON.
* @return string Rendered output
*/
public function renderDataWithOptions($options = 0): string
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a new method, I think it will be more easy to understand if we add an options property to configure a JsonRenderer instance and continue to use the renderData method. This way we can chain calls with another decorators forever.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adayth the idea here is to show that we can add some additional method (behaviour) to the class

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good example of chaining decorators https://sourcemaking.com/design_patterns/decorator
But maybe in the current Json/Xml example chaining is not useful at all. You will render Xml, Json or another one like Csv for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adayth you could create chaining with JsonRenderer::renderData if you need.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @andrewnester probably I didn't explain clearly. Maybe a code example will be better.
To me the pull request is good as is, but I think It could be better this way.

class JsonRenderer extends RendererDecorator
{
  private $options;

  public  __construct($options = 0)
  {
    $this->options = $options;
  }

  public function setOptions($options)
  {
     $this->options = $options;
  }

  public function renderData(): string
  {
     return json_encode($this->wrapped->renderData(), $options);
  }
}

// Example
$service = new Decorator\Webservice('foo/bar');
$decorated_service1 = new Decorator\JsonRenderer($this->service, JSON_UNESCAPED_SLASHES);
$decorated_service2 = new FooRenderer($decorated_service1);
$rendered_data = $decorated_service2->renderData();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like @adayth solution here (of course complete with type hints I would be even better). But to show chaining of decorators, this is a bad example and should be replaced with something new.

@@ -14,20 +14,21 @@ class DecoratorTest extends TestCase

protected function setUp()
{
$this->service = new Decorator\Webservice('foobar');
$this->service = new Decorator\Webservice('foo/bar');
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add a test for Decorator\Webservice, so before testing decorators, you are sure that Webservice does its job.

@andrewnester
Copy link
Contributor Author

@domnikl any feedback on this?


/**
* Renders data as JSON based on options passed.
* In terms of decorator patters, it represents additional extended behaviour.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

theres a typo here: "patters"

* @param int $options Options to be used to encode data into JSON.
* @return string Rendered output
*/
public function renderDataWithOptions($options = 0): string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like @adayth solution here (of course complete with type hints I would be even better). But to show chaining of decorators, this is a bad example and should be replaced with something new.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants