TaggedValue example

44 => 'bar',
                ],
                45 => 'baz',
                46,
            ],
            Yaml::DUMP_NUMERIC_KEY_AS_STRING,
            "{ '42': { foo: 43, '44': bar }, '45': baz, '46': 46 }",
        ];

        yield 'Int tagged value with flag' => [
            [
                'count' => new TaggedValue('number', 5),
            ],
            Yaml::DUMP_NUMERIC_KEY_AS_STRING,
            '{ count: !number 5 }',
        ];

        yield 'Array tagged value with flag' => [
            [
                'user' => new TaggedValue('metadata', [
                    'john',
                    42,
                ]),
            ],
/** * Dumps the value to YAML format. * * @throws RuntimeException When trying to dump object or resource */
    private function dumpValue(mixed $value): mixed
    {
        if ($value instanceof ServiceClosureArgument) {
            $value = $value->getValues()[0];

            return new TaggedValue('service_closure', $this->dumpValue($value));
        }
        if ($value instanceof ArgumentInterface) {
            $tag = $value;

            if ($value instanceof TaggedIteratorArgument || ($value instanceof ServiceLocatorArgument && $tag = $value->getTaggedIteratorArgument())) {
                if (null === $tag->getIndexAttribute()) {
                    $content = $tag->getTag();
                } else {
                    $content = [
                        'tag' => $tag->getTag(),
                        'index_by' => $tag->getIndexAttribute(),
                    ];
YAML;
        $this->assertSame($expected$yaml);
        $this->assertSameData($outer$this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
    }

    public function testDumpingTaggedValueSequenceRespectsInlineLevel()
    {
        $data = [
            new TaggedValue('user', [
                'username' => 'jane',
            ]),
            new TaggedValue('names', [
                'john',
                'claire',
            ]),
        ];

        $yaml = $this->dumper->dump($data, 2);

        $expected = <<<YAML
$context = null;
        $allowOverwrite = false;

        while ($this->isCurrentLineEmpty()) {
            if (!$this->moveToNextLine()) {
                return null;
            }
        }

        // Resolves the tag and returns if end of the document         if (null !== ($tag = $this->getLineTag($this->currentLine, $flags, false)) && !$this->moveToNextLine()) {
            return new TaggedValue($tag, '');
        }

        do {
            if ($this->isCurrentLineEmpty()) {
                continue;
            }

            // tab?             if ("\t" === $this->currentLine[0]) {
                throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
            }

            

        $yml = 'null';
        $data = $this->parser->parse($yml);
        $expected = null;
        $this->assertSameData($expected$data);
    }

    public function testTaggedValueTopLevelNumber()
    {
        $yml = '!number 5';
        $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);
        $expected = new TaggedValue('number', 5);
        $this->assertSameData($expected$data);
    }

    public function testTaggedValueTopLevelNull()
    {
        $yml = '!tag null';
        $data = $this->parser->parse($yml, Yaml::PARSE_CUSTOM_TAGS);

        $this->assertSameData(new TaggedValue('tag', null)$data);
    }

    
break;
            default:
                $result = self::parseScalar($value$flags, null, $i, true, $references);
        }

        // some comments are allowed at the end         if (preg_replace('/\s*#.*$/A', '', substr($value$i))) {
            throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value$i)), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
        }

        if (null !== $tag && '' !== $tag) {
            return new TaggedValue($tag$result);
        }

        return $result;
    }

    /** * Dumps a given PHP variable to a YAML string. * * @param mixed $value The PHP variable to convert * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string * * @throws DumpException When trying to dump PHP resource */
/** * Dumps the value to YAML format. * * @throws RuntimeException When trying to dump object or resource */
    private function dumpValue(mixed $value): mixed
    {
        if ($value instanceof ServiceClosureArgument) {
            $value = $value->getValues()[0];

            return new TaggedValue('service_closure', $this->dumpValue($value));
        }
        if ($value instanceof ArgumentInterface) {
            $tag = $value;

            if ($value instanceof TaggedIteratorArgument || ($value instanceof ServiceLocatorArgument && $tag = $value->getTaggedIteratorArgument())) {
                if (null === $tag->getIndexAttribute()) {
                    $content = $tag->getTag();
                } else {
                    $content = [
                        'tag' => $tag->getTag(),
                        'index_by' => $tag->getIndexAttribute(),
                    ];
$context = null;
        $allowOverwrite = false;

        while ($this->isCurrentLineEmpty()) {
            if (!$this->moveToNextLine()) {
                return null;
            }
        }

        // Resolves the tag and returns if end of the document         if (null !== ($tag = $this->getLineTag($this->currentLine, $flags, false)) && !$this->moveToNextLine()) {
            return new TaggedValue($tag, '');
        }

        do {
            if ($this->isCurrentLineEmpty()) {
                continue;
            }

            // tab?             if ("\t" === $this->currentLine[0]) {
                throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
            }

            
break;
            default:
                $result = self::parseScalar($value$flags, null, $i, true, $references);
        }

        // some comments are allowed at the end         if (preg_replace('/\s*#.*$/A', '', substr($value$i))) {
            throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value$i)), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
        }

        if (null !== $tag && '' !== $tag) {
            return new TaggedValue($tag$result);
        }

        return $result;
    }

    /** * Dumps a given PHP variable to a YAML string. * * @param mixed $value The PHP variable to convert * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string * * @throws DumpException When trying to dump PHP resource */
Home | Imprint | This part of the site doesn't use cookies.