PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Application/UI/Link.php

https://github.com/lm/nette
PHP | 108 lines | 40 code | 21 blank | 47 comment | 0 complexity | a8328e96efd7c5390115f1fa9de47e75 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (http://nette.org)
  4. *
  5. * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6. *
  7. * For the full copyright and license information, please view
  8. * the file license.txt that was distributed with this source code.
  9. */
  10. namespace Nette\Application\UI;
  11. use Nette;
  12. /**
  13. * Lazy encapsulation of PresenterComponent::link().
  14. * Do not instantiate directly, use PresenterComponent::lazyLink()
  15. *
  16. * @author David Grudl
  17. * @internal
  18. *
  19. * @property-read string $destination
  20. * @property-read array $parameters
  21. */
  22. class Link extends Nette\Object
  23. {
  24. /** @var PresenterComponent */
  25. private $component;
  26. /** @var string */
  27. private $destination;
  28. /** @var array */
  29. private $params;
  30. /**
  31. * Link specification.
  32. */
  33. public function __construct(PresenterComponent $component, $destination, array $params)
  34. {
  35. $this->component = $component;
  36. $this->destination = $destination;
  37. $this->params = $params;
  38. }
  39. /**
  40. * Returns link destination.
  41. * @return string
  42. */
  43. public function getDestination()
  44. {
  45. return $this->destination;
  46. }
  47. /**
  48. * Changes link parameter.
  49. * @param string
  50. * @param mixed
  51. * @return self
  52. */
  53. public function setParameter($key, $value)
  54. {
  55. $this->params[$key] = $value;
  56. return $this;
  57. }
  58. /**
  59. * Returns link parameter.
  60. * @param string
  61. * @return mixed
  62. */
  63. public function getParameter($key)
  64. {
  65. return isset($this->params[$key]) ? $this->params[$key] : NULL;
  66. }
  67. /**
  68. * Returns link parameters.
  69. * @return array
  70. */
  71. public function getParameters()
  72. {
  73. return $this->params;
  74. }
  75. /**
  76. * Converts link to URL.
  77. * @return string
  78. */
  79. public function __toString()
  80. {
  81. try {
  82. return (string) $this->component->link($this->destination, $this->params);
  83. } catch (\Exception $e) {
  84. trigger_error("Exception in " . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
  85. }
  86. }
  87. }