|
20 | 20 | use CodeIgniter\Pager\Pager; |
21 | 21 | use CodeIgniter\Validation\ValidationInterface; |
22 | 22 | use Config\Services; |
| 23 | +use InvalidArgumentException; |
23 | 24 | use ReflectionClass; |
24 | 25 | use ReflectionException; |
25 | 26 | use ReflectionProperty; |
@@ -715,31 +716,7 @@ public function insert($data = null, bool $returnID = true) |
715 | 716 | { |
716 | 717 | $this->insertID = 0; |
717 | 718 |
|
718 | | - if (empty($data)) |
719 | | - { |
720 | | - throw DataException::forEmptyDataset('insert'); |
721 | | - } |
722 | | - |
723 | | - // If $data is using a custom class with public or protected |
724 | | - // properties representing the collection elements, we need to grab |
725 | | - // them as an array. |
726 | | - if (is_object($data) && ! $data instanceof stdClass) |
727 | | - { |
728 | | - $data = $this->objectToArray($data, false, true); |
729 | | - } |
730 | | - |
731 | | - // If it's still a stdClass, go ahead and convert to |
732 | | - // an array so doProtectFields and other model methods |
733 | | - // don't have to do special checks. |
734 | | - if (is_object($data)) |
735 | | - { |
736 | | - $data = (array) $data; |
737 | | - } |
738 | | - |
739 | | - if (empty($data)) |
740 | | - { |
741 | | - throw DataException::forEmptyDataset('insert'); |
742 | | - } |
| 719 | + $data = $this->transformDataToArray($data, 'insert'); |
743 | 720 |
|
744 | 721 | // Validate data before saving. |
745 | 722 | if (! $this->skipValidation && ! $this->cleanRules()->validate($data)) |
@@ -877,32 +854,7 @@ public function update($id = null, $data = null): bool |
877 | 854 | $id = [$id]; |
878 | 855 | } |
879 | 856 |
|
880 | | - if (empty($data)) |
881 | | - { |
882 | | - throw DataException::forEmptyDataset('update'); |
883 | | - } |
884 | | - |
885 | | - // If $data is using a custom class with public or protected |
886 | | - // properties representing the collection elements, we need to grab |
887 | | - // them as an array. |
888 | | - if (is_object($data) && ! $data instanceof stdClass) |
889 | | - { |
890 | | - $data = $this->objectToArray($data, true, true); |
891 | | - } |
892 | | - |
893 | | - // If it's still a stdClass, go ahead and convert to |
894 | | - // an array so doProtectFields and other model methods |
895 | | - // don't have to do special checks. |
896 | | - if (is_object($data)) |
897 | | - { |
898 | | - $data = (array) $data; |
899 | | - } |
900 | | - |
901 | | - // If it's still empty here, means $data is no change or is empty object |
902 | | - if (empty($data)) |
903 | | - { |
904 | | - throw DataException::forEmptyDataset('update'); |
905 | | - } |
| 857 | + $data = $this->transformDataToArray($data, 'update'); |
906 | 858 |
|
907 | 859 | // Validate data before saving. |
908 | 860 | if (! $this->skipValidation && ! $this->cleanRules(true)->validate($data)) |
@@ -1692,6 +1644,55 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur |
1692 | 1644 | return $properties; |
1693 | 1645 | } |
1694 | 1646 |
|
| 1647 | + /** |
| 1648 | + * Transform data to array |
| 1649 | + * |
| 1650 | + * @param array|object|null $data Data |
| 1651 | + * @param string $type Type of data (insert|update) |
| 1652 | + * |
| 1653 | + * @return array |
| 1654 | + * |
| 1655 | + * @throws DataException |
| 1656 | + * @throws InvalidArgumentException |
| 1657 | + * @throws ReflectionException |
| 1658 | + */ |
| 1659 | + protected function transformDataToArray($data, string $type): array |
| 1660 | + { |
| 1661 | + if (! in_array($type, ['insert', 'update'], true)) |
| 1662 | + { |
| 1663 | + throw new InvalidArgumentException(sprintf('Invalid type "%s" used upon transforming data to array.', $type)); |
| 1664 | + } |
| 1665 | + |
| 1666 | + if (empty($data)) |
| 1667 | + { |
| 1668 | + throw DataException::forEmptyDataset($type); |
| 1669 | + } |
| 1670 | + |
| 1671 | + // If $data is using a custom class with public or protected |
| 1672 | + // properties representing the collection elements, we need to grab |
| 1673 | + // them as an array. |
| 1674 | + if (is_object($data) && ! $data instanceof stdClass) |
| 1675 | + { |
| 1676 | + $data = $this->objectToArray($data, true, true); |
| 1677 | + } |
| 1678 | + |
| 1679 | + // If it's still a stdClass, go ahead and convert to |
| 1680 | + // an array so doProtectFields and other model methods |
| 1681 | + // don't have to do special checks. |
| 1682 | + if (is_object($data)) |
| 1683 | + { |
| 1684 | + $data = (array) $data; |
| 1685 | + } |
| 1686 | + |
| 1687 | + // If it's still empty here, means $data is no change or is empty object |
| 1688 | + if (empty($data)) |
| 1689 | + { |
| 1690 | + throw DataException::forEmptyDataset($type); |
| 1691 | + } |
| 1692 | + |
| 1693 | + return $data; |
| 1694 | + } |
| 1695 | + |
1695 | 1696 | // endregion |
1696 | 1697 |
|
1697 | 1698 | // region Magic |
|
0 commit comments