1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\Application\UI;
9:
10: use Nette;
11:
12:
13: /**
14: * Lazy encapsulation of Component::link().
15: * Do not instantiate directly, use Component::lazyLink()
16: */
17: class Link
18: {
19: use Nette\SmartObject;
20:
21: /** @var Component */
22: private $component;
23:
24: /** @var string */
25: private $destination;
26:
27: /** @var array */
28: private $params;
29:
30:
31: /**
32: * Link specification.
33: */
34: public function __construct(Component $component, $destination, array $params = [])
35: {
36: $this->component = $component;
37: $this->destination = $destination;
38: $this->params = $params;
39: }
40:
41:
42: /**
43: * Returns link destination.
44: * @return string
45: */
46: public function getDestination()
47: {
48: return $this->destination;
49: }
50:
51:
52: /**
53: * Changes link parameter.
54: * @param string
55: * @param mixed
56: * @return static
57: */
58: public function setParameter($key, $value)
59: {
60: $this->params[$key] = $value;
61: return $this;
62: }
63:
64:
65: /**
66: * Returns link parameter.
67: * @param string
68: * @return mixed
69: */
70: public function getParameter($key)
71: {
72: return isset($this->params[$key]) ? $this->params[$key] : null;
73: }
74:
75:
76: /**
77: * Returns link parameters.
78: * @return array
79: */
80: public function getParameters()
81: {
82: return $this->params;
83: }
84:
85:
86: /**
87: * Converts link to URL.
88: * @return string
89: */
90: public function __toString()
91: {
92: try {
93: return $this->component->link($this->destination, $this->params);
94:
95: } catch (\Exception $e) {
96: } catch (\Throwable $e) {
97: }
98: if (isset($e)) {
99: if (func_num_args()) {
100: throw $e;
101: }
102: trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
103: }
104: }
105: }
106: