PHP code example of dd / modxevo-library-ddtools

1. Go to this page and download the library: Download dd/modxevo-library-ddtools library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dd / modxevo-library-ddtools example snippets


//Include (MODX)EvolutionCMS.libraries.ddInstaller
nstaller/ler::install([
	'url' => 'https://github.com/DivanDesign/EvolutionCMS.libraries.ddTools',
	'type' => 'library',
]);

// Relative
$url = 'some/page?q=42#hash';
// Relative starting with slash
$url = '/some/page?q=42#hash';
// Absolute starting with domain
$url = 'example.com/some/page?q=42#hash';
// Absolute starting with double slash
$url = '//example.com/some/page?q=42#hash';
// Absolute starting with scheme
$url = 'https://example.com/some/page?q=42#hash';

\ddTools::convertUrlToAbsolute([
	'url' => $url,
	// The parameter is optional and is used here just for clarity. By default it will be equal to domain of your site.
	'host' => 'example.com',
]);

'https://example.com/some/page?q=42#hash'

// Include (MODX)EvolutionCMS.libraries.ddTools
ddTools/modx.ddtools.class.php'
);

// Backward compatibility
extract(\ddTools::verifyRenamedParams([
	// We called the method inside of a snippet, so its parameters are contained in the `$params` variable (MODX feature)
	'params' => $params,
	'compliance' => [
		// The new name => The old name
		'docField' => 'getEmail',
		'docId' => 'getId',
	],
]));

extract(\ddTools::verifyRenamedParams([
	// We called the method inside of a snippet, so its parameters are contained in the `$params` variable (MODX feature)
	'params' => $params,
	'compliance' => [
		// The new name => The old names
		'email_docField' => [
			'docField',
			'getEmail',
		],
		'email_docId' => [
			'docId',
			'getId',
		],
	],
	// Also you can prevent writing to the CMS event log if you want
	'writeToLog' => false,
]));

\ddTools::parseText([
	'text' => '
		<article>
			<h1>[+title+]</h1>
			[+text+]
			<p>[+authorFirstName+] [+authorLastName+], [+date+].</p>
		</article>
	',
	'data' => [
		'title' => 'Bethink Yourselves!',
		'text' => '<p>Question your loyalty to your country and government and strive for a more just and peaceful society.</p>',
		'authorFirstName' => 'Leo',
		'authorLastName' => 'Tolstoy',
		'date' => '1904',
	],
]);

\ddTools::parseText([
	// Data can have a complex nested structure
	'data' => [
		'title' => 'Bethink Yourselves!',
		'text' => '<p>Question your actions and consider the morality behind them.</p>',
		// Note that this is not a string, but that's okay
		'meta' => [
			// Moreover, any depth is supported
			// And objects are also supported as well as arrays regardless of nesting level
			'author' => (object) [
				'firstName' => 'Leo',
				'lastName' => 'Tolstoy',
			],
			'date' => '1904',
		],
	],
	// For nested data you can use placeholders like '[+meta.date+]' for getting a property
	// Or like '[+meta+]' to get whole object as JSON
	'text' => '
		<article data-meta=\'[+meta+]\'>
			<h1>[+title+]</h1>
			[+text+]
			<p>[+meta.author.firstName+] [+meta.author.lastName+], [+meta.date+].</p>
		</article>
	',
]);

// We can pass string in JSON format
\DDTools\Tools\Objects::convertType([
	'object' => '{
		"pagetitle": "Test title",
		"published": "0"
	}',
	'type' => 'objectArray',
]);

// Or Query string
\DDTools\Tools\Objects::convertType([
	'object' => 'pagetitle=Test title&published=0',
	'type' => 'objectArray',
]);

[
	'pagetitle' => 'Test title',
	'published' => '0',
];

\DDTools\Tools\Objects::convertType([
	'object' => 'firstName=Hans&lastName=Zimmer',
	'type' => 'stringJsonAuto',
]);

\DDTools\Tools\Objects::convertType([
	'object' => '{
		"firstName": "Ramin",
		"lastName": "Djawadi"
	}',
	'type' => 'stringJsonArray',
]);

\DDTools\Tools\Objects::convertType([
	'object' => "{
		// This is HJSON, not JSON, so we can use comments insides
		keys: and values can be specified without quotes,
		multilineValues:
			'''
			Write multiline strings with proper whitespace handling.
			Starts and ends with triple quotes.
			A simple syntax and easy to read.
			'''
	}",
	'type' => 'objectStdClass',
]);

