HandlebarsArguments

Encapsulates helpers arguments.

Defined (2)

The class is defined in the following location(s).

/vendor/calderawp/metaplate-core/vendor/xamin/handlebars.php/src/Handlebars/Arguments.php  
  1. class Arguments 
  2. /** 
  3. * List of named arguments. 
  4. * @var array 
  5. */ 
  6. protected $namedArgs = array(); 
  7.  
  8. /** 
  9. * List of positional arguments. 
  10. * @var array 
  11. */ 
  12. protected $positionalArgs = array(); 
  13.  
  14. /** 
  15. * The original arguments string that was used to fill in arguments. 
  16. * @var string 
  17. */ 
  18. protected $originalString = ''; 
  19.  
  20. /** 
  21. * Class constructor. 
  22. * @param string $args_string Arguments string as passed to a helper. 
  23. */ 
  24. public function __construct($args_string = false) 
  25. $this->originalString = (string)$args_string; 
  26.  
  27. if ($this->originalString !== '') { 
  28. $this->parse($args_string); 
  29.  
  30. /** 
  31. * Returns string representation of the arguments list. 
  32. * This method is here mostly for backward compatibility reasons. 
  33. * @return string 
  34. */ 
  35. public function __toString() 
  36. return $this->originalString; 
  37.  
  38. /** 
  39. * Returns a list of named arguments. 
  40. * @return array 
  41. */ 
  42. public function getNamedArguments() 
  43. return $this->namedArgs; 
  44.  
  45. /** 
  46. * Returns a list of positional arguments. 
  47. * @return array 
  48. */ 
  49. public function getPositionalArguments() 
  50. return $this->positionalArgs; 
  51.  
  52. /** 
  53. * Breaks an argument string into arguments and stores them inside the 
  54. * object. 
  55. * @param string $args_string Arguments string as passed to a helper. 
  56. * @return void 
  57. * @throws \InvalidArgumentException 
  58. */ 
  59. protected function parse($args_string) 
  60. $bad_chars = preg_quote(Context::NOT_VALID_NAME_CHARS, '#'); 
  61. $bad_seg_chars = preg_quote(Context::NOT_VALID_SEGMENT_NAME_CHARS, '#'); 
  62.  
  63. $name_chunk = '(?:[^' . $bad_chars . '\s]+)|(?:\[[^' . $bad_seg_chars . ']+\])'; 
  64. $variable_name = '(?:\.\.\/)*(?:(?:' . $name_chunk . ')[\.\/])*(?:' . $name_chunk . ')\.?'; 
  65. $special_variable_name = '@[a-z]+'; 
  66. $escaped_value = '(?:(?<!\\\\)".*?(?<!\\\\)"|(?<!\\\\)\'.*?(?<!\\\\)\')'; 
  67. $argument_name = $name_chunk; 
  68. $argument_value = $variable_name . '|' . $escaped_value . '|' . $special_variable_name; 
  69. $positional_argument = '#^(' . $argument_value . ')#'; 
  70. $named_argument = '#^(' . $argument_name . ')\s*=\s*(' . $argument_value . ')#'; 
  71.  
  72. $current_str = trim($args_string); 
  73.  
  74. // Split arguments string 
  75. while (strlen($current_str) !== 0) { 
  76. if (preg_match($named_argument, $current_str, $matches)) { 
  77. // Named argument found 
  78. $name = $this->prepareArgumentName($matches[1]); 
  79. $value = $this->prepareArgumentValue($matches[2]); 
  80.  
  81. $this->namedArgs[$name] = $value; 
  82.  
  83. // Remove found argument from arguments string. 
  84. $current_str = ltrim(substr($current_str, strlen($matches[0]))); 
  85. } elseif (preg_match($positional_argument, $current_str, $matches)) { 
  86. // A positional argument found. It cannot follow named arguments 
  87. if (count($this->namedArgs) !== 0) { 
  88. throw new \InvalidArgumentException('Positional arguments cannot follow named arguments'); 
  89.  
  90. $this->positionalArgs[] = $this->prepareArgumentValue($matches[1]); 
  91.  
  92. // Remove found argument from arguments string. 
  93. $current_str = ltrim(substr($current_str, strlen($matches[0]))); 
  94. } else { 
  95. throw new \InvalidArgumentException('Malformed arguments string'); 
  96.  
  97. /** 
  98. * Prepares argument's value to add to arguments list. 
  99. * The method unescapes value and wrap it into \Handlebars\String class if 
  100. * needed. 
  101. * @param string $value Argument's value 
  102. * @return string|\Handlebars\String 
  103. */ 
  104. protected function prepareArgumentValue($value) 
  105. // Check if argument's value is a quoted string literal 
  106. if ($value[0] == "'" || $value[0] == '"') { 
  107. // Remove enclosing quotes and unescape 
  108. return new String(stripcslashes(substr($value, 1, -1))); 
  109.  
  110. // Check if the value is an integer literal 
  111. if (preg_match("/^-?\d+$/", $value)) { 
  112. // Wrap the value into the String class to tell the Context that 
  113. // it's a value and not a variable name. 
  114. return new String($value); 
  115.  
  116. return $value; 
  117.  
  118. /** 
  119. * Prepares argument's name. 
  120. * Remove sections braces if needed. 
  121. * @param strign $name Argument's name 
  122. * @return string 
  123. */ 
  124. protected function prepareArgumentName($name) 
  125. // Check if argument's name is a segment 
  126. if ($name[0] == '[') { 
  127. $name = substr($name, 1, -1); 
  128.  
  129. return $name; 
/vendor/xamin/handlebars.php/src/Handlebars/Arguments.php  
  1. class Arguments 
  2. /** 
  3. * List of named arguments. 
  4. * @var array 
  5. */ 
  6. protected $namedArgs = array(); 
  7.  
  8. /** 
  9. * List of positional arguments. 
  10. * @var array 
  11. */ 
  12. protected $positionalArgs = array(); 
  13.  
  14. /** 
  15. * The original arguments string that was used to fill in arguments. 
  16. * @var string 
  17. */ 
  18. protected $originalString = ''; 
  19.  
  20. /** 
  21. * Class constructor. 
  22. * @param string $args_string Arguments string as passed to a helper. 
  23. */ 
  24. public function __construct($args_string = false) 
  25. $this->originalString = (string)$args_string; 
  26.  
  27. if ($this->originalString !== '') { 
  28. $this->parse($args_string); 
  29.  
  30. /** 
  31. * Returns string representation of the arguments list. 
  32. * This method is here mostly for backward compatibility reasons. 
  33. * @return string 
  34. */ 
  35. public function __toString() 
  36. return $this->originalString; 
  37.  
  38. /** 
  39. * Returns a list of named arguments. 
  40. * @return array 
  41. */ 
  42. public function getNamedArguments() 
  43. return $this->namedArgs; 
  44.  
  45. /** 
  46. * Returns a list of positional arguments. 
  47. * @return array 
  48. */ 
  49. public function getPositionalArguments() 
  50. return $this->positionalArgs; 
  51.  
  52. /** 
  53. * Breaks an argument string into arguments and stores them inside the 
  54. * object. 
  55. * @param string $args_string Arguments string as passed to a helper. 
  56. * @return void 
  57. * @throws \InvalidArgumentException 
  58. */ 
  59. protected function parse($args_string) 
  60. $bad_chars = preg_quote(Context::NOT_VALID_NAME_CHARS, '#'); 
  61. $bad_seg_chars = preg_quote(Context::NOT_VALID_SEGMENT_NAME_CHARS, '#'); 
  62.  
  63. $name_chunk = '(?:[^' . $bad_chars . '\s]+)|(?:\[[^' . $bad_seg_chars . ']+\])'; 
  64. $variable_name = '(?:\.\.\/)*(?:(?:' . $name_chunk . ')[\.\/])*(?:' . $name_chunk . ')\.?'; 
  65. $special_variable_name = '@[a-z]+'; 
  66. $escaped_value = '(?:(?<!\\\\)".*?(?<!\\\\)"|(?<!\\\\)\'.*?(?<!\\\\)\')'; 
  67. $argument_name = $name_chunk; 
  68. $argument_value = $variable_name . '|' . $escaped_value . '|' . $special_variable_name; 
  69. $positional_argument = '#^(' . $argument_value . ')#'; 
  70. $named_argument = '#^(' . $argument_name . ')\s*=\s*(' . $argument_value . ')#'; 
  71.  
  72. $current_str = trim($args_string); 
  73.  
  74. // Split arguments string 
  75. while (strlen($current_str) !== 0) { 
  76. if (preg_match($named_argument, $current_str, $matches)) { 
  77. // Named argument found 
  78. $name = $this->prepareArgumentName($matches[1]); 
  79. $value = $this->prepareArgumentValue($matches[2]); 
  80.  
  81. $this->namedArgs[$name] = $value; 
  82.  
  83. // Remove found argument from arguments string. 
  84. $current_str = ltrim(substr($current_str, strlen($matches[0]))); 
  85. } elseif (preg_match($positional_argument, $current_str, $matches)) { 
  86. // A positional argument found. It cannot follow named arguments 
  87. if (count($this->namedArgs) !== 0) { 
  88. throw new \InvalidArgumentException('Positional arguments cannot follow named arguments'); 
  89.  
  90. $this->positionalArgs[] = $this->prepareArgumentValue($matches[1]); 
  91.  
  92. // Remove found argument from arguments string. 
  93. $current_str = ltrim(substr($current_str, strlen($matches[0]))); 
  94. } else { 
  95. throw new \InvalidArgumentException('Malformed arguments string'); 
  96.  
  97. /** 
  98. * Prepares argument's value to add to arguments list. 
  99. * The method unescapes value and wrap it into \Handlebars\String class if 
  100. * needed. 
  101. * @param string $value Argument's value 
  102. * @return string|\Handlebars\String 
  103. */ 
  104. protected function prepareArgumentValue($value) 
  105. // Check if argument's value is a quoted string literal 
  106. if ($value[0] == "'" || $value[0] == '"') { 
  107. // Remove enclosing quotes and unescape 
  108. return new String(stripcslashes(substr($value, 1, -1))); 
  109.  
  110. // Check if the value is an integer literal 
  111. if (preg_match("/^-?\d+$/", $value)) { 
  112. // Wrap the value into the String class to tell the Context that 
  113. // it's a value and not a variable name. 
  114. return new String($value); 
  115.  
  116. return $value; 
  117.  
  118. /** 
  119. * Prepares argument's name. 
  120. * Remove sections braces if needed. 
  121. * @param strign $name Argument's name 
  122. * @return string 
  123. */ 
  124. protected function prepareArgumentName($name) 
  125. // Check if argument's name is a segment 
  126. if ($name[0] == '[') { 
  127. $name = substr($name, 1, -1); 
  128.  
  129. return $name;