Replace a message format with context information.

The message format contains fields wrapped in curly braces.

@param string $format The message format to replace.
@param array $context The context data.
@return string Returns the formatted message.
489850
private function replaceContext(string $format, array $context): string {
        $msg = preg_replace_callback('`({[^\s{}]+})`', function ($m) use ($context) {
            $field = trim($m[1], '{}');
            if (array_key_exists($field, $context)) {
                return $context[$field];
            } else {
                return $m[1];
            }
        }, $format);
        return $msg;
    }
724035
private function interpolate($message, array $context = array())
    {
        $replaces = array();
        foreach ($context as $key => $val) {
            if (is_bool($val)) {
                $val = '[bool: ' . (int) $val . ']';
            } elseif (is_null($val)
                || is_scalar($val)
                || ( is_object($val) && method_exists($val, '__toString') )
            ) {
                $val = (string) $val;
            } elseif (is_array($val) || is_object($val)) {
                $val = @json_encode($val);
            } else {
                $val = '[type: ' . gettype($val) . ']';
            }
            $replaces['{' . $key . '}'] = $val;
        }
        return strtr($message, $replaces);
    }
1217541
public function format($format)
    {
        $format = str_replace('u', sprintf('%06d', $this->microseconds), $format);

        return parent::format($format);
    }
131624
public function normalize($object, $format = null, array $context = [])
    {
        foreach ($this->normalizers as $normalizer) {
            // Return the result from the first denormalizer that can
            // denormalize this.
            if ($normalizer->supportsNormalization($object, $format)) {
                return $normalizer->normalize($object, $format, $context);
            }
        }
    }
120930
public function normalize($object, $format = null, array $context = [])
    {
        $this->maxDepthHandler->handleMaxDepth($context);

        if ($this->byReferenceHandler->isByShortReference($context)) {
            return $this->getShortReference($object, $format, $context);
        }

        if ($this->byReferenceHandler->isByReference($context)) {
            return $this->getReference($object, $format, $context);
        }

        try {
            return parent::normalize($object, $format, $context);
        } catch (CircularReferenceException $e) {
            return $this->getShortReference($object, $format, $context);
        }
    }
692725
public function format($format)
    {
        $format = str_replace('%U', sprintf("%06d", $this->u), $format);
        $format = str_replace('%u', sprintf("%d", intval($this->u)), $format);

        return parent::format($format);
    }
986626
function interpolate($message, array $context = [])
    {
        // build a replacement array with braces around the context keys
        $replace = array();

        foreach ($context as $key => $val) {

            if (($key == 'exception' || $key == 'throwable') && $val instanceof \Throwable) {
                $val = $val->getTraceAsString();
            }

            if (is_object($val)) {
                $val = 'Objects can not be interpolated.';
            }

            $replace['{' . $key . '}'] = $val;
        }

        // interpolate replacement values into the message and return
        return strtr($message, $replace);
    }
950625
public function parseCustomFormat($format)
    {
        if (isset($this->parsedFormats[$format])) {
            return $this->parsedFormats[$format];
        }

        return $this->parsedFormats[$format] = $this->parseFormat($format);
    }
233874
public function normalize($data, $format = null, array $context = [])
    {
        if ($data instanceof ResourceInterface) {
            $event = new BeforeNormalizationEvent($data, (string) $format, $context);
            $this->eventDispatcher->dispatch(SerializationEvents::BEFORE_NORMALIZATION, $event);
        }

        $countNormalizers = 0;

        if (array_key_exists('normalizers', $context)) {
            $countNormalizers = count($context['normalizers']);
            /** @var NormalizerInterface[] $normalizers */
            $normalizers = array_reverse($context['normalizers']);

            foreach ($normalizers as $normalizer) {
                $this->prepareNormalizer($normalizer);
                array_unshift($this->normalizers, $normalizer);
            }
        }

        try {
            $normalized = parent::normalize($data, $format, $context);
        } finally {
            for ($i = 0; $i < $countNormalizers; $i++) {
                array_shift($this->normalizers);
            }
        }

        if ($data instanceof ResourceInterface) {
            $event = new AfterNormalizationEvent($data, $normalized, (string) $format, $context);
            $this->eventDispatcher->dispatch(SerializationEvents::AFTER_NORMALIZATION, $event);
        }

        return $normalized;
    }
