diff --git a/README.md b/README.md index a768871..308ad4c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/ObjectCollection/ObjectCollection.php b/src/ObjectCollection/ObjectCollection.php index 157bd74..199c453 100644 --- a/src/ObjectCollection/ObjectCollection.php +++ b/src/ObjectCollection/ObjectCollection.php @@ -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 */ @@ -123,9 +123,14 @@ 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 + ]) ]); } } @@ -133,7 +138,7 @@ public function convertItemsType($params = []){ /** * updateItems - * @version 1.0.1 (2022-12-28) + * @version 1.0.2 (2022-12-28) * * @see README.md */ @@ -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 @@ -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 */ @@ -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{ @@ -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; @@ -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 * @@ -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 @@ -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 ]) ){ @@ -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 @@ -404,7 +457,7 @@ private function isItemMatchFilter($params){ }else{ $result = \DDTools\ObjectTools::getPropValue([ - 'object' => $params->item, + 'object' => $itemData, 'propName' => $andCondition->propName ]) != $andCondition->propValue