stdClass::__set_state(array(
   'keys' => 'and values can be specified without quotes,',
   'multilineValues' => 'Write multiline strings with proper whitespace handling.
		Starts and ends with triple quotes.
		A simple syntax and easy to read.'
	,
))

\DDTools\Tools\Objects::convertType([
	'object' => [
		'data-name' => 'KINO',
		// Will be converted to 1
		'data-is-active' => true,
		// Will be converted to JSON array
		'data-members' => [
			'Viktor Tsoi',
			'Yuri Kasparyan',
			'Aleksei Rybin',
			'Igor Tikhomirov',
			'Aleksandr Titov',
			'Georgy Guryanov',
			'Oleg Valinsky',
		],
	],
	'type' => 'stringHtmlAttrs',
]);

var_export(
	\DDTools\Tools\Objects::extend([
		'objects' => [
			(object) [
				'cat' => 'mew',
				'dog' => (object) [
					'name' => 'Floyd',
					'weight' => 6,
				],
				'rabbit' => 42,
			],
			(object) [
				'dog' => (object) [
					'weight' => 10,
				],
				'bird' => 0,
			],
		],
	])
);

stdClass::__set_state(array(
	'cat' => 'mew',
	'dog' => stdClass::__set_state(array(
		'name' => 'Floyd',
		'weight' => 10,
	)),
	'rabbit' => 42,
	'bird' => 0,
))

var_export(
	\DDTools\Tools\Objects::extend([
		'objects' => [
			[
				'cat' => 'mew',
				'dog' => [
					'name' => 'Floyd',
					'weight' => 6,
				],
				'rabbit' => 42,
			],
			[
				'dog' => (object) [
					'weight' => 10,
				],
				'bird' => 0,
			],
		],
	])
);

array(
	'cat' => 'mew',
	'dog' => array(
		'name' => 'Floyd',
		'weight' => 10,
	),
	'rabbit' => 42,
	'bird' => 0,
)

var_export(
	\DDTools\Tools\Objects::extend([
		'objects' => [
			[
				'name' => 'jokes',
				'countries' => (object) [
					'usa' => 'democracy',
					'china' => 'chinese democracy',
				],
			],
			(object) [
				'countries' => [
					'china' => 'democracy too',
				],
			],
		],
	])
);

// The object expanded the source array
array(
	'name' => 'jokes',
	// The array expanded the source object
	'countries' => stdClass::__set_state(
		'usa' => 'democracy',
		'china' => 'democracy too',
	)),
)

var_export(
	\DDTools\Tools\Objects::extend([
		'objects' => [
			(object) [
				'firstName' => 'John',
				'lastName' => 'Tesla',
				'discipline' => 'Electrical engineering',
			],
			(object) [
				'firstName' => 'Nikola',
				'lastName' => '',
			],
		],
	])
);

stdClass::__set_state(array(
	'firstName' => 'Nikola',
	'lastName' => '',
	'discipline' => 'Electrical engineering',
))

var_export(
	\DDTools\Tools\Objects::extend([
		'objects' => [
			(object) [
				'firstName' => 'John',
				'lastName' => 'Tesla',
				'discipline' => 'Electrical engineering',
			],
			(object) [
				'firstName' => 'Nikola',
				'lastName' => '',
			],
		],
		'overwriteWithEmpty' => false,
	])
);

stdClass::__set_state(array(
	'firstName' => 'Nikola',
	'lastName' => 'Tesla',
	'discipline' => 'Electrical engineering',
))

var_export(
	\DDTools\Tools\Objects::extend([
		'objects' => [
			(object) [
				'name' => 'Classic Italian Pizza',
				'toppings' => (object) [
					'cheese' => 'mozzarella',
					'tomatoSauce' => true,
					'olive' => true,
				],
				'size' => 'medium',
			],
			[
				// Not interested in extra toppings
				'toppings' => [
					'pineapple' => true,
				],
				'size' => 'large',
				'price' => 15.99,
			],
		],
		// Only keeping the price and size
		'extendableProperties' => [
			'price',
			'size',
		],
	])
);

stdClass::__set_state(array(
	'name' => 'Classic Italian Pizza',
	'toppings' => stdClass::__set_state(array(
		'cheese' => 'mozzarella',
		'tomatoSauce' => true,
		'olive' => true,
	)),
	'size' => 'large',
	'price' => 15.99,
))

