function xsprintf_command()

in src/xsprintf/csprintf.php [56:140]


function xsprintf_command($userdata, &$pattern, &$pos, &$value, &$length) {
  $type = $pattern[$pos];
  $next = (strlen($pattern) > $pos + 1) ? $pattern[$pos + 1] : null;

  $is_unmasked = !empty($userdata['unmasked']);

  if (empty($userdata['mode'])) {
    $mode = PhutilCommandString::MODE_DEFAULT;
  } else {
    $mode = $userdata['mode'];
  }

  if ($value instanceof PhutilCommandString) {
    if ($is_unmasked) {
      $value = $value->getUnmaskedString();
    } else {
      $value = $value->getMaskedString();
    }
  }

  switch ($type) {
    case 'L':
      // Remove the L.
      $pattern = substr_replace($pattern, '', $pos, 1);
      $length  = strlen($pattern);
      $type = 's';

      // Check that the value is a non-empty array.
      if (!is_array($value)) {
        throw new InvalidArgumentException(
          pht('Expected an array for %%L%s conversion.', $next));
      }

      switch ($next) {
        case 's':
          $values = array();
          foreach ($value as $val) {
            $values[] = csprintf('%s', $val);
          }
          $value = implode(' ', $values);
          break;

        case 'R':
          $values = array();
          foreach ($value as $val) {
            $values[] = csprintf('%R', $val);
          }
          $value = implode(' ', $values);
          break;

        default:
          throw new XsprintfUnknownConversionException("%L{$next}");
      }
      break;

    case 'R':
      if (!preg_match('(^[a-zA-Z0-9:/@._+-]+$)', $value)) {
        $value = PhutilCommandString::escapeArgument($value, $mode);
      }
      $type = 's';
      break;
    case 's':
      $value = PhutilCommandString::escapeArgument($value, $mode);
      $type = 's';
      break;
    case 'P':
      if (!($value instanceof PhutilOpaqueEnvelope)) {
        throw new InvalidArgumentException(
          pht('Expected %s for %%P conversion.', 'PhutilOpaqueEnvelope'));
      }
      if ($is_unmasked) {
        $value = $value->openEnvelope();
      } else {
        $value = '********';
      }
      $value = PhutilCommandString::escapeArgument($value, $mode);
      $type = 's';
      break;
    case 'C':
      $type = 's';
      break;
  }

  $pattern[$pos] = $type;
}