| 1 | <?php namespace FlatPlan\Components; |
||
| 2 | |||
| 3 | class Text extends AbstractComponent { |
||
| 4 | |||
| 5 | protected $text; |
||
| 6 | protected $format; |
||
| 7 | |||
| 8 | protected $formats = ['html', 'markdown', 'none']; |
||
| 9 | protected $roles = [ |
||
| 10 | 'body', |
||
| 11 | 'title', |
||
| 12 | 'heading', |
||
| 13 | 'heading1', |
||
| 14 | 'heading2', |
||
| 15 | 'heading3', |
||
| 16 | 'heading4', |
||
| 17 | 'heading5', |
||
| 18 | 'heading6', |
||
| 19 | 'intro', |
||
| 20 | 'caption', |
||
| 21 | 'author', |
||
| 22 | 'byline', |
||
| 23 | 'illustrator', |
||
| 24 | 'photographer', |
||
| 25 | 'quote', |
||
| 26 | 'pullquote', |
||
| 27 | 'link_button' |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $role |
||
| 32 | * @param string $text |
||
| 33 | * @param string $format |
||
| 34 | * @param string $url |
||
| 35 | * @return void |
||
| 36 | */ |
||
| 37 | public function __construct($role, $text, $format = 'html', $url = null) |
||
| 38 | { |
||
| 39 | if (empty($text)) { |
||
| 40 | return false; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 41 | } |
||
| 42 | |||
| 43 | if ($role === 'link_button') { |
||
| 44 | if (is_null($url)) { |
||
| 45 | return false; |
||
|
0 ignored issues
–
show
|
|||
| 46 | } |
||
| 47 | $this->setUrl($url); |
||
| 48 | } |
||
| 49 | |||
| 50 | $this->setRole($role); |
||
| 51 | $this->setText($text); |
||
| 52 | $this->setFormat($format); |
||
| 53 | } |
||
| 54 | |||
| 55 | private function setText($text) |
||
| 56 | { |
||
| 57 | $this->text = $text; |
||
| 58 | } |
||
| 59 | |||
| 60 | private function getText() |
||
| 61 | { |
||
| 62 | return $this->text; |
||
| 63 | } |
||
| 64 | |||
| 65 | private function setFormat($format = null) |
||
| 66 | { |
||
| 67 | if (!in_array($format, $this->formats)) { |
||
| 68 | throw new \ErrorException('Invalid format supplied.'); |
||
| 69 | } |
||
| 70 | $this->format = $format; |
||
| 71 | } |
||
| 72 | |||
| 73 | private function getFormat() |
||
| 74 | { |
||
| 75 | return $this->format; |
||
| 76 | } |
||
| 77 | |||
| 78 | private function setUrl($url) |
||
| 79 | { |
||
| 80 | $this->url = $url; |
||
|
0 ignored issues
–
show
|
|||
| 81 | } |
||
| 82 | |||
| 83 | private function getUrl() |
||
| 84 | { |
||
| 85 | return $this->url; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function getComponent() |
||
| 89 | { |
||
| 90 | $component = new \stdClass(); |
||
| 91 | $component->role = $this->getRole(); |
||
| 92 | $component->text = $this->getText(); |
||
| 93 | if ($this->getRole() === 'link_button') { |
||
| 94 | $component->URL = $this->getUrl(); |
||
| 95 | } else { |
||
| 96 | $component->format = $this->getFormat(); |
||
| 97 | } |
||
| 98 | $component->layout = $this->getLayout(); |
||
| 99 | $component->style = $this->getStyle(); |
||
| 100 | $component->textStyle = $this->getTextStyle(); |
||
| 101 | if (!is_null($this->behaviour)) { |
||
| 102 | $component->behaviour = $this->getBehaviour(); |
||
| 103 | } |
||
| 104 | return $component; |
||
| 105 | } |
||
| 106 | } |
||
| 107 |