Download the PHP package dd/modxevo-library-ddtools without Composer
On this page you can find all versions of the php package dd/modxevo-library-ddtools. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dd/modxevo-library-ddtools
More information about dd/modxevo-library-ddtools
Files in dd/modxevo-library-ddtools
Package modxevo-library-ddtools
Short Description A library with various tools facilitating your work.
License MIT
Homepage https://code.divandesign.ru/modx/ddtools
Informations about the package modxevo-library-ddtools
(MODX)EvolutionCMS.libraries.ddTools
A library with various tools facilitating your work.
Requires
- PHP >= 7.4
- (MODX)EvolutionCMS >= 1.1
- PHP.libraries.HJSON 2.2 (included)
- PHP.libraries.phpThumb 1.7.19-202210110924 (included)
Installation
Manually
- Create a new folder
assets/libs/ddTools/. - Extract the archive to the folder.
Using Composer
Just add dd/evolutioncms-libraries-ddtools to your composer.json.
ddTools version must be 0.14 or higher to use this method. If you use it, the compatibility with all your snippets, modules, etc. that use ddTools versions under 0.14 will be maintained.
Update using (MODX)EvolutionCMS.libraries.ddInstaller
Just run the following PHP code in your sources or Console:
- If
ddToolsis already exist on your site,ddInstallerwill check it version and update it if needed. - If
ddToolsis not exist on your site,ddInstallercan't do anything because requires it for itself.
Parameters description
\ddTools::isEmpty($value)
Determines whether a variable is empty.
The following values are considered as empty:
- Empty objects (e. g.
new \stdClass()). -
Any values equal to
false(the same as$value == false). $value- Description: Value to be checked.
- Valid values:
mixed - Default value:
null
\ddTools::convertUrlToAbsolute($params)
Converts relative URLs to absolute.
The method tends to change URL as little as possible and just prepends required scheme and/or host (or sometimes nothing at all). All kinds of query parameters, hash, ports, etc. are not modified.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->url- Description: Source URL. Can be set as:
'some/url'— relative'/some/url'— relative starting with slash'example.com/some/url'— absolute starting with domain'//example.com/some/url'— absolute starting with double slash'https://example.com/some/url'— absolute starting with scheme
- Valid values:
string - Required
- Description: Source URL. Can be set as:
-
$params->host- Description: Host for the result URL.
- Valid values:
string - Default value:
$_SERVER['HTTP_HOST']
$params->scheme- Description: Scheme for the result URL.
- Valid values:
string - Default value:
'https'or'http'depending on$_SERVER['HTTPS']
Returns
$result- Description: Source URL converted to absolute. Always contains scheme.
- Valid values:
string
\ddTools::getTpl($tpl = '')
The same as $modx->getTpl with some differences:
- This method always returns
stringregardless of the parameter type. For example,$modx->getTpl(null)returnsnull, this method returns''. - The parameter is optional.
$modx->getTpl()throws an error, this method jsut returns''. -
$modx->getTpl('@CODE:')returns'@CODE:', this method returns''. $tpl- Description: Chunk name or inline template.
- Valid values:
stringChunkNamestring— use inline templates starting with@CODE:
- Default value:
''
Returns
$result- Description: Required template.
- Valid values:
string
\ddTools::parseText($params)
Replaces placeholders in a text with required values.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->text- Description: String to parse.
- Valid values:
string - Required
-
$params->data- Description:
The array of additional data has to be replaced in
$params->text.
Nested objects and arrays are supported too:{"someOne": "1", "someTwo": "test" }=>[+someOne+], [+someTwo+].{"some": {"a": "one", "b": "two"} }=>[+some.a+],[+some.b+].{"some": ["one", "two"] }=>[+some.0+],[+some.1+].
- Valid values:
arrayAssociativestdClassstringJsonObject— as JSONstringHjsonObject— as HJSONstringQueryFormatted— as Query string
- Default value:
[]
- Description:
The array of additional data has to be replaced in
-
$params->data->{$key}- Description: Key is placeholder name, value is value.
- Valid values:
stringboolean— will be converted to'1'or'0'respectivelyarray— will be unfolded and also will be converted to a JSON stringobject— will be unfolded and also will be converted to a JSON string
- Required
-
$params->placeholderPrefix- Description: Placeholders prefix.
- Valid values:
string - Default value:
'[+'
-
$params->placeholderSuffix- Description: Placeholders suffix.
- Valid values:
string - Default value:
'+]'
-
$params->removeEmptyPlaceholders- Description: Do you need to remove empty placeholders?
- Valid values:
boolean - Default value:
false
$params->isCompletelyParsingEnabled- Description: Additional parsing of document fields, settings, chunks, snippets, URLs — everything.
- Valid values:
boolean - Default value:
true
Returns
$result- Description: Parsed text.
- Valid values:
string
\ddTools::verifyRenamedParams($params)
The method checks an array for deprecated parameters and writes warning messages into the MODX event log.
It returns an associative array, in which the correct parameter names are the keys and the parameter values are the values.
You can use the exctract function to turn the array into variables of the current symbol table.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->params- Description: The associative array of the parameters of a snippet, in which the parameter names are the keys and the parameter values are the values.
You can directly pass here the$paramsvariable if you call the method inside of a snippet. - Valid values:
stdClassarrayAssociative
- Required
- Description: The associative array of the parameters of a snippet, in which the parameter names are the keys and the parameter values are the values.
-
$params->compliance- Description: An array (or object) of correspondence between new parameter names and old ones, in which the new names are the keys and the old names are the values.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->compliance->{$newName}- Description: The old name(s). Use a string for a single name or an array for multiple.
- Valid values:
stringarray
- Required
-
$params->compliance->{$newName}[i]- Description: One of the old names.
- Valid values:
string - Required
-
$params->returnCorrectedOnly- Description: Need to return only corrected parameters?
- Valid values:
boolean - Default value:
true
$params->writeToLog- Description: Write a warning message about deprecated parameters to the CMS event log.
- Valid values:
boolean - Default value:
true
Returns
-
$result- Description: An array or object, in which the correct parameter names are the keys and the parameter values are the values.
Can contains all parameters or only corrected (see$params->returnCorrectedOnly). - Valid values:
arrayAssociative— if$params->paramsset as an arraystdClass— if$params->paramsset as an object
- Description: An array or object, in which the correct parameter names are the keys and the parameter values are the values.
$result[$newName]- Description: A parameter value, in which the correct parameter name is the key and the parameter value is the value.
- Valid values:
mixed
\ddTools::getDocumentIdByUrl($url)
Gets ID of a document by its URL.
$url- Description: Document URL or relative path.
- Valid values:
stringUrl- Relative path:
info/about - Absolute URL:
https://example.com/info/about - IDNA ASCII-compatible domains in absolute URLs are also supported.
- Relative path:
- Required
Returns
$result- Description: Document ID, or
0if the document was not found. - Valid values:
integer
- Description: Document ID, or
\ddTools::getDocumentUrlById($docId)
Gets relative URL path of a document by its ID.
$docId- Description: Document ID.
- Valid values:
integer - Required
Returns
$result- Description: Relative path without leading or trailing slashes.
- Valid values:
stringUrlRelative''— empty string for site start or invalid ID (not found inaliasListing)
\DDTools\Tools\Files
\DDTools\Tools\Files::modifyImage($params)
Modify your images: create thumbnails, crop, resize, fill background color or add watermark.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->sourceFullPathName- Description: Full file path of source image.
You can pass a relative path too (e. g.assets/images/some.jpg), the method will automatically addbase_pathif needed. - Valid values:
string - Required
- Description: Full file path of source image.
-
$params->outputFullPathName- Description: Full file path of result image.
- You can pass a relative path too (e. g.
assets/images/some.jpg), the method will automatically addbase_pathif needed. - The original image will be overwritten if this parameter is omitted.
- You can pass a relative path too (e. g.
- Valid values:
string - Default value: ==
$params->sourceFullPathName
- Description: Full file path of result image.
-
$params->transformMode- Description: Transform mode.
- Valid values:
'resize'— resize only, the image will be inscribed into the specified sizes with the same proportions'crop'— crop only'resizeAndCrop'— resize small side then crop big side to the specified value'resizeAndFill'— inscribe image into the specified sizes and fill empty space with the specified background (see$params->backgroundColor)
- Default value:
'resize'
-
$params->width- Description: Result image width.
In pair width / height only one is required, omitted size will be calculated according to the image proportions. - Valid values:
integer - Required
- Description: Result image width.
-
$params->height- Description: Result image height.
In pair width / height only one is required, omitted size will be calculated according to the image proportions. - Valid values:
integer - Required
- Description: Result image height.
-
$params->allowEnlargement- Description: Allow image enlargement when resizing.
- Valid values:
boolean - Default value:
false
-
$params->backgroundColor- Description: Result image background color in HEX (used only for
$params->transformMode=='resizeAndFill'). - Valid values:
string - Default value:
FFFFFF
- Description: Result image background color in HEX (used only for
-
$params->allowEnlargement- Description: Allow image enlargement when resizing.
- Valid values:
boolean - Default value:
false
-
$params->quality- Description: JPEG compression level.
- Valid values:
integer - Default value:
100
$params->watermarkImageFullPathName- Description: Specify if you want to overlay your image with watermark.
You can pass a relative path too (e. g.assets/images/some.jpg), the method will automatically addbase_pathif needed. - Valid values:
string - Default value: —
- Description: Specify if you want to overlay your image with watermark.
\DDTools\Tools\Objects
\DDTools\Tools\Objects::isPropExists($params)
Checks if the object, class or array has a property / element. This is a “syntactic sugar” for checking an element in one way regardless of the “object” type.
The first reason for creating this method is convenience to not thinking about type of “object” variables. Second, the different order of parameters in the native PHP functions makes us crazy.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->object- Description: Source object or array.
- Valid values:
stdClassarray
- Required
$params->propName- Description: Object property name or array key.
- Valid values:
stringinteger
- Required
\DDTools\Tools\Objects::getPropValue($params)
Get the value of an object property or an array element in any nesting level in one way regardless of the “object” type.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->object- Description: Source object or array.
It can be nested, and elements of all levels can be mix of objects and arrays (see Examples below). - Valid values:
stdClassarray
- Required
- Description: Source object or array.
-
$params->propName- Description: Object property name or array key.
You can also use'.'to get nested properties. Several examples (see also full Examples below):somePlainProp— get first-level propertysomeObjectProp.secondLevelProp— get property of thesomeObjectPropobject|arraysomeArrayProp.0.thirdLevelProp— get property of the zero element of thesomeArrayProparray
- Valid values:
stringinteger
- Required
- Description: Object property name or array key.
$params->notFoundResult- Description: What will be returned when property is not found.
- Valid values:
mixed - Default value:
null
Returns
$result- Description: Value of an object property or an array element.
- Valid values:
mixed$params->notFoundResult— if property not exists
\DDTools\Tools\Objects::convertType($params)
Converts an object type. Arrays, JSON and Query string objects are also supported.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->object- Description: Input object | array | encoded string.
- Valid values:
stdClassobject— custom class instances are also supportedarraystringJsonObject— JSON objectstringJsonArray— JSON arraystringHjsonObject— HJSON objectstringHjsonArray— HJSON arraystringQueryFormatted— Query string
- Required
$params->type- Description: Type of resulting object.
Values are case insensitive (the following names are equal:'stringjsonauto','stringJsonAuto','STRINGJSONAUTO', etc). - Valid values:
'objectAuto'—stdClassorarraydepends on input object'objectStdClass'—stdClass'objectArray'—array'stringJsonAuto'—stringJsonObjectorstringJsonArraydepends on input object'stringJsonObject''stringJsonArray''stringQueryFormatted''stringHtmlAttrs'— HTML attributes string (e. g.width='100' height='50'), boolean values will be converted to0or1(e. g.data-is-loaded='1'), objects/arrays will be converted to JSON string (e. g.data-user-data='{"firstName": "Elon", "lastName": "Musk"}')
- Default value:
'objectAuto'
- Description: Type of resulting object.
Returns
$result- Description: Result type depends on
$params->type. - Valid values:
stdClassarraystringJsonObjectstringJsonArray
- Description: Result type depends on
\DDTools\Tools\Objects::extend($params)
Merge the contents of two or more objects or arrays together into the first one.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->objects- Description: Objects or arrays to merge. Moreover, objects can extend arrays and vice versa.
- Valid values:
array - Required
-
$params->objects[0]- Description: The object or array to extend. It will receive the new properties.
- Valid values:
objectarraymixed— if passed something else, the newstdClassobject will be created instead
- Required
-
$params->objects[i]- Description: An object or array containing additional properties to merge in.
- Valid values:
objectarray
- Required
-
$params->deep- Description: If true, the merge becomes recursive (aka. deep copy).
- Valid values:
boolean - Default value:
true
-
$params->overwriteWithEmpty- Description: Overwrite fields with empty values (see examples below).
The following values are considered to be empty:''— an empty string[]— an empty array(object) []— an empty objectNULL
- Valid values:
boolean - Default value:
true
- Description: Overwrite fields with empty values (see examples below).
-
$params->extendableProperties- Description: An array of property names that can be extended from additional objects or arrays. Properties in the initial object or array are not restricted by this parameter.
- Valid values:
arraynullor any empty value — all properties will be extended
- Default value:
null
$params->extendableProperties[$i]- Description: The name of a property that is allowed to be extended from additional objects or arrays.
- Valid values:
string - Required
\DDTools\Tools\Objects::unfold($params)
Converts a multidimensional array/object into an one-dimensional one joining the keys with $params->keySeparator.
For example, it can be helpful while using placeholders like [+size.width+].
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->object- Description: An object/array to convert.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->keySeparator- Description: Separator between nested keys in the result object/array.
- Valid values:
string - Default value:
'.'
-
$params->keyPrefix- Description: Prefix of the keys of an object/array (it's an internal varible, but can be used if required).
- Valid values:
string - Default value:
''
$params->isCrossTypeEnabled- Description: This parameter determines whether the method should process elements across different data types, such as arrays and objects, at all levels. When set to
true, the method will recursively unfold elements of both array and object types, regardless of the type of the root parent. - Valid values:
boolean - Default value:
false
- Description: This parameter determines whether the method should process elements across different data types, such as arrays and objects, at all levels. When set to
Returns
$result- Description: Unfolded object/array. Type of results depends on
$params->object. - Valid values:
stdClassarray
- Description: Unfolded object/array. Type of results depends on
\DDTools\Tools\Cache
You can cache some data (e. g. a snippet result).
- There are 2 levels of caching: stable (file-based) and quick (
$_SESSION-based). All methods utilize both levels automatically. - The name of each cache item is
[+prefix+]-[+resourceId+]-[+suffix+]. - Each cache item can contain a
string,arrayorstdClass. - All cache files are stored in the
assets/cache/ddCachefolder. - The name of each cache file is
[+cacheName+].php. - Quick cache items are stored in
$_SESSION['ddCache'].
\DDTools\Tools\Cache::save($params)
Saves custom data to cache storage.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->data- Description: Data to save.
- Valid values:
stringarraystdClass
- Required
-
$params->resourceId- Description: Resource ID related to cache (e. g. document ID).
- Valid values:
string - Required
-
$params->suffix- Description: Cache suffix. You can use several suffixes with the same
$params->resourceIdto cache some parts within a resource. - Valid values:
string - Required
- Description: Cache suffix. You can use several suffixes with the same
-
$params->prefix- Description: Cache prefix. Useful if you want to cache some custom data that is not related to any documents.
- Valid values:
string - Default value:
'doc'
$params->isExtendEnabled- Description: Should existing data be extended by
$params->dataor overwritten? - Valid values:
boolean - Default value:
false
- Description: Should existing data be extended by
\DDTools\Tools\Cache::saveSeveral($params)
Saves data of several items to cache storage.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->items- Description: Items to save.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->items->{$resourceId}- Description: Item data to save. Key is resource ID related to cache (e. g. document ID).
- Valid values:
stringarraystdClass
- Required
-
$params->suffix- Description: Cache suffix.
- Valid values:
string - Required
-
$params->prefix- Description: Cache prefix.
- Valid values:
string - Default value:
'doc'
$params->isExtendEnabled- Description: Should existing items data be extended by
$params->itemsor overwritten? - Valid values:
boolean - Default value:
false
- Description: Should existing items data be extended by
\DDTools\Tools\Cache::get($params)
Retrieves item data from cache storage.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->resourceId- Description: Resource ID related to cache (e. g. document ID).
- Valid values:
string - Required
-
$params->suffix- Description: Cache suffix.
- Valid values:
string - Required
$params->prefix- Description: Cache prefix.
- Valid values:
string - Default value:
'doc'
Returns
$result- Description: Cached data.
- Valid values:
- Type of returned data depends on type of saved data:
stringarraystdClass
null— means that the cache item does not exist
- Type of returned data depends on type of saved data:
\DDTools\Tools\Cache::getSeveral($params)
Retrieves data of several items from cache storage.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->resourceId- Description: Resource ID(s) related to cache (e. g. document ID).
- Valid values:
string'*'— means any IDarray— you can specify multiple IDs
- Required
-
$params->resourceId[$i]- Description: A resource ID.
- Valid values:
string - Required
-
$params->suffix- Description: Cache suffix.
- Valid values:
string - Required
$params->prefix- Description: Cache prefix.
- Valid values:
string - Default value:
'doc'
Returns
-
$result- Description: Cached items data.
- Valid values:
stdClassnull— means that the cache of specified items does not exist
$result->{$resourceName}- Description: Cached item data.
- A key is an item's cache name (
[+prefix+]-[+resourceId+]-[+suffix+]), a value is a data. - Type of returned data depends on type of saved data.
- A key is an item's cache name (
- Valid values:
stringarraystdClass
- Description: Cached item data.
\DDTools\Tools\Cache::delete($params)
Deletes one or more cache items.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Default value: —
-
$params->resourceId- Description: Resource ID(s) related to cache (e. g. document ID).
- Valid values:
string'*'— means any IDarray— you can specify multiple IDsnull— cache of all resources will be cleared independent of$params->prefix
- Default value:
null
-
$params->resourceId[$i]- Description: A resource ID.
- Valid values:
string - Default value: —
-
$params->prefix- Description: Cache prefix. Useful if you want to cache some custom data that is not related to any documents.
- Valid values:
string'*'— means any prefix
- Default value:
'doc'
$params->suffix- Description: Cache suffix.
- Valid values:
string'*'— means any suffix
- Default value:
'*'
\DDTools\ObjectCollection
Class representing a collection of some objects or arrays.
\DDTools\ObjectCollection::__construct($params)
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Default value: —
-
$params->items- Description: An array of items.
You can avoid this parameter to create an empty collection and set items later. - Valid values:
array— can be indexed or associative, keys will not be usedobject— also can be set as an object for better convenience, only property values will be usedstringJsonObject— JSON objectstringJsonArray— JSON arraystringHjsonObject— HJSON objectstringHjsonArray— HJSON arraystringQueryFormatted— Query string
- Default value: —
- Description: An array of items.
-
$params->items[$itemIndex]- Description: An item.
- Valid values:
array— indexed arrays are supported as well as associativeobject
- Required
$params->itemType- Description: Allows to convert item type. If set, each item of
$params->itemswill be converted to needed type.
Values are case insensitive (the following names are equal:'objectstdclass','objectStdClass','OBJECTSTDCLASS', etc). - Valid values:
'objectStdClass''objectArray'null— do not convert type of items, use them as is
- Default value:
null
- Description: Allows to convert item type. If set, each item of
\DDTools\ObjectCollection::setItems($params)
Sets new collection items. Existing items will be removed.
Has the same parameters as \DDTools\ObjectCollection::__construct($params).
\DDTools\ObjectCollection::addItems($params)
Appends items onto the end of collection.
Has the same parameters as \DDTools\ObjectCollection::__construct($params).
\DDTools\ObjectCollection::getItems($params)
Gets an array of required collection items.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Default value: —
-
$params->filter-
Description: Filter clause for item properties.
-
Thus,
returns:
- All items with the
genderproperty equal to'female'. - All items:
- with the
genderproperty equal to'male'and - with the
firstNameproperty not equal to'Bill'and - with the
lastNameproperty is exist with any value.
- with the
- All items with the
-
Quoted property names and values are optional, this is valid too:
-
Single quotes are also supported as double quotes:
- Spaces, tabs and line breaks are optional, this is valid too:
gender==female||gender==male&&firstName!=Bill&&lastName.
-
- Valid values:
stringSeparated - Default value:
''(without filtration)
-
-
$params->limit- Description: Maximum number of items to return.
- Valid values:
integer0— all matching items
- Default value:
0
-
$params->propAsResultKey- Description: Item property, which value will be an item key in result array instead of an item index.
For example, it can be useful if items have an ID property or something like that. - Valid values:
stringnull— result array will be indexed
- Default value:
null
- Description: Item property, which value will be an item key in result array instead of an item index.
$params->propAsResultValue- Description: Item property, which value will be an item value in result array instead of an item object.
- Valid values:
stringnull— result array values will item objects
- Default value:
null
Returns
-
$result- Description: An array of items.
- Valid values:
arrayIndexedarrayAssociative— item property values will be used as result keys if$params->propAsResultKeyis set
$result[$itemIndex|$itemFieldValue]- Description: An item object or item property value if specified in
$params->propAsResultValue. - Valid values:
mixed
- Description: An item object or item property value if specified in
\DDTools\ObjectCollection::getOneItem($params)
Gets required item.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Default value: —
-
$params->filter- Description: Filter clause for item properties. The same parameter as
\DDTools\ObjectCollection::getItems($params). - Valid values:
stringSeparated - Default value:
''(first found item will be returned)
- Description: Filter clause for item properties. The same parameter as
$params->notFoundResult- Description: What will be returned when no items found.
- Valid values:
mixed - Default value:
null
\DDTools\ObjectCollection::count()
Counts all items.
\DDTools\ObjectCollection::convertItemsType($params)
Converts type of needed items in collection.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Default value: —
-
$params->itemType- Description: Result item type.
Values are case insensitive (the following names are equal:'objectstdclass','objectStdClass','OBJECTSTDCLASS', etc). - Valid values:
'objectStdClass''objectArray'
- Required
- Description: Result item type.
$params->filter- Description: Filter clause for item properties. The same parameter as
\DDTools\ObjectCollection::getItems($params). - Valid values:
stringSeparated - Default value:
''(all items will be converted)
- Description: Filter clause for item properties. The same parameter as
\DDTools\ObjectCollection::updateItems($params)
Undates properties of existing items with new values.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->data- Description: New item data. Existing item will be extended by this data, it means:
- Type of existing item will not be changed.
- All given property values will overwrite existing.
- Non-existing given properties will be created.
- Existing properties that absent in
$params->datawill remain as is.
- Valid values:
arrayobject
- Required
- Description: New item data. Existing item will be extended by this data, it means:
-
$params->filter- Description: Filter clause for item properties. The same parameter as
\DDTools\ObjectCollection::getItems($params). - Valid values:
stringSeparated - Default value:
''(any items will be updated)
- Description: Filter clause for item properties. The same parameter as
$params->limit- Description: Maximum number of items can be updated.
- Valid values:
integer0— all matching items
- Default value:
0
\DDTools\ObjectCollection::deleteItems($params)
Deletes required items from collection.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Default value: —
-
$params->filter- Description: Filter clause for item properties. The same parameter as
\DDTools\ObjectCollection::getItems($params). - Valid values:
stringSeparated - Default value:
''(any items will be deleted)
- Description: Filter clause for item properties. The same parameter as
$params->limit- Description: Maximum number of items can be deleted.
- Valid values:
integer0— all matching items
- Default value:
0
\DDTools\ObjectCollection::toJSON(), \DDTools\ObjectCollection::__toString()
Gets an JSON-array of all collection items.
Returns
$result- Description: An JSON-array of items.
- Valid values:
stringJsonArray
\DDTools\ObjectCollection::setOneItemData (protected)
Sets data of an item object. All setting of an item data inside the class must be use this method. It's convenient to override this method in child classes if items are not plain objects.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->index- Description: Item index which data will be set.
- Valid values:
integer - Required
$params->data- Description: New item data.
- Valid values:
array— indexed arrays are supported as well as associativeobject
- Required
\DDTools\ObjectCollection::getOneItemData (protected)
Returns data of an item object. All getting of an item data inside the class must use this method. It's convenient to override this method in child classes if items are not plain objects.
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
$params->itemObject- Description: An item object which data will be returned.
- Valid values:
array— indexed arrays are supported as well as associativeobject
- Required
Returns
$result- Description: Data of an item object.
- Valid values:
arrayobject
\DDTools\Base\Base
Simple abstract class with some small methods facilitating your work. It is convenient to inherit your classes from this.
You can see an example of how it works in the (MODX)EvolutionCMS.snippets.ddGetDocumentField code.
\DDTools\Base\Base::getClassName($classNameFull = null)
Gets data about a class name.
Parameters
$classNameFull- Description: Full class name including namespace.
- Valid values:
stringnull— If not specified, will be usedget_called_class()
- Default value:
null
Returns
-
$result- Description: Class name data.
- Valid values:
stdClass
-
$result->full- Description: Full class name including namespace, e. g.:
'\\ddSendFeedback\\Sender\\Email\\Sender'. - Valid values:
string
- Description: Full class name including namespace, e. g.:
-
$result->nameShort- Description: Short class name, e. g.:
'Sender'. - Valid values:
string
- Description: Short class name, e. g.:
-
$result->namespaceFull- Description: Namespace, e. g.:
'\\ddSendFeedback\\Sender\\Email'. - Valid values:
string
- Description: Namespace, e. g.:
-
$result->namespaceShort- Description: Last namespace item, e. g.:
'Email'. - Valid values:
string
- Description: Last namespace item, e. g.:
-
$result->namespacePrefix- Description: Namespace prefix, e. g.:
'\\ddSendFeedback\\Sender'. - Valid values:
string
- Description: Namespace prefix, e. g.:
$result->namespacePrefixRoot- Description: Root namespace prefix, e. g.:
'\\ddSendFeedback'. - Valid values:
string
- Description: Root namespace prefix, e. g.:
\DDTools\Base\Base::setExistingProps($props)
Sets existing object properties.
-
$props- Description: The object properties.
- The method sets all existing properties: public, private or protected — it doesn't matter, exactly what you pass will be set.
- No problem if If some properties are not exist, the method just skip them without errors.
- Valid values:
arrayAssociativeobject- It can also be set as an object-like string:
stringJsonObject— as JSONstringHjsonObject— as HJSONstringQueryFormatted— as Query string
- Required
- Description: The object properties.
$props->{$propName}- Description: Key is the property name, value is the property value.
- Valid values:
mixed - Required
\DDTools\Base\Base::toArray()
Returns all properties of this object as an associative array independent of their visibility.
Returns
-
$result- Description: An associative array representation of this object.
The method returns all existing properties: public, private and protected. - Valid values:
arrayAssociative
- Description: An associative array representation of this object.
$result[$propName]- Description: The key is the object field name and the value is the object field value.
- Valid values:
mixed
\DDTools\Base\Base::toJSON()
Returns all properties of this object as an JSON string independent of their visibility.
Returns
-
$result- Description: An JSON string representation of this object.
The method returns all existing properties: public, private and protected. - Valid values:
stringJsonObjectstringJsonArray— if$this->toArrayreturns indexed array
- Description: An JSON string representation of this object.
$result->{$propName}- Description: The key is the object field name and the value is the object field value.
- Valid values:
mixed
\DDTools\Base\Base::__toString()
The same as \DDTools\Base\Base::toJSON().
\DDTools\Base\AncestorTrait
Simple trait for ancestors with some small methods facilitating your work.
You can see an example of how it works in the (MODX)EvolutionCMS.snippets.ddGetDocumentField code.
\DDTools\Base\AncestorTrait::createChildInstance($params)
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->name- Description: Short name of child class.
- Valid values:
string - Required
-
$params->params- Description: Params to be passed to object constructor.
- Valid values:
stdClassarrayAssociative
- Default value:
[]
-
$params->parentDir- Description: Directory of the parent file (e. g.
__DIR__). - Valid values:
string - Default value: — (dirname of a class that uses this trait)
- Description: Directory of the parent file (e. g.
$params->capitalizeName- Description: Need to capitalize child name?
- Valid values:
boolean - Default value:
true
Returns
$result- Description: The new object instance.
- Valid values:
object
\DDTools\Base\AncestorTrait::getChildClassName($params)
-
$params- Description: The parameters object.
- Valid values:
stdClassarrayAssociative
- Required
-
$params->name- Description: Short name of child class.
- Valid values:
string - Required
-
$params->parentDir- Description: Directory of the parent file (e. g.
__DIR__). - Valid values:
string - Default value: — (dirname of a class that uses this trait)
- Description: Directory of the parent file (e. g.
$params->capitalizeName- Description: Need to capitalize child name?
- Valid values:
boolean - Default value:
true
Returns
$result- Description: Child class name.
- Valid values:
string
\DDTools\Snippet
Abstract class for snippets.
Properties
-
\DDTools\Snippet::$name- Description: Snippet name (e. g.
ddGetDocuments).
Will be set from namespace in\DDTools\Snippet::__construct($params).
You can use it inside child classes:$this->name. - Valid values:
string - Visibility:
protected
- Description: Snippet name (e. g.
-
\DDTools\Snippet::$version- Description: Snippet version.
You must define it in your child class declaration. - Valid values:
string - Visibility:
protected
- Description: Snippet version.
-
\DDTools\Snippet::$paths- Description: Snippet paths.
Will be set in\DDTools\Snippet::__construct($params). - Valid values:
stdClass - Visibility:
protected
- Description: Snippet paths.
-
\DDTools\Snippet::$paths->snippet- Description: Full path to the snippet folder.
- Valid values:
string
-
\DDTools\Snippet::$paths->src- Description: Ful path to the
srcfolder. - Valid values:
string
- Description: Ful path to the
-
\DDTools\Snippet::$params- Description: Snippet params.
Will be set in\DDTools\Snippet::__construct($params).
You can define default values of parameters as associative array in this field of your child class (e. g.protected $params = ['someParameter' => 'valueByDefault'];);. - Valid values:
stdClass - Visibility:
protected
- Description: Snippet params.
-
\DDTools\Snippet::$params->{$paramName}- Description: Key is parameter name, value is value.
- Valid values:
mixed
-
\DDTools\Snippet::$paramsTypes- Description: Overwrite in child classes if you want to convert some parameters types.
Parameters types will be converted respectively with this field in\DDTools\Snippet::prepareParams. - Valid values:
arrayAssociative - Visibility:
protected
- Description: Overwrite in child classes if you want to convert some parameters types.
-
\DDTools\Snippet::$paramsTypes[$paramName]- Description: The parameter type.
Values are case insensitive (the following names are equal:'stringjsonauto','stringJsonAuto','STRINGJSONAUTO', etc). - Valid values:
'integer''float''boolean''objectAuto''objectStdClass''objectArray''stringJsonAuto''stringJsonObject''stringJsonArray'
- Visibility:
protected
- Description: The parameter type.
\DDTools\Snippet::$renamedParamsCompliance- Description: Overwrite in child classes if you want to rename some parameters with backward compatibility (see
$params->complianceof\ddTools::verifyRenamedParams). - Valid values:
arrayAssociative - Visibility:
protected
- Description: Overwrite in child classes if you want to rename some parameters with backward compatibility (see
\DDTools\Snippet::__construct($params)
-
$params- Description: Snippet parameters, the pass-by-name style is used.
- Valid values:
stdClassarrayAssociativestringJsonObjectstringQueryFormatted
- Default value:
[]
$params->{$paramName}- Description: Key is parameter name, value is value.
- Valid values:
mixed - Required
\DDTools\Snippet::run()
Abstract method for main snippet action.
You must define it in your child class declaration.
\DDTools\Snippet::runSnippet($params)
Static method for easy running needed snippet using only it's name and parameters (if needed).
-
$params- Description: Snippet parameters, the pass-by-name style is used.
- Valid values:
stdClassarrayAssociativestringJsonObjectstringQueryFormatted
- Required
-
$params->name- Description: The name of the snippet you want to run (e. g.
ddGetDocuments). - Valid values:
string - Required
- Description: The name of the snippet you want to run (e. g.
-
$params->params- Description: Parameters that will be passed to the snippet constructor.
- Valid values:
stdClassarrayAssociativestringJsonObjectstringQueryFormatted
- Default value: —
$params->params->{$paramName}- Description: Key is parameter name, value is value.
- Valid values:
mixed - Required
Examples
\ddTools::convertUrlToAbsolute($params): Convert relative URLs to absolute
$params->url can be set in various ways for more convenience:
Returns this with any of the above URLs:
Verify renamed snippet params (\ddTools::verifyRenamedParams($params))
Suppose we have the snippet ddSendFeedback with the getEmail and getId parameters.
Over time, we decided to rename the parameters as docField and docId respectively (as it happened in version 1.9).
And we want to save backward compatibility, the snippet must work with the old names and send message to the MODX event log.
Below we can use $docField and $docId and not to worry. The method will check everything and will send a message to the MODX event log.
After some time we decided to rename the parameters again as email_docField и email_docId. Nothing wrong, the method can works with multiple old names, just pass an array:
Document URL and ID (\ddTools::getDocumentIdByUrl, \ddTools::getDocumentUrlById)
Resolve document ID ↔ relative URL path.
Suppose document 8 is available at info/about on the site:
URL → ID (\ddTools::getDocumentIdByUrl)
ID → URL path (\ddTools::getDocumentUrlById)
\ddTools::parseText($params)
Basic example
Returns:
Nested objects in $params->data
Returns:
\DDTools\Tools\Objects
\DDTools\Tools\Objects::convertType($params)
Convert a JSON or Query encoded string to an array
For example, some snippet supports 2 formats in one of parameters: JSON or Query string. Users use the format that is convenient to them and we support both. Just call this method and don't care about it.
Both calls return:
Convert a Query encoded string to a JSON object string
Returns:
Convert a JSON object to a JSON array
Returns:
Convert a HJSON encoded string to an object
Returns:
Convert an associative array to a string of HTML attributes
Returns:
\DDTools\Tools\Objects::extend($params)
Merge two objects, modifying the first
Returns:
Also you can extend arrays
Returns:
Moreover, objects can extend arrays and vice versa
Returns:
Don't overwrite fields with empty values ($params->overwriteWithEmpty == false)
By default, empty field values (e. g. '') are treated as other values and will replace non-empty ones.
Returns:
Empty lastName from the second object replaced non-empty lastName from the first.
If you want to ignore empty values, just use $params->overwriteWithEmpty == false:
Returns:
Extending only specific properties from subsequent objects ($params->extendableProperties)
Sometimes you want to keep only the key ingredients, like avoiding the pineapple on your pizza.
Returns:
\DDTools\Tools\Objects::unfold($params)
Unfold an object
Returns:
Unfold an array
Returns:
Use custom key separator
Returns:
Cross-type unfolding ($params->isCrossTypeEnabled == true)
Without cross-type unfolding (by default)
Returns:
With cross-type unfolding enabled
Returns:
\DDTools\Tools\Objects::isPropExists($params)
Checks if the object, class or array has a property / element using the same syntax.
You can pass an object:
Or an array:
Both calls return true.
\DDTools\Tools\Objects::getPropValue($params)
Get the value of an object property or an array element using the same syntax
You can pass an object:
Or an array:
Both calls return 'Floyd'.
Get the value of an object property or an array element in any nesting level
Source object can be nested, and elements of all levels can be mix of objects and arrays.
Get a first-level property
There's nothing special, just look at this example for the full picture.
Returns:
Get a second-level property
Let's make it a little more interesting: let's get the 4th element of the second-level indexed array.
Returns:
Get a third-level property
Any level of nesting is supported.
No matter what type of element is used in any nesting level, the method will work fine. So let's get Roger's name. As you remember, he is stdClass as opposed to the other members who are associative arrays.
Returns:
Of course, it works fine with single-level objects that contain '.' in their property names
Returns:
\DDTools\ObjectCollection
Create a collection with items
Set items as a JSON string
\DDTools\ObjectCollection::getItems($params)
Get an array of items using filter ($params->filter)
Filter by existence of a property
Returns:
Filter by a property value
Returns:
Filter using several conditions
Returns:
Get an associative array of items using a property value as a result key
Returns:
Get a one-dimensional array of item property values
Returns:
\DDTools\ObjectCollection::getOneItem($params)
Returns:
Custom results when no items found
Returns:
\DDTools\ObjectCollection::convertItemsType($params)
Returns:
\DDTools\ObjectCollection::updateItems($params)
Returns:
\DDTools\ObjectCollection::deleteItems($params)
Returns: