/** * Selects one among a number of possible alternatives. * * <pre> * <!-- Category: instruction --> * <p:choose> * <!-- Content: (p:when+, p:otherwise?) --> * </p:choose> * * <p:when * test = boolean-expression> * <!-- Content: template --> * </p:when> * * <p:otherwise> * <!-- Content: template --> * </p:otherwise> * </pre> * * It consists of a sequence of `p:when` elements followed by an optional `p:otherwise` * element. Each `p:when` element has a single attribute, test, which specifies an expression. * The content of the `p:when` and `p:otherwise` elements is a template. When an `p:choose` * element is processed, each of the `p:when` elements is tested in turn, by evaluating the * expression and converting the resulting object to a boolean as if by a call to the boolean * function. The content of the first, and only the first, `p:when` element whose test is true * is instantiated. If no `p:when` is true, the content of the `p:otherwise` element is * instantiated. If no `p:when` element is true, and no `p:otherwise` element is present, * nothing is created. * * @param array $args * @param Engine $patron * @param mixed $template * * @return string */ public static function markup_choose(array $args, Engine $patron, $template) { $otherwise = null; # # handle 'when' children as they are defined. # if we find an 'otherwise' we keep it for later # foreach ($template as $node) { $name = $node->name; if ($name == 'otherwise') { $otherwise = $node; continue; } if ($name != 'when') { $patron->error('Unexpected child: :node', [':node' => $node]); return null; } $value = $patron->evaluate($node->args['test'], true); if ($value) { return $patron($node->nodes); } } # # otherwise # if (!$otherwise) { return null; } return $patron($otherwise->nodes); }