Extended
2 days ago
ExtendedValueLink.php
2 days ago
ExtendedValueLinkFactory.php
2 days ago
ExtendedValueRegistry.php
2 days ago
ExtendedValueLink.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Value; |
| 6 | |
| 7 | final class ExtendedValueLink |
| 8 | { |
| 9 | private string $view; |
| 10 | |
| 11 | private array $attributes; |
| 12 | |
| 13 | /** |
| 14 | * @var mixed |
| 15 | */ |
| 16 | private $id; |
| 17 | |
| 18 | private string $label; |
| 19 | |
| 20 | private array $params; |
| 21 | |
| 22 | public function __construct(string $label, $id, string $view, array $attributes = [], array $params = []) |
| 23 | { |
| 24 | $this->view = $view; |
| 25 | $this->attributes = $attributes; |
| 26 | $this->id = $id; |
| 27 | $this->label = $label; |
| 28 | $this->params = $params; |
| 29 | } |
| 30 | |
| 31 | protected function with_attribute(string $key, string $value): self |
| 32 | { |
| 33 | $this->attributes[$key] = $value; |
| 34 | |
| 35 | return new self($this->label, $this->id, $this->view, $this->attributes, $this->params); |
| 36 | } |
| 37 | |
| 38 | public function with_params($params): self |
| 39 | { |
| 40 | return new self($this->label, $this->id, $this->view, $this->attributes, $params); |
| 41 | } |
| 42 | |
| 43 | public function with_title(string $title): self |
| 44 | { |
| 45 | return $this->with_attribute('title', $title); |
| 46 | } |
| 47 | |
| 48 | public function with_edit_link(string $edit_link): self |
| 49 | { |
| 50 | return $this->with_attribute('edit_link', $edit_link); |
| 51 | } |
| 52 | |
| 53 | public function with_download_link(string $download_link): self |
| 54 | { |
| 55 | return $this->with_attribute('download_link', $download_link); |
| 56 | } |
| 57 | |
| 58 | public function with_view_link(string $download_link): self |
| 59 | { |
| 60 | return $this->with_attribute('view_link', $download_link); |
| 61 | } |
| 62 | |
| 63 | public function with_class(string $class): self |
| 64 | { |
| 65 | return $this->with_attribute('class', $class); |
| 66 | } |
| 67 | |
| 68 | public function render(): string |
| 69 | { |
| 70 | $attributes = $this->attributes; |
| 71 | $attribute_markup = []; |
| 72 | |
| 73 | if (isset($attributes['title']) && $attributes['title']) { |
| 74 | $attribute_markup[] = sprintf('data-modal-title="%s"', esc_attr($attributes['title'])); |
| 75 | } |
| 76 | if (isset($attributes['edit_link']) && $attributes['edit_link']) { |
| 77 | $attribute_markup[] = sprintf('data-modal-edit-link="%s"', esc_url($attributes['edit_link'])); |
| 78 | } |
| 79 | if (isset($attributes['download_link']) && $attributes['download_link']) { |
| 80 | $attribute_markup[] = sprintf('data-modal-download-link="%s"', esc_url($attributes['download_link'])); |
| 81 | } |
| 82 | if (isset($attributes['view_link']) && $attributes['view_link']) { |
| 83 | $attribute_markup[] = sprintf('data-modal-view-link="%s"', esc_url($attributes['view_link'])); |
| 84 | } |
| 85 | if (isset($attributes['class']) && $attributes['class']) { |
| 86 | $attribute_markup[] = sprintf('data-modal-class="%s"', esc_attr($attributes['class'])); |
| 87 | } |
| 88 | |
| 89 | if (! empty($this->params)) { |
| 90 | $attribute_markup[] = sprintf('data-modal-params="%s"', esc_attr(json_encode($this->params) ?: '')); |
| 91 | } |
| 92 | |
| 93 | $attribute_markup[] = sprintf('data-modal-id="%s"', esc_attr((string)$this->id)); |
| 94 | $attribute_markup[] = sprintf('data-view="%s"', esc_attr($this->view)); |
| 95 | |
| 96 | return sprintf( |
| 97 | '<a style="border-bottom: 1px dotted;" data-modal-value %s>%s</a>', |
| 98 | implode(' ', $attribute_markup), |
| 99 | $this->label |
| 100 | ); |
| 101 | } |
| 102 | } |
| 103 |