var_export(
	\DDTools\Tools\Objects::unfold([
		'object' => (object) [
			'name' => 'Elon Musk',
			'address' => (object) [
				'line1' => '3500 Deer Creek Road',
				'city' => 'Palo Alto',
				'state' => 'California',
				'country' => 'United States',
			],
		],
	])
);

stdClass::__set_state(array (
	'name' => 'Elon Musk',
	'address.line1' => '3500 Deer Creek Road',
	'address.city' => 'Palo Alto',
	'address.state' => 'California',
	'address.country' => 'United States',
))

var_export(
	\DDTools\Tools\Objects::unfold([
		'object' => [
			'a' => 'a val',
			'b' => [
				'b1' => 'b1 val',
				'b2' => [
					'b21' => 'b21 val',
					'b22' => 'b22 val',
				],
			],
			'c' => 'c val',
		],
	])
);

array (
	'a' => 'a val',
	'b.b1' => 'b1 val',
	'b.b2.b21' => 'b21 val',
	'b.b2.b22' => 'b22 val',
	'c' => 'c val',
)

var_export(
	\DDTools\Tools\Objects::unfold([
		'object' => [
			'name' => 'Elon Musk',
			'parents' => [
				'mother' => 'Maye Musk',
				'father' => 'Errol Musk',
			],
		],
		'keySeparator' => '_',
	])
);

stdClass::__set_state(array (
	'name' => 'Elon Musk',
	'parents_mother' => 'Maye Musk',
	'parents_father' => 'Errol Musk',
))

// Array
$data = [
	// Array
	'bin1' => [
		'plastic' => 'plastic bottles',
		'paper' => 'newspapers',
		'glass' => 'glass bottles',
	],
	// Object
	'bin2' => (object) [
		'organic' => 'food waste',
		'paper' => 'cardboard boxes',
		'metal' => 'aluminum cans',
	],
];

var_export(
	\DDTools\Tools\Objects::unfold([
		'object' => $data,
	])
);

array (
	'bin1.plastic' => 'plastic bottles',
	'bin1.paper' => 'newspapers',
	'bin1.glass' => 'glass bottles',
	'bin2' => (object) array(
		'organic' => 'food waste',
		'paper' => 'cardboard boxes',
		'metal' => 'aluminum cans',
	),
)

var_export(
	\DDTools\Tools\Objects::unfold([
		'object' => $data,
		'isCrossTypeEnabled' => true,
	])
);

array (
	'bin1.plastic' => 'plastic bottles',
	'bin1.paper' => 'newspapers',
	'bin1.glass' => 'glass bottles',
	'bin2.organic' => 'food waste',
	'bin2.paper' => 'cardboard boxes',
	'bin2.metal' => 'aluminum cans',
)

var_export(
	\DDTools\Tools\Objects::isPropExists([
		'object' => (object) [
			'firstName' => 'John',
			'lastName' => 'Lennon',
		],
		'propName' => 'firstName',
	])
);

var_export(
	\DDTools\Tools\Objects::isPropExists([
		'object' => [
			'firstName' => 'Paul',
			'lastName' => 'McCartney',
		],
		'propName' => 'firstName',
	])
);

var_export(
	\DDTools\Tools\Objects::getPropValue([
		'object' => (object) [
			'name' => 'Floyd',
			'weight' => 7,
		],
		'propName' => 'name',
	])
);

var_export(
	\DDTools\Tools\Objects::getPropValue([
		'object' => [
			'name' => 'Floyd',
			'weight' => 7,
		],
		'propName' => 'name',
	])
);

// For example let the first level be stdClass
$sourceObject = (object) [
	// Let the second level be an indexed array
	'PinkFloyd' => [
		// Let the third level be an associative array
		[
			'name' => 'Syd Barrett',
			'role' => 'lead and rhythm guitars, vocals',
		],
		[
			'name' => 'David Gilmour',
			'role' => 'lead and rhythm guitars, vocals, bass, keyboards, synthesisers',
		],
		// Let Roger be a little bit special ;)
		(object) [
			'name' => 'Roger Waters',
			'role' => 'bass, vocals, rhythm guitar, synthesisers',
		],
		[
			'name' => 'Richard Wright',
			'role' => 'keyboards, piano, organ, synthesisers, vocals',
		],
		[
			'name' => 'Nick Mason',
			'role' => 'drums, percussion',
		],
	],
];

