-
Notifications
You must be signed in to change notification settings - Fork 4.6k
RendererDecorator class should implement RenderableInterface. #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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();
There was a problem hiding this comment.
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'); | |||
} | |||
|
There was a problem hiding this comment.
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.
@domnikl any feedback on this? |
|
||
/** | ||
* Renders data as JSON based on options passed. | ||
* In terms of decorator patters, it represents additional extended behaviour. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
Fixes #267