PHP code example of dd / evolutioncms-libraries-ddtools
1. Go to this page and download the library: Download dd/evolutioncms-libraries-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 / evolutioncms-libraries-ddtools example snippets
// 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',
]);
\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',
]);
// 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' => [
'1973.03.01' => 'The Dark Side of the Moon',
'1975.09.12' => 'Wish You Were Here',
],
'propName' => '1975.09.12',
])
);
$collection->getItems([
// Spaces, tabs and line breaks are also allowed and do not matter
'filter' => '
gender == female
|| nobelPeacePrize == 1 && isHuman == 0
'
]);
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',
))
)
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>
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.