. namespace tool_generator\output; use templatable; use tool_generator\local\testscenario\runner; /** * Class stepsinformation * * @package tool_generator * @copyright 2024 Ferran Recio * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class stepsinformation implements templatable, \renderable { /** * Constructor. * * @param runner $runner the scenario runner */ public function __construct( /** @var runner the runner instance. */ public runner $runner ) { } /** * Export this data so it can be used as the context for a mustache template (core/inplace_editable). * * @param \renderer_base $output typically, the renderer that's calling this function * @return \stdClass data context for a mustache template */ public function export_for_template(\renderer_base $output): \stdClass { $steps = []; $validsteps = $this->runner->get_valid_steps(); foreach ($validsteps as $step) { $steps[] = (object)[ 'given' => $this->highlight_params($step->given), 'example' => $step->example, ]; } return (object) [ 'steps' => $steps, ]; } /** * Highlight the parameters in a step. * * @param string $step the step to highlight * @return string the step with the parameters highlighted */ private function highlight_params(string $step): string { $step = htmlentities($step); // Highlight params starting with ":". $step = preg_replace('/:(\w+)/', '$0', $step); // Highlight params enclosed in "(?P<...>)". $step = preg_replace('/\(\?P<(\w+)>((?:[^"]|\\")*)\)/', '$0', $step); return $step; } }