templatestring.class.inc.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Simple helper class to interpret and transform a template string
  20. *
  21. * Usage:
  22. * $oString = new TemplateString("Blah $this->friendlyname$ is in location $this->location_id->name$ ('$this->location_id->org_id->name$)");
  23. * echo $oString->Render(array('this' => $oContact));
  24. * @copyright Copyright (C) 2010-2012 Combodo SARL
  25. * @license http://opensource.org/licenses/AGPL-3.0
  26. */
  27. /**
  28. * Helper class
  29. */
  30. class TemplateStringPlaceholder
  31. {
  32. public $sToken;
  33. public $sAttCode;
  34. public $sFunction;
  35. public $sParamName;
  36. public $bIsValid;
  37. public function __construct($sToken)
  38. {
  39. $this->sToken = $sToken;
  40. $this->sAttcode = '';
  41. $this->sFunction = '';
  42. $this->sParamName = '';
  43. $this->bIsValid = false; // Validity may be false in general, but it can work anyway (thanks to specialization) when rendering
  44. }
  45. }
  46. /**
  47. * Class TemplateString
  48. */
  49. class TemplateString
  50. {
  51. protected $m_sRaw;
  52. protected $m_aPlaceholders;
  53. public function __construct($sRaw)
  54. {
  55. $this->m_sRaw = $sRaw;
  56. $this->m_aPlaceholders = null;
  57. }
  58. /**
  59. * Split the string into placholders
  60. * @param Hash $aParamTypes Class of the expected parameters: hash array of '<param_id>' => '<class_name>'
  61. * @return void
  62. */
  63. protected function Analyze($aParamTypes = array())
  64. {
  65. if (!is_null($this->m_aPlaceholders)) return;
  66. $this->m_aPlaceholders = array();
  67. if (preg_match_all('/\\$([a-z0-9_]+(->[a-z0-9_]+)*)\\$/', $this->m_sRaw, $aMatches))
  68. {
  69. foreach($aMatches[1] as $sPlaceholder)
  70. {
  71. $oPlaceholder = new TemplateStringPlaceholder($sPlaceholder);
  72. $oPlaceholder->bIsValid = false;
  73. foreach ($aParamTypes as $sParamName => $sClass)
  74. {
  75. $sParamPrefix = $sParamName.'->';
  76. if (substr($sPlaceholder, 0, strlen($sParamPrefix)) == $sParamPrefix)
  77. {
  78. // Todo - detect functions (label...)
  79. $oPlaceholder->sFunction = '';
  80. $oPlaceholder->sParamName = $sParamName;
  81. $sAttcode = substr($sPlaceholder, strlen($sParamPrefix));
  82. $oPlaceholder->sAttcode = $sAttcode;
  83. $oPlaceholder->bIsValid = MetaModel::IsValidAttCode($sClass, $sAttcode, true /* extended */);
  84. }
  85. }
  86. $this->m_aPlaceholders[] = $oPlaceholder;
  87. }
  88. }
  89. }
  90. /**
  91. * Return the placeholders (for reporting purposes)
  92. * @return void
  93. */
  94. public function GetPlaceholders()
  95. {
  96. return $this->m_aPlaceholders;
  97. }
  98. /**
  99. * Check the format when possible
  100. * @param Hash $aParamTypes Class of the expected parameters: hash array of '<param_id>' => '<class_name>'
  101. * @return void
  102. */
  103. public function IsValid($aParamTypes = array())
  104. {
  105. $this->Analyze($aParamTypes);
  106. foreach($this->m_aPlaceholders as $oPlaceholder)
  107. {
  108. if (!$oPlaceholder->bIsValid)
  109. {
  110. if (count($aParamTypes) == 0)
  111. {
  112. return false;
  113. }
  114. if (array_key_exists($oPlaceholder->sParamName, $aParamTypes))
  115. {
  116. return false;
  117. }
  118. }
  119. }
  120. return true;
  121. }
  122. /**
  123. * Apply the given parameters to replace the placeholders
  124. * @param Hash $aParamValues Value of the expected parameters: hash array of '<param_id>' => '<value>'
  125. * @return void
  126. */
  127. public function Render($aParamValues = array())
  128. {
  129. $aParamTypes = array();
  130. foreach($aParamValues as $sParamName => $value)
  131. {
  132. $aParamTypes[$sParamName] = get_class($value);
  133. }
  134. $this->Analyze($aParamTypes);
  135. $aSearch = array();
  136. $aReplace = array();
  137. foreach($this->m_aPlaceholders as $oPlaceholder)
  138. {
  139. if (array_key_exists($oPlaceholder->sParamName, $aParamValues))
  140. {
  141. $oRef = $aParamValues[$oPlaceholder->sParamName];
  142. try
  143. {
  144. $value = $oRef->Get($oPlaceholder->sAttcode);
  145. $aSearch[] = '$'.$oPlaceholder->sToken.'$';
  146. $aReplace[] = $value;
  147. $oPlaceholder->bIsValid = true;
  148. }
  149. catch(Exception $e)
  150. {
  151. $oPlaceholder->bIsValid = false;
  152. }
  153. }
  154. else
  155. {
  156. $oPlaceholder->bIsValid = false;
  157. }
  158. }
  159. return str_replace($aSearch, $aReplace, $this->m_sRaw);
  160. }
  161. }
  162. ?>