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