Data.php
3 years ago
Directory.php
3 years ago
FileExtension.php
3 years ago
Folder.php
3 years ago
Folders.php
3 years ago
Func.php
3 years ago
Functions.php
3 years ago
Name.php
3 years ago
Template.php
3 years ago
Data.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\League\Plates\Template; |
| 4 | |
| 5 | use LogicException; |
| 6 | /** |
| 7 | * Preassigned template data. |
| 8 | */ |
| 9 | class Data |
| 10 | { |
| 11 | /** |
| 12 | * Variables shared by all templates. |
| 13 | * @var array |
| 14 | */ |
| 15 | protected $sharedVariables = array(); |
| 16 | /** |
| 17 | * Specific template variables. |
| 18 | * @var array |
| 19 | */ |
| 20 | protected $templateVariables = array(); |
| 21 | /** |
| 22 | * Add template data. |
| 23 | * @param array $data; |
| 24 | * @param null|string|array $templates; |
| 25 | * @return Data |
| 26 | */ |
| 27 | public function add(array $data, $templates = null) |
| 28 | { |
| 29 | if (\is_null($templates)) { |
| 30 | return $this->shareWithAll($data); |
| 31 | } |
| 32 | if (\is_array($templates)) { |
| 33 | return $this->shareWithSome($data, $templates); |
| 34 | } |
| 35 | if (\is_string($templates)) { |
| 36 | return $this->shareWithSome($data, array($templates)); |
| 37 | } |
| 38 | throw new LogicException('The templates variable must be null, an array or a string, ' . \gettype($templates) . ' given.'); |
| 39 | } |
| 40 | /** |
| 41 | * Add data shared with all templates. |
| 42 | * @param array $data; |
| 43 | * @return Data |
| 44 | */ |
| 45 | public function shareWithAll($data) |
| 46 | { |
| 47 | $this->sharedVariables = \array_merge($this->sharedVariables, $data); |
| 48 | return $this; |
| 49 | } |
| 50 | /** |
| 51 | * Add data shared with some templates. |
| 52 | * @param array $data; |
| 53 | * @param array $templates; |
| 54 | * @return Data |
| 55 | */ |
| 56 | public function shareWithSome($data, array $templates) |
| 57 | { |
| 58 | foreach ($templates as $template) { |
| 59 | if (isset($this->templateVariables[$template])) { |
| 60 | $this->templateVariables[$template] = \array_merge($this->templateVariables[$template], $data); |
| 61 | } else { |
| 62 | $this->templateVariables[$template] = $data; |
| 63 | } |
| 64 | } |
| 65 | return $this; |
| 66 | } |
| 67 | /** |
| 68 | * Get template data. |
| 69 | * @param null|string $template; |
| 70 | * @return array |
| 71 | */ |
| 72 | public function get($template = null) |
| 73 | { |
| 74 | if (isset($template, $this->templateVariables[$template])) { |
| 75 | return \array_merge($this->sharedVariables, $this->templateVariables[$template]); |
| 76 | } |
| 77 | return $this->sharedVariables; |
| 78 | } |
| 79 | } |
| 80 |