1<?php 2/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ 3 4namespace Icinga\Chart\Render; 5 6use Icinga\Chart\Render\RenderContext; 7use Icinga\Chart\Primitive\Drawable; 8use DOMElement; 9 10/** 11 * Class Rotator 12 * @package Icinga\Chart\Render 13 */ 14class Rotator implements Drawable 15{ 16 /** 17 * The drawable element to rotate 18 * 19 * @var Drawable 20 */ 21 private $element; 22 23 /** 24 * @var int 25 */ 26 private $degrees; 27 28 /** 29 * Wrap an element into a new instance of Rotator 30 * 31 * @param Drawable $element The element to rotate 32 * @param int $degrees The amount of degrees 33 */ 34 public function __construct(Drawable $element, $degrees) 35 { 36 $this->element = $element; 37 $this->degrees = $degrees; 38 } 39 40 /** 41 * Rotate the given element. 42 * 43 * @param RenderContext $ctx The rendering context 44 * @param DOMElement $el The element to rotate 45 * @param $degrees The amount of degrees 46 * 47 * @return DOMElement The rotated DOMElement 48 */ 49 private function rotate(RenderContext $ctx, DOMElement $el, $degrees) 50 { 51 // Create a box containing the rotated element relative to the original element position 52 $container = $ctx->getDocument()->createElement('g'); 53 $x = $el->getAttribute('x'); 54 $y = $el->getAttribute('y'); 55 $container->setAttribute('transform', 'translate(' . $x . ',' . $y . ')'); 56 $el->removeAttribute('x'); 57 $el->removeAttribute('y'); 58 59 // Put the element into a rotated group 60 //$rotate = $ctx->getDocument()->createElement('g'); 61 $el->setAttribute('transform', 'rotate(' . $degrees . ')'); 62 //$rotate->appendChild($el); 63 64 $container->appendChild($el); 65 return $container; 66 } 67 68 /** 69 * Create the SVG representation from this Drawable 70 * 71 * @param RenderContext $ctx The context to use for rendering 72 * 73 * @return DOMElement The SVG Element 74 */ 75 public function toSvg(RenderContext $ctx) 76 { 77 $el = $this->element->toSvg($ctx); 78 return $this->rotate($ctx, $el, $this->degrees); 79 } 80} 81