Skip to content

Commit

Permalink
+ \DDTools\ObjectCollection::setOneItemData, getOneItemData: The …
Browse files Browse the repository at this point in the history
…new protected methods. All setting/getting of an item data inside the class must use these methods. Its convenient to override these methods in child classes if items are not plain objects.
  • Loading branch information
Ronef committed Jan 12, 2023
1 parent 194393e commit 4fa8ef9
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 18 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,60 @@ Gets an JSON-array of all collection 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`
* Desctription: Parameters, the pass-by-name style is used.
* Valid values:
* `stdClass`
* `arrayAssociative`
* **Required**

* `$params->itemIndex`
* Desctription: Item index which data will be set.
* Valid values: `integer`
* **Required**

* `$params->itemData`
* Desctription: New item data.
* Valid values:
* `array` — indexed arrays are supported as well as associative
* `object`
* **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`
* Desctription: Parameters, the pass-by-name style is used.
* Valid values:
* `stdClass`
* `arrayAssociative`
* **Required**

* `$params->itemObject`
* Desctription: An item object which data will be returned.
* Valid values:
* `array` — indexed arrays are supported as well as associative
* `object`
* **Required**


##### Returns

* `$result`
* Desctription: Data of an item object.
* Valid values:
* `array`
* `object`


### `\DDTools\Base\Base`

Simple abstract class with some small methods facilitating your work.
Expand Down
89 changes: 71 additions & 18 deletions src/ObjectCollection/ObjectCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function addItems($params = []){

/**
* convertItemsType
* @version 1.0.1 (2022-12-28)
* @version 1.0.2 (2022-12-28)
*
* @see README.md
*/
Expand Down Expand Up @@ -123,17 +123,22 @@ public function convertItemsType($params = []){
'filter' => $params->filter
])
){
$this->items[$itemIndex] = \DDTools\ObjectTools::convertType([
'object' => $itemObject,
'type' => $params->itemType
$this->setOneItemData([
'itemIndex' => $itemIndex,
'itemData' => \DDTools\ObjectTools::convertType([
'object' => $this->getOneItemData([
'itemObject' => $itemObject
]),
'type' => $params->itemType
])
]);
}
}
}

/**
* updateItems
* @version 1.0.1 (2022-12-28)
* @version 1.0.2 (2022-12-28)
*
* @see README.md
*/
Expand Down Expand Up @@ -169,11 +174,16 @@ public function updateItems($params = []){
'filter' => $params->filter
])
){
$this->items[$itemIndex] = \DDTools\ObjectTools::extend([
'objects' => [
$itemObject,
$params->data
]
$this->setOneItemData([
'itemIndex' => $itemIndex,
'itemData' => \DDTools\ObjectTools::extend([
'objects' => [
$this->getOneItemData([
'itemObject' => $itemObject
]),
$params->data
]
])
]);

//Increment result count
Expand All @@ -190,7 +200,7 @@ public function updateItems($params = []){

/**
* getItems
* @version 2.0.1 (2022-12-28)
* @version 2.0.2 (2022-12-28)
*
* @see README.md
*/
Expand Down Expand Up @@ -228,10 +238,14 @@ public function getItems($params = []){
'filter' => $params->filter
])
){
$itemData = $this->getOneItemData([
'itemObject' => $itemObject
]);

//Save only field value instead of object if needed
if (!is_null($params->propAsResultValue)){
$resultItemObject = \DDTools\ObjectTools::getPropValue([
'object' => $itemObject,
'object' => $itemData,
'propName' => $params->propAsResultValue
]);
}else{
Expand All @@ -242,7 +256,7 @@ public function getItems($params = []){
if (!is_null($params->propAsResultKey)){
$result[
\DDTools\ObjectTools::getPropValue([
'object' => $itemObject,
'object' => $itemData,
'propName' => $params->propAsResultKey
])
] = $resultItemObject;
Expand Down Expand Up @@ -354,11 +368,46 @@ public function deleteItems($params = []){
}
}

/**
* setOneItemData
* @version 1.0 (2022-12-27)
*
* @desc 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.
*
* @param $params {stdClass|arrayAssociative} — Parameters, the pass-by-name style is used. @required
* @param $params->itemIndex {integer} — Item index which data will be set. @required
* @param $params->itemData {array|object} — New item data. @required
*
* @return {void}
*/
protected function setOneItemData($params){
$params = (object) $params;

$this->items[$params->itemIndex] = $params->itemData;
}

/**
* getOneItemData
* @version 1.0 (2022-12-27)
*
* @desc 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.
*
* @param $params {stdClass|arrayAssociative} — Parameters, the pass-by-name style is used. @required
* @param $params->itemObject {array|object} — An item object which data will be returned. @required
*
* @return $result {object|arrayAssociative}
*/
protected function getOneItemData($params){
$params = (object) $params;

return $params->itemObject;
}

/**
* isItemMatchFilter
* @version 2.0 (2022-12-28)
* @version 2.0.1 (2022-12-28)
*
* @param $params {array} — Parameters, the pass-by-name style is used. @required
* @param $params {stdClass|arrayAssociative} — Parameters, the pass-by-name style is used. @required
* @param $params->itemObject {array|object} — An item to test. @required
* @param $params->filter {array} — Result of $this->prepareItemsFilter. @required
*
Expand All @@ -370,6 +419,10 @@ private function isItemMatchFilter($params){
//By default assume that item is matched
$result = true;

$itemData = $this->getOneItemData([
'itemObject' => $params->itemObject
]);

//Iterate over “or” conditions
foreach (
$params->filter as
Expand All @@ -383,7 +436,7 @@ private function isItemMatchFilter($params){
//If the item has no the property
if (
!\DDTools\ObjectTools::isPropExists([
'object' => $params->item,
'object' => $itemData,
'propName' => $andCondition->propName
])
){
Expand All @@ -395,7 +448,7 @@ private function isItemMatchFilter($params){
}elseif ($andCondition->operator == '=='){
$result =
\DDTools\ObjectTools::getPropValue([
'object' => $params->item,
'object' => $itemData,
'propName' => $andCondition->propName
]) ==
$andCondition->propValue
Expand All @@ -404,7 +457,7 @@ private function isItemMatchFilter($params){
}else{
$result =
\DDTools\ObjectTools::getPropValue([
'object' => $params->item,
'object' => $itemData,
'propName' => $andCondition->propName
]) !=
$andCondition->propValue
Expand Down

0 comments on commit 4fa8ef9

Please sign in to comment.