1322307
public function setFormat($format)
    {
        if (!is_null($format)) {
            $this->format = $format;
            $this->useFormat(false);
        }

        return $this;
    }
1109819
private function getEncoder(string $format, array $context): EncoderInterface
    {
        if (isset($this->encoderByFormat[$format])
            && isset($this->encoders[$this->encoderByFormat[$format]])
        ) {
            return $this->encoders[$this->encoderByFormat[$format]];
        }

        foreach ($this->encoders as $i => $encoder) {
            if ($encoder->supportsEncoding($format, $context)) {
                $this->encoderByFormat[$format] = $i;

                return $encoder;
            }
        }

        throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
    }
262096
public function format($format) {
        
        // first of all check for convention
        $this->updateJalaliFromGeorgian();
        
        // Find what to replace
        $characters = (preg_match_all('/([a-zA-Z]{1})/', $format, $characters)) ? $characters[0] : [];
        
        // making the replace array
        $replaces = [];
        foreach ($characters as $character) {
            
            switch ($character) {
                
                // Intact ones
                case 'B':
                case 'h':
                case 'H':
                case 'g':
                case 'G':
                case 'i':
                case 's':
                case 'I':
                case 'U':
                case 'u':
                case 'Z':
                case 'O':
                case 'P':
                case 'e':
                case 'T':
                    $replace = $this->carbon->format($character);
                    break;
                
                // Day
                case 'd':
                    $replace = sprintf("%02d", $this->day);
                    break;
                case 'D':
                    $replace = static::getDayName($this->carbon->format('D'), true);
                    break;
                case 'j':
                    $replace = $this->day;
                    break;
                case 'l':
                    $replace = static::getDayName($this->carbon->format('l'));
                    break;
                case 'N':
                    $replace = static::getDayName($this->carbon->format('l'), false, 1, true);
                    break;
                case 'S':
                    $replace = static::t('th');
                    break;
                case 'w':
                    $replace = static::getDayName($this->carbon->format('l'), false, 1, true) - 1;
                    break;
                case 'z':
                    $replace = static::getDayOfYear($this->day, $this->month);
                    break;
                
                // Week
                case 'W':
                    $replace = ceil(static::getDayOfYear($this->day, $this->month) / 7);
                    break;
                
                // Month
                case 'F':
                    $replace = static::getMonthName($this->month);
                    break;
                case 'm':
                    $replace = sprintf("%02d", $this->month);
                    break;
                case 'M':
                    $replace = static::getMonthName($this->month, true);
                    break;
                case 'n':
                    $replace = $this->month;
                    break;
                case 't':
                    $replace = static::getMonthLength($this->month, $this->year);
                    break;
                
                // Year
                case 'L':
                    $replace = static::isLeapYear($this->year, true) ? 1 : 0;
                    break;
                case 'o':
                case 'Y':
                    $replace = $this->year;
                    break;
                case 'y':
                    $replace = $this->year % 100;
                    break;
                
                // Time
                case 'a':
                    $replace = static::t($this->carbon->format('a'));
                    break;
                case 'A':
                    $replace = static::t($this->carbon->format('a'));
                    break;
                
                // Full Dates
                case 'c':
                    $replace = $this->year . '-' .
                               sprintf("%02d", $this->month) . '-' .
                               sprintf("%02d", $this->day) . 'T' .
                               $this->carbon->format('H') . ':' .
                               $this->carbon->format('i') . ':' .
                               $this->carbon->format('s') .
                               $this->carbon->format('P');
                    break;
                case 'r':
                    $replace = static::getDayName($this->carbon->format('D'), true) . ', ' .
                               sprintf("%02d", $this->day) . ' ' .
                               static::getMonthName($this->month, true) . ' ' .
                               $this->year . ' ' .
                               $this->carbon->format('H') . ':' .
                               $this->carbon->format('i') . ':' .
                               $this->carbon->format('s') . ' ' .
                               $this->carbon->format('P');
                    break;
                default:
                    $replace = $character;
            }
            
            //do the replace
            $replaces[$character] = $replace;
            
        }
        
        return strtr($format, $replaces);
    }