var_export(
	\DDTools\Tools\Objects::getPropValue([
		'object' => $sourceObject,
		'propName' => 'PinkFloyd',
	])
);

array (
	0 => array (
		'name' => 'Syd Barrett',
		'role' => 'lead and rhythm guitars, vocals',
	),
	1 => array (
		'name' => 'David Gilmour',
		'role' => 'lead and rhythm guitars, vocals, bass, keyboards, synthesisers',
	),
	2 => stdClass::__set_state(array(
		 'name' => 'Roger Waters',
		 'role' => 'bass, vocals, rhythm guitar, synthesisers',
	)),
	3 => array (
		'name' => 'Richard Wright',
		'role' => 'keyboards, piano, organ, synthesisers, vocals',
	),
	4 => array (
		'name' => 'Nick Mason',
		'role' => 'drums, percussion',
	),
)

var_export(
	\DDTools\Tools\Objects::getPropValue([
		'object' => $sourceObject,
		'propName' => 'PinkFloyd.4',
	])
);

array (
	'name' => 'Nick Mason',
	'role' => 'drums, percussion',
)

var_export(
	\DDTools\Tools\Objects::getPropValue([
		'object' => $sourceObject,
		'propName' => 'PinkFloyd.2.name',
	])
);

'Roger Waters'

var_export(
	\DDTools\Tools\Objects::getPropValue([
		'object' => [
			'1973.03.01' => 'The Dark Side of the Moon',
			'1975.09.12' => 'Wish You Were Here',
		],
		'propName' => '1975.09.12',
	])
);

'Wish You Were Here'

$collection = new \DDTools\ObjectCollection([
	'items' => [
		[
			'name' => 'Mary Teresa',
			'isHuman' => 1,
			'gender' => 'female',
			'nobelPeacePrize' => 1,
			'religion' => 'Catholicism',
		],
		[
			'name' => 'Mahatma Gandhi',
			'isHuman' => 1,
			'gender' => 'male',
			'nobelPeacePrize' => 0,
		],
		[
			'name' => 'Tenzin Gyatso',
			'isHuman' => 1,
			'gender' => 'male',
			'nobelPeacePrize' => 1,
			'religion' => 'Tibetan Buddhism',
		],
		[
			'name' => 'ICAN',
			'isHuman' => 0,
			'nobelPeacePrize' => 1,
		],
	],
]);

$collection->setItems([
	'items' => '[
		{
			"name": "Mary Teresa",
			"isHuman": 1,
			"gender": "female",
			"nobelPeacePrize": 1,
			"religion": "Catholicism"
		},
		{
			"name": "Mahatma Gandhi",
			"isHuman": 1,
			"gender": "male",
			"nobelPeacePrize": 0
		},
		{
			"name": "Tenzin Gyatso",
			"isHuman": 1,
			"gender": "male",
			"nobelPeacePrize": 1,
			"religion": "Tibetan Buddhism"
		},
		{
			"name": "ICAN",
			"isHuman": 0,
			"nobelPeacePrize": 1
		}
	]'
]);

$collection->getItems([
	'filter' => 'religion',
]);

array(
	0 => array(
		'name' => 'Mary Teresa',
		'isHuman' => 1,
		'gender' => 'female',
		'nobelPeacePrize' => 1,
		'religion' => 'Catholicism',
	),
	1 => array(
		'name' => 'Tenzin Gyatso',
		'isHuman' => 1,
		'gender' => 'male',
		'nobelPeacePrize' => 1,
		'religion' => 'Tibetan Buddhism',
	),
)

$collection->getItems([
	'filter' => 'gender==male',
]);

array(
	0 => array(
		'name' => 'Mahatma Gandhi',
		'isHuman' => 1,
		'gender' => 'male',
		'nobelPeacePrize' => 0,
	),
	1 => array(
		'name' => 'Tenzin Gyatso',
		'isHuman' => 1,
		'gender' => 'male',
		'nobelPeacePrize' => 1,
		'religion' => 'Tibetan Buddhism',
	),
)

$collection->getItems([
	// Spaces, tabs and line breaks are also allowed and do not matter
	'filter' => '
		gender == female
		|| nobelPeacePrize == 1 && isHuman == 0
	'
]);

