dd/evolutioncms-snippets-ddobjecttools

Tools for modifying objects.


Keywords
json, objects, modx, MODX Evo, evo, modx evolution, evo cms, evolutioncms, evolution cms, divandesign, dd studio, dd group, ddobjecttools, object tools, merge json, merge objects, extend json, extend objects, extend arrays

Documentation

(MODX)EvolutionCMS.snippets.ddObjectTools

Tools for modifying objects.

Requires

Installation

Manually

1. Elements β†’ Snippets: Create a new snippet with the following data

  1. Snippet name: ddObjectTools.
  2. Description: <b>0.7</b> Tools for modifying objects..
  3. Category: Core.
  4. Parse DocBlock: no.
  5. Snippet code (php): Insert content of the ddObjectTools_snippet.php file from the archive.

2. Elements β†’ Manage Files

  1. Create a new folder assets/snippets/ddObjectTools/.
  2. Extract the archive to the folder (except ddObjectTools_snippet.php).

Using (MODX)EvolutionCMS.libraries.ddInstaller

Just run the following PHP code in your sources or Console:

//Include (MODX)EvolutionCMS.libraries.ddInstaller
require_once(
	$modx->getConfig('base_path') .
	'assets/libs/ddInstaller/require.php'
);

//Install (MODX)EvolutionCMS.snippets.ddObjectTools
\DDInstaller::install([
	'url' => 'https://github.com/DivanDesign/EvolutionCMS.snippets.ddObjectTools',
	'type' => 'snippet'
]);
  • If ddObjectTools is not exist on your site, ddInstaller will just install it.
  • If ddObjectTools is already exist on your site, ddInstaller will check it version and update it if needed.

Parameters description

  • sourceObject

    • Desctription: Source object or array.
    • Valid values:
      • stringJsonObject β€” as JSON
      • stringJsonArray β€” as JSON
      • stringHjsonObject β€” as HJSON
      • stringHjsonArray β€” as HJSON
      • stringQueryFormatted β€” as Query string
      • It can also be set as a native PHP object or array (e. g. for calls through $modx->runSnippet):
        • array
        • object
    • Default value: '{}'
  • extend

    • Desctription: Merge the contents of two or more objects / arrays together into sourceObject (it will receive the new properties).
    • Valid values:
      • stringJsonObject β€” as JSON
      • stringHjsonObject β€” as HJSON
      • stringQueryFormatted β€” as Query string
      • It can also be set as a native PHP object or array (e. g. for calls through $modx->runSnippet):
        • arrayAssociative
        • object
    • Default value: β€”
  • extend->objects

    • Desctription: Objects or arrays to merge. Moreover, objects can extend arrays and vice versa.
    • Valid values:
      • array
      • stringJsonArray β€” as JSON
      • stringHjsonArray β€” as HJSON
      • stringQueryFormatted β€” as Query string
    • Required
  • extend->objects[i]

    • Desctription: An object or array containing additional properties to merge in.
    • Valid values:
      • object
      • array
    • Required
  • extend->deep

    • Desctription: If true, the merge becomes recursive (aka β€œdeep copy”).
    • Valid values: boolean
    • Default value: true
  • extend->overwriteWithEmpty

    • Desctription: Overwrite fields with empty values.
      The following values are considered to be empty:
      • '' β€”Β an empty string
      • [] β€”Β an empty array
      • (object) [] β€”Β an empty object
      • NULL
    • Valid values: boolean
    • Default value: true
  • getPropValue

    • Desctription: Object property name or array index / key to return.
      You can also use '.' to get nested properties (see \DDTools\ObjectTools::getPropValue for more info).
    • Valid values: string
    • Default value: β€”
  • outputter

    • Desctription: Output format (when result is an object or array).
      Values are case insensitive (the following values are equal: 'stringjsonauto', 'stringJsonAuto', 'STRINGJSONAUTO', etc).
    • Valid values:
      • The snippet can return object as string:
        • 'stringJsonAuto' β€” stringJsonObject or stringJsonArray depends on result object
        • 'stringJsonObject'
        • 'stringJsonArray'
        • 'stringQueryFormatted' β€” Query string
      • The snippet can also return object as a native PHP object or array (it is convenient to call through \DDTools\Snippet).
        • 'objectAuto' β€”Β stdClass or array depends on result object
        • 'objectStdClass' β€” stdClass
        • 'objectArray' β€” array
    • Default value: 'stringJsonAuto'

Examples

Merge the contents of two or more objects together into the first object (the extend parameter)

[[ddObjectTools?
	&sourceObject=`{
		"cat": "mew",
		"dog": {
			"name": "Floyd",
			"weight": 6
		},
		"rabbit": 42
	}`
	&extend=`{
		"objects": [
			{
				"dog": {
					"weight": 10
				},
				"bird": 0
			}
		]
	}`
]]

Returns:

{
	"cat": "mew",
	"dog": {
		"name": "Floyd",
		"weight": 10,
	},
	"rabbit": 42,
	"bird": 0
}

Get an object property

[[ddObjectTools?
	&sourceObject=`{
		"firstName": "Chuck",
		"lastName": "Norris"
	}`
	&getPropValue=`firstName`
]]

Returns: Chuck.

Get an array element

[[ddObjectTools?
	&sourceObject=`[
		"Pink Floyd",
		"The Beatles",
		"Queen"
	]`
	&getPropValue=`2`
]]

Returns: Queen.

Convert a JSON object to an array

[[ddObjectTools?
	&sourceObject=`{
		"firstName": "Angus",
		"lastName": "Young"
	}`
	&outputter=`jsonArray`
]]

Returns:

[
	"Angus",
	"Young"
]

Run the snippet through \DDTools\Snippet::runSnippet without DB and eval

//Include (MODX)EvolutionCMS.libraries.ddTools
require_once(
	$modx->getConfig('base_path') .
	'assets/libs/ddTools/modx.ddtools.class.php'
);

//Run (MODX)EvolutionCMS.snippets.ddObjectTools
\DDTools\Snippet::runSnippet([
	'name' => 'ddObjectTools',
	'params' => [
		'sourceObject' => [
			'cat' => 'mew',
			'dog' => [
				'name' => 'Floyd',
				'weight' => 6
			],
			'rabbit' => 42
		],
		'extend' => [
			'objects' => [
				[
					'dog' => [
						'weight' => 11
					],
					'bird' => 0
				]
			]
		]
	]
]);

Links