1208781
public function normalize($string, $format = null, array $context = array())
    {
        $mime = isset($context[static::CONTEXT_MIME]) ? $context[static::CONTEXT_MIME] : 'application/octet-stream';
        $charset = isset($context[static::CONTEXT_CHARSET]) ? $context[static::CONTEXT_CHARSET] : null;
        $gzip = isset($context[static::CONTEXT_GZIP]) ? $context[static::CONTEXT_GZIP] : null;
        $base64 = isset($context[static::CONTEXT_BASE64]) ? $context[static::CONTEXT_BASE64] : null;
        $base64 = ($base64 !== null) ? $base64 : strpos($mime, '/octet-stream') !== false;
        $urlencode = isset($context[static::CONTEXT_URLENCODE]) ? $context[static::CONTEXT_URLENCODE] : null;
        $urlencode = ($urlencode !== null) ? $urlencode : strpos($mime, 'text/') !== false;

        if ($gzip) {
            $string = gzencode($string);
            $mime = 'application/x-gzip';
        }
        if ($urlencode and !$base64) {
            $string = urlencode($string);
        }
        if ($base64 or !$urlencode) {
            $string = base64_encode($string);
        }

        $options = $mime;
        if ($charset and $mime) {
            $options .= ';charset='.$charset;
        }
        if ($base64) {
            $options .= ';base64';
        }

        return sprintf('data:%s,%s', $options, $string);
    }
698433
public static function momentFormat($format)
    {
        $replacements = [
            'd' => 'DD',
            'D' => 'ddd',
            'j' => 'D',
            'l' => 'dddd',
            'N' => 'E',
            'S' => 'o',
            'w' => 'e',
            'z' => 'DDD',
            'W' => 'W',
            'F' => 'MMMM',
            'm' => 'MM',
            'M' => 'MMM',
            'n' => 'M',
            't' => '', // no equivalent
            'L' => '', // no equivalent
            'o' => 'YYYY',
            'Y' => 'YYYY',
            'y' => 'YY',
            'a' => 'a',
            'A' => 'A',
            'B' => '', // no equivalent
            'g' => 'h',
            'G' => 'H',
            'h' => 'hh',
            'H' => 'HH',
            'i' => 'mm',
            's' => 'ss',
            'u' => 'SSS',
            'e' => 'zz', // deprecated since version 1.6.0 of moment.js
            'I' => '', // no equivalent
            'O' => '', // no equivalent
            'P' => '', // no equivalent
            'T' => '', // no equivalent
            'Z' => '', // no equivalent
            'c' => '', // no equivalent
            'r' => '', // no equivalent
            'U' => 'X',
        ];

        foreach ($replacements as $from => $to) {
            $replacements['\\'.$from] = '['.$from.']';
        }

        return strtr($format, $replacements);
    }
765705
public function normalize($object, $format = null, array $context = [])
    {
        /** @var object $normalized */
        $normalized = parent::normalize($object, $format, $context);
        // Remove properties that saved to attributes on apps.
        unset($normalized->displayName);
        unset($normalized->description);

        return $normalized;
    }
1347745
public function normalize($object, $format = null, array $context = [])
    {
        $return = [
            'property' => parent::normalize($object, $format, $context),
        ];

        return (object) $return;
    }
558208
public function format(string $format)
    {
        $result = '';
        $length = \mb_strlen($format);

        $skip_next = false;

        // iterate for each character
        for ($index = 0; $index < $length; $index++) {
            $format_char = mb_substr($format, $index, 1);

            $result .= $this->getValueOfFormatCharacter($format_char, $skip_next);
        }

        // remove null bits if they exist
        return str_replace("\0", '', $result);
    }