Download the PHP package thamtech/yii2-yaml without Composer
On this page you can find all versions of the php package thamtech/yii2-yaml. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package yii2-yaml
Yii2 Yaml
Yii2 Yaml provides a Yaml helper and a Yaml parser/dumper extension.
The Yaml helper provdesencode()
, decode()
and errorSummary()
similar to yii\helpers\Json.
Yii2 Yaml also includes an extension of the symfony/yaml library that supports custom yaml tags representing UnsetArrayValue and ReplaceArrayValue, as well as allowing you to attach events to handle any other custom yaml tags.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
or add
to the require
section of your composer.json
file.
Background
The goal of this Yii2 Yaml extension is to support Yaml in Yii2 the way support
for JSON is built-in. A Yaml
helper is introduced to match the API of Yii2's
built-in Json helper.
Furthermore, we have extended the symfony/yaml
library to support decoding
and encoding of ReplaceArrayValue
and UnsetArrayValue
objects for use with
Yii's ArrayHelper::merge()
method. You can use Yii2 event handlers to process custom tags as they
are parsed/decoded and pre-process objects before they are dumped/encoded.
Yii2 uses encode/decode terminology in the Json helper, while symfony/yaml
uses dump/parse terminology. Our helper is consistent with the Json helper
in using the encode/decode terminology.
Usage
Decoding/Parsing
Example Yaml:
Example decoding using the Yaml helper:
In the example above, you can see that the keys tagged with !yii/helpers/ReplaceArrayValue
and !yii/helpers/UnsetArrayValue
were automatically replaced with the helper objects
ready for use with the ArrayHelper::merge()
method.
In order to add your own handlers for tags like the
!lookupIdFromEmployeeNumber
tag, you can specify them in a thamtech\yaml\Parser
configuration array that you pass to the Yaml::decode()
method:
Encoding/Dumping
Example data to encode:
Dumping with the standard symfony/yaml
library:
Dumping with our default Yaml helper:
In the example above, you can see that the keys tagged with !yii/helpers/ReplaceArrayValue
and !yii/helpers/UnsetArrayValue
were automatically encoded from the ReplaceArrayValue
and UnsetArrayValue
objects.
In order to add your own handlers for values like the
EmployeeWithoutId
object, you can specify them in a thamtech\yaml\Dumper
configuration array that you pass to the Yaml::decode()
method:
Configuring Default Handlers
You may not want to have to send a configuration array with one or more
custom handlers every time you call Yaml::encode()
or Yaml::decode()
.
The Yaml
component sets its default Parser
and Dumper
definitions into
Yii::$container
. You can set your own default Parser
and Dumper
definitions there with your own handlers.
To not use any handlers by default (not even the Yaml
component's default
handlers):
However, it is probably more likely that you will want to start with the Yaml
component's default handlers and override them or add your own. The
Yaml::getDumperDefinition()
and Yaml::getParserDefinition()
methods are
a convenient way to get Parser and Dumper definitions ready for setting
in the Yii::$container
.
Yaml Response Formatter
You can add the YamlResponseFormatter
as a yaml
formatter in your
yii\web\Response
component to add support for returning yaml responses.
See Also
All versions of yii2-yaml with dependencies
yiisoft/yii2 Version >=2.0.14 <2.1
symfony/yaml Version >=3.4 <=3.4.29 || ^4.4.0 || ^5.0.0