1<?php
2
3namespace PhpOffice\PhpSpreadsheet\Chart;
4
5class Legend
6{
7    /** Legend positions */
8    const XL_LEGEND_POSITION_BOTTOM = -4107; //    Below the chart.
9    const XL_LEGEND_POSITION_CORNER = 2; //    In the upper right-hand corner of the chart border.
10    const XL_LEGEND_POSITION_CUSTOM = -4161; //    A custom position.
11    const XL_LEGEND_POSITION_LEFT = -4131; //    Left of the chart.
12    const XL_LEGEND_POSITION_RIGHT = -4152; //    Right of the chart.
13    const XL_LEGEND_POSITION_TOP = -4160; //    Above the chart.
14
15    const POSITION_RIGHT = 'r';
16    const POSITION_LEFT = 'l';
17    const POSITION_BOTTOM = 'b';
18    const POSITION_TOP = 't';
19    const POSITION_TOPRIGHT = 'tr';
20
21    private static $positionXLref = [
22        self::XL_LEGEND_POSITION_BOTTOM => self::POSITION_BOTTOM,
23        self::XL_LEGEND_POSITION_CORNER => self::POSITION_TOPRIGHT,
24        self::XL_LEGEND_POSITION_CUSTOM => '??',
25        self::XL_LEGEND_POSITION_LEFT => self::POSITION_LEFT,
26        self::XL_LEGEND_POSITION_RIGHT => self::POSITION_RIGHT,
27        self::XL_LEGEND_POSITION_TOP => self::POSITION_TOP,
28    ];
29
30    /**
31     * Legend position.
32     *
33     * @var string
34     */
35    private $position = self::POSITION_RIGHT;
36
37    /**
38     * Allow overlay of other elements?
39     *
40     * @var bool
41     */
42    private $overlay = true;
43
44    /**
45     * Legend Layout.
46     *
47     * @var Layout
48     */
49    private $layout;
50
51    /**
52     * Create a new Legend.
53     *
54     * @param string $position
55     * @param null|Layout $layout
56     * @param bool $overlay
57     */
58    public function __construct($position = self::POSITION_RIGHT, Layout $layout = null, $overlay = false)
59    {
60        $this->setPosition($position);
61        $this->layout = $layout;
62        $this->setOverlay($overlay);
63    }
64
65    /**
66     * Get legend position as an excel string value.
67     *
68     * @return string
69     */
70    public function getPosition()
71    {
72        return $this->position;
73    }
74
75    /**
76     * Get legend position using an excel string value.
77     *
78     * @param string $position see self::POSITION_*
79     *
80     * @return bool
81     */
82    public function setPosition($position)
83    {
84        if (!in_array($position, self::$positionXLref)) {
85            return false;
86        }
87
88        $this->position = $position;
89
90        return true;
91    }
92
93    /**
94     * Get legend position as an Excel internal numeric value.
95     *
96     * @return int
97     */
98    public function getPositionXL()
99    {
100        return array_search($this->position, self::$positionXLref);
101    }
102
103    /**
104     * Set legend position using an Excel internal numeric value.
105     *
106     * @param int $positionXL see self::XL_LEGEND_POSITION_*
107     *
108     * @return bool
109     */
110    public function setPositionXL($positionXL)
111    {
112        if (!isset(self::$positionXLref[$positionXL])) {
113            return false;
114        }
115
116        $this->position = self::$positionXLref[$positionXL];
117
118        return true;
119    }
120
121    /**
122     * Get allow overlay of other elements?
123     *
124     * @return bool
125     */
126    public function getOverlay()
127    {
128        return $this->overlay;
129    }
130
131    /**
132     * Set allow overlay of other elements?
133     *
134     * @param bool $overlay
135     *
136     * @return bool
137     */
138    public function setOverlay($overlay)
139    {
140        if (!is_bool($overlay)) {
141            return false;
142        }
143
144        $this->overlay = $overlay;
145
146        return true;
147    }
148
149    /**
150     * Get Layout.
151     *
152     * @return Layout
153     */
154    public function getLayout()
155    {
156        return $this->layout;
157    }
158}
159