array(
	// gender == female
	0 => array(
		'name' => 'Mary Teresa',
		'isHuman' => 1,
		'gender' => 'female',
		'nobelPeacePrize' => 1,
		'religion' => 'Catholicism',
	),
	// nobelPeacePrize == 1 && isHuman == 0
	1 => array(
		'name' => 'ICAN',
		'isHuman' => 0,
		'nobelPeacePrize' => 1,
	),
)

$collection->getItems([
	'propAsResultKey' => 'name',
]);

array(
	'Mary Teresa' => array(
		'name' => 'Mary Teresa',
		'isHuman' => 1,
		'gender' => 'female',
		'nobelPeacePrize' => 1,
		'religion' => 'Catholicism',
	),
	'Mahatma Gandhi' => array(
		'name' => 'Mahatma Gandhi',
		'isHuman' => 1,
		'gender' => 'male',
		'nobelPeacePrize' => 0,
	),
	'Tenzin Gyatso' => array(
		'name' => 'Tenzin Gyatso',
		'isHuman' => 1,
		'gender' => 'male',
		'nobelPeacePrize' => 1,
		'religion' => 'Tibetan Buddhism',
	),
	'ICAN' => array(
		'name' => 'ICAN',
		'isHuman' => 0,
		'nobelPeacePrize' => 1,
	),
)

$collection->getItems([
	'propAsResultKey' => 'name',
	'propAsResultValue' => 'isHuman',
]);

array(
	'Mary Teresa' => 1,
	'Mahatma Gandhi' => 1,
	'Tenzin Gyatso' => 1,
	'ICAN' => 0,
)

$collection->getOneItem([
	'filter' => 'name == Mahatma Gandhi',
]);

array(
	'name' => 'Mahatma Gandhi',
	'isHuman' => 1,
	'gender' => 'male',
	'nobelPeacePrize' => 0,
)

$collection->getOneItem([
	'filter' => 'name == European Union',
	'notFoundResult' => [
		'name' => 'Default item',
		'nobelPeacePrize' => 0,
	],
]);

array(
	'name' => 'Default item',
	'nobelPeacePrize' => 0,
)

$collection->convertItemsType([
	'filter' => 'gender==male',
	'itemType' => 'objectStdClass',
]);

$collection->getItems();

array(
	0 => array(
		'name' => 'Mary Teresa',
		'isHuman' => 1,
		'gender' => 'female',
		'nobelPeacePrize' => 1,
		'religion' => 'Catholicism',
	),
	1 => stdClass::__set_state(array(
		'name' => 'Mahatma Gandhi',
		'isHuman' => 1,
		'gender' => 'male',
		'nobelPeacePrize' => 0,
	)),
	2 => stdClass::__set_state(array(
		'name' => 'Tenzin Gyatso',
		'isHuman' => 1,
		'gender' => 'male',
		'nobelPeacePrize' => 1,
		'religion' => 'Tibetan Buddhism',
	)),
	3 => array(
		'name' => 'ICAN',
		'isHuman' => 0,
		'nobelPeacePrize' => 1,
	),
)

$collection->updateItems([
	'filter' => 'name==Mahatma Gandhi',
	'data' => [
		'nobelPeacePrize' => 1,
		'birthday' => '2 October 1869',
	]
]);

$collection->getItems(
	'filter' => 'name==Mahatma Gandhi',
);

array(
	0 => stdClass::__set_state(array(
		// Existing properties that absent in `$params->data` have remained as is
		'name' => 'Mahatma Gandhi',
		'isHuman' => 1,
		'gender' => 'male',
		// Given property values have overwritten the existing ones
		'nobelPeacePrize' => 1,
		// Non-existing properties have been created
		'birthday' => '2 October 1869',
	))
)

$collection->updateItems([
	'filter' => 'isHuman==1',
	'limit' => 2,
]);

$collection->getItems();

array(
	// 2 humans have been deleted, 1 have remained
	0 => stdClass::__set_state(array(
		'name' => 'Tenzin Gyatso',
		'isHuman' => 1,
		'gender' => 'male',
		'nobelPeacePrize' => 1,
		'religion' => 'Tibetan Buddhism',
	)),
	1 => array(
		'name' => 'ICAN',
		'isHuman' => 0,
		'nobelPeacePrize' => 1,
	),
)
html
<article data-meta='{"author":{"firstName":"Leo","lastName":"Tolstoy"},"date":"1904"}'>
	<h1>Bethink Yourselves!</h1>
	<p>Question your actions and consider the morality behind them.</p>
	<p>Leo Tolstoy, 1904.</p>
</article>