Skip to content

Commit

Permalink
Version 0.54
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronef committed Jan 8, 2022
2 parents de46a89 + 6ead9f2 commit f6d6c5a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# (MODX)EvolutionCMS.libraries.ddTools changelog


## Version 0.54 (2022-01-08)
* \+ `\DDTools\BaseClass::setExistingProps` → Parameters → `$props`: Can also be set as a [JSON](https://en.wikipedia.org/wiki/JSON), [HJSON](https://hjson.github.io/) or [Query formatted](https://en.wikipedia.org/wiki/Query_string) string.
* \* Included PHP.libraries.HJSON has been updated from 2.1 to 2.2.
* \+ README → Installation → Update using (MODX)EvolutionCMS.libraries.ddInstaller.


## Version 0.53 (2021-12-13)
* \+ `\DDTools\ObjectCollection`: The new class representing a collection of some objects or arrays. See more info and examples in README.
* \* `\ddTools::parseText` → Parameters → `$params->data`: A bug with multidimensional objects has been fixed.
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG_ru.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# (MODX)EvolutionCMS.libraries.ddTools changelog


## Версия 0.54 (2022-01-08)
* \+ `\DDTools\BaseClass::setExistingProps` → Параметры → `$props`: Также может быть задан как строка в формете [JSON](https://ru.wikipedia.org/wiki/JSON), [HJSON](https://hjson.github.io/) или [Query](https://en.wikipedia.org/wiki/Query_string).
* \* PHP.libraries.HJSON, включённая в репозиторий, обновлена с 2.1 до 2.2.
* \+ README → Установка → Обновление используя (MODX)EvolutionCMS.libraries.ddInstaller.


## Версия 0.53 (2021-12-13)
* \+ `\DDTools\ObjectCollection`: Новый класс, представляющий коллекцию некоторых объектов или массивов. См. больше информации и примеры в README.
* \* `\ddTools::parseText` → Параметры → `$params->data`: Исправлена ошибка с многомерными объектами.
Expand Down
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ A library with various tools facilitating your work.

* PHP >= 5.6
* [(MODX)EvolutionCMS](https://github.com/evolution-cms/evolution) >= 1.1
* [PHP.libraries.HJSON](https://github.com/hjson/hjson-php) 2.2 (included)
* [PHP.libraries.phpThumb](http://phpthumb.sourceforge.net) 1.7.15-202004301145 (included)
* [PHP.libraries.hjson](https://github.com/hjson/hjson-php) 2.1 (included)


## Installation


### Manual
### Manually

1. Create a new folder `assets/libs/ddTools/`.
2. Extract the archive to the folder.
Expand All @@ -27,6 +27,28 @@ 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](https://github.com/DivanDesign/EvolutionCMS.libraries.ddInstaller)

Just run the following PHP code in your sources or [Console](https://github.com/vanchelo/MODX-Evolution-Ajax-Console):

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

//Update (MODX)EvolutionCMS.libraries.ddTools
\DDInstaller::install([
'url' => 'https://github.com/DivanDesign/EvolutionCMS.libraries.ddTools',
'type' => 'snippet'
]);
```

* If `ddTools` is already exist on your site, `ddInstaller` will check it version and update it if needed.
* If `ddTools` is not exist on your site, `ddInstaller` can't do anything because requires it for itself.


## Parameters description


Expand Down Expand Up @@ -59,7 +81,7 @@ Like `$modx->parseChunk`, but takes a text and has some features.
* `stdClass`
* `stringJsonObject` — as [JSON](https://en.wikipedia.org/wiki/JSON)
* `stringHjsonObject` — as [HJSON](https://hjson.github.io/)
* `stringQueryFormated` — as [Query string](https://en.wikipedia.org/wiki/Query_string)
* `stringQueryFormatted` — as [Query string](https://en.wikipedia.org/wiki/Query_string)
* Default value: `[]`

* `$params->data->{$key}`
Expand Down Expand Up @@ -327,7 +349,7 @@ Arrays, [JSON](https://en.wikipedia.org/wiki/JSON) and [Query string](https://en
* `stringJsonArray` — [JSON](https://en.wikipedia.org/wiki/JSON) array
* `stringHjsonObject` — [HJSON](https://hjson.github.io/) object
* `stringHjsonArray` — [HJSON](https://hjson.github.io/) array
* `stringQueryFormated` — [Query string](https://en.wikipedia.org/wiki/Query_string)
* `stringQueryFormatted` — [Query string](https://en.wikipedia.org/wiki/Query_string)
* **Required**

* `$params->type`
Expand All @@ -340,7 +362,7 @@ Arrays, [JSON](https://en.wikipedia.org/wiki/JSON) and [Query string](https://en
* `'stringJsonAuto'` — `stringJsonObject` or `stringJsonArray` depends on input object
* `'stringJsonObject'`
* `'stringJsonArray'`
* `'stringQueryFormated'`
* `'stringQueryFormatted'`
* Default value: `'objectAuto'`


Expand Down Expand Up @@ -465,7 +487,7 @@ Class representing a collection of some objects or arrays.
* `stringJsonArray` — [JSON](https://en.wikipedia.org/wiki/JSON) array
* `stringHjsonObject` — [HJSON](https://hjson.github.io/) object
* `stringHjsonArray` — [HJSON](https://hjson.github.io/) array
* `stringQueryFormated` — [Query string](https://en.wikipedia.org/wiki/Query_string)
* `stringQueryFormatted` — [Query string](https://en.wikipedia.org/wiki/Query_string)
* Default value: —

* `$params->items[$itemIndex]`
Expand Down Expand Up @@ -713,8 +735,12 @@ Sets existing 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:
* `stdClass`
* `arrayAssociative`
* `object`
* It can also be set as an object-like string:
* `stringJsonObject` — as [JSON](https://en.wikipedia.org/wiki/JSON)
* `stringHjsonObject` — as [HJSON](https://hjson.github.io/)
* `stringQueryFormatted` — as [Query string](https://en.wikipedia.org/wiki/Query_string)
* **Required**

* `$props->{$propName}`
Expand Down Expand Up @@ -880,7 +906,7 @@ Abstract class for snippets.
* `stdClass`
* `arrayAssociative`
* `stringJsonObject`
* `stringQueryFormated`
* `stringQueryFormatted`
* Default value: `[]`

* `$params->{$paramName}`
Expand All @@ -906,7 +932,7 @@ Static method for easy running needed snippet using only it's name and parameter
* `stdClass`
* `arrayAssociative`
* `stringJsonObject`
* `stringQueryFormated`
* `stringQueryFormatted`
* **Required**

* `$params->name`
Expand All @@ -920,7 +946,7 @@ Static method for easy running needed snippet using only it's name and parameter
* `stdClass`
* `arrayAssociative`
* `stringJsonObject`
* `stringQueryFormated`
* `stringQueryFormatted`
* Default value: —

* `$params->params->{$paramName}`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dd/evolutioncms-libraries-ddtools",
"type": "modxevo-library-ddtools",
"version": "0.53.0",
"version": "0.54.0",
"description": "A library with various tools facilitating your work.",
"keywords": [
"modx",
Expand Down
4 changes: 2 additions & 2 deletions modx.ddtools.class.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/**
* EvolutionCMS.libraries.ddTools
* @version 0.53 (2021-12-13)
* @version 0.54 (2022-01-08)
*
* @see README.md
*
* @copyright 2012–2021 DD Group {@link https://DivanDesign.biz }
* @copyright 2012–2022 DD Group {@link https://DivanDesign.biz }
*/

global $modx;
Expand Down
9 changes: 7 additions & 2 deletions src/BaseClass/BaseClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
class BaseClass {
/**
* setExistingProps
* @version 1.3.1 (2020-05-06)
* @version 1.4 (2022-01-08)
*
* @see README.md
*
* @return {void}
*/
public function setExistingProps($props){
$props = (object) $props;
if (is_string($props)){
$props = \DDTools\ObjectTools::convertType([
'object' => $props,
'type' => 'objectStdClass'
]);
}

foreach (
$props as
Expand Down
64 changes: 42 additions & 22 deletions src/ObjectTools/hjson/HJSONParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
class HJSONParser
{

private $text;
private $textArray;
private $textLengthChars;
private $at; // The index of the current character
private $ch; // The current character
private $escapee = [];
Expand All @@ -29,7 +30,9 @@ public function __construct()
public function parse($source, $options = [])
{
$this->keepWsc = $options && isset($options['keepWsc']) && $options['keepWsc'];
$this->text = $source;
$this->textArray = preg_split("//u", $source, null, PREG_SPLIT_NO_EMPTY);
$this->textLengthChars = count($this->textArray);

$data = $this->rootValue();

if ($options && isset($options['assoc']) && $options['assoc']) {
Expand Down Expand Up @@ -312,26 +315,33 @@ private function error($m)
$colBytes = 0;
$line=1;

// Start with where we're at now, count back to most recent line break
// - to determine "column" of error hit
$i = $this->at;
while ($i > 0) {
$ch = mb_substr(mb_strcut($this->text, $i - 1), 0, 1);
$i -= strlen($ch);
// Mimic old behavior with mb_substr
if ($i >= $this->textLengthChars) {
$ch = "";
} else {
$ch = $this->textArray[$i];
}

--$i;
if ($ch === "\n") {
break;
}

$col++;
$colBytes += strlen($ch);
}

for (; $i > 0;
$i--) {
if ($this->text[$i] === "\n") {
// Count back line endings from there to determine line# of error hit
for (; $i > 0; $i--) {
if ($this->textArray[$i] === "\n") {
$line++;
}
}
throw new HJSONException("$m at line $line, $col >>>". mb_substr(mb_strcut($this->text, $this->at - $colBytes), 0, 20) ." ...");

throw new HJSONException("$m at line $line, $col >>>". implode(array_slice($this->textArray, $this->at - $col, 20)) ." ...");
}

private function next($c = false)
Expand All @@ -344,19 +354,29 @@ private function next($c = false)

// Get the next character. When there are no more characters,
// return the empty string.
$this->ch = (strlen($this->text) > $this->at) ? mb_substr(mb_strcut($this->text, $this->at), 0, 1) : null;
$this->at += strlen($this->ch);
$this->ch = ($this->textLengthChars > $this->at) ? $this->textArray[$this->at] : null;
++$this->at;
return $this->ch;
}

/**
* Peek at character at given offset from current "at"
* - >=0 - ahead of "at"
* - <0 = before "at"
*/
private function peek($offs)
{
// range check is not required
if ($offs >= 0) {
return mb_substr(mb_strcut($this->text, $this->at), $offs, 1);
} else {
return mb_substr(mb_strcut($this->text, 0, $this->at), $offs, 1);
$index = $this->at + $offs;

// Mimic old behavior with mb_substr
if ($index < 0) {
$index = 0;
}
if ($index >= $this->textLengthChars) {
return "";
}

return $this->textArray[$index];
}

private function skipIndent($indent)
Expand Down Expand Up @@ -526,20 +546,20 @@ private function getComment($wat)
$i;
$wat--;
// remove trailing whitespace
for ($i = $this->at - 2; $i > $wat && $this->text[$i] <= ' ' && $this->text[$i] !== "\n";
$i--) {
for ($i = $this->at - 2; $i > $wat && $this->textArray[$i] <= ' ' && $this->textArray[$i] !== "\n"; $i--) {
}

// but only up to EOL
if ($this->text[$i] === "\n") {
if ($i > 0 && $this->textArray[$i] === "\n") {
$i--;
}
if ($this->text[$i] === "\r") {
if ($i > 0 && $this->textArray[$i] === "\r") {
$i--;
}

$res = mb_substr($this->text, $wat, $i-$wat+1);
for ($i = 0; $i < mb_strlen($res); $i++) {
$res = array_slice($this->textArray, $wat, $i-$wat+1);
$res_len = count($res);
for ($i = 0; $i < $res_len; $i++) {
if ($res[$i] > ' ') {
return $res;
}
Expand Down

0 comments on commit f6d6c5a

Please sign in to comment.