From c161be521ff0f1ead5ada5636c11affcec20e3e4 Mon Sep 17 00:00:00 2001 From: Hayk Manasyan Date: Tue, 22 Aug 2017 18:48:15 +0400 Subject: [PATCH] - Updated Utils --- module/Utils/README.md | 6 + module/Utils/config/module.config.php | 5 + module/Utils/src/Mapper/DbMapper.php | 121 +++++++++++++++++++++ module/Utils/src/Module.php | 11 ++ module/Utils/src/Service/CsvParser.php | 26 +++++ module/Utils/src/Service/HostService.php | 58 ++++++++++ module/Utils/src/Service/ObjectService.php | 79 ++++++++++++++ module/Utils/src/Service/SessionTrait.php | 29 +++++ module/Utils/src/Service/StringService.php | 18 +++ 9 files changed, 353 insertions(+) create mode 100644 module/Utils/README.md create mode 100755 module/Utils/config/module.config.php create mode 100755 module/Utils/src/Mapper/DbMapper.php create mode 100755 module/Utils/src/Module.php create mode 100755 module/Utils/src/Service/CsvParser.php create mode 100644 module/Utils/src/Service/HostService.php create mode 100755 module/Utils/src/Service/ObjectService.php create mode 100755 module/Utils/src/Service/SessionTrait.php create mode 100755 module/Utils/src/Service/StringService.php diff --git a/module/Utils/README.md b/module/Utils/README.md new file mode 100644 index 0000000..1c87a5f --- /dev/null +++ b/module/Utils/README.md @@ -0,0 +1,6 @@ +# Utils +This is Utils module for ZF3 with useful classes and methods such as +# 1. Method for rest call +# 2. Method for doing db operations +# 3. Numeric and string operation +# 4. CSV parse. diff --git a/module/Utils/config/module.config.php b/module/Utils/config/module.config.php new file mode 100755 index 0000000..9efd9c2 --- /dev/null +++ b/module/Utils/config/module.config.php @@ -0,0 +1,5 @@ +dbAdapter = $dbAdapter; + $this->hydrator = $hydrator; + $this->entityPrototype = $entityPrototype; + } + + private function getSQL() + { + if(is_null($this->SQL)) { + $this->setSql(); + } + + return $this->SQL; + } + + private function setSQL() + { + $this->SQL = new Sql($this->dbAdapter); + + return; + } + + protected function makeSqlQuery($query, $returnType = self::RETURN_TYPE_OBJECT) + { + + try { + $stmt = $this->getSQL()->prepareStatementForSqlObject($query); + + $result = $stmt->execute(); + } catch (\Exception $ex) { +//var_dump($ex->getMessage()); die; + throw new \Exception('There was an error while executing query.'); + } + + if ($result instanceof ResultInterface && $result->getAffectedRows()) { + if($returnType === self::RETURN_TYPE_OBJECT) { + $resultSet = new HydratingResultSet($this->hydrator, $this->entityPrototype); + $resultSet->initialize($result); + + return $resultSet; + } else { + $resultSet = new ResultSet(); + $resultSet->initialize($result); + + return $resultSet->toArray(); + } + } + + return null; + } + + protected function makeSingleSqlQuery($query) + { + try { + $stmt = $this->getSQL()->prepareStatementForSqlObject($query); + $result = $stmt->execute(); + } catch (\Exception $ex) { + throw new \Exception('There was an error while executing single query.'); + } + + if ($result instanceof ResultInterface && $result->getAffectedRows()) { + return $this->hydrator->hydrate($result->current(), $this->entityPrototype); + } + + return null; + } + + protected function inertOrUpdateSqlQuery($query) + { + try { + $sql = new Sql($this->dbAdapter); + $stmt = $sql->prepareStatementForSqlObject($query); + $result = $stmt->execute(); + } catch (Exception $ex) { + var_dump($ex->getMessage()); + throw new Exception('Faild save/update.'); + } + + if ($result instanceof ResultInterface) { + return $result; + } + + throw new \Exception("Database error"); + + } +} \ No newline at end of file diff --git a/module/Utils/src/Module.php b/module/Utils/src/Module.php new file mode 100755 index 0000000..8fa04ce --- /dev/null +++ b/module/Utils/src/Module.php @@ -0,0 +1,11 @@ +setAdapter(new Client\Adapter\Curl()); + + $request = new Request(); + + + + if(false == $isPost) { + $requestStr = '?'; + foreach ($params as $key => $param) { + $requestStr .= $key . '=' . $param . '&'; + } + + $url .= $requestStr; + } else { + $request->setMethod(Request::METHOD_POST); + } + + $request->setUri($url); + + $header = new Headers(); + if(!is_null($contentType)) { + + + $header->addHeaderLine("Content-Type", $contentType); + $request->setHeaders($header); + + } elseif($contentType == 'application/json') { + $request->setContent(json_encode($params)); + } else { + $request->setContent($params); + } + + $response = $client->dispatch($request); + + return $response->getContent(); + } +} \ No newline at end of file diff --git a/module/Utils/src/Service/ObjectService.php b/module/Utils/src/Service/ObjectService.php new file mode 100755 index 0000000..1e6a53e --- /dev/null +++ b/module/Utils/src/Service/ObjectService.php @@ -0,0 +1,79 @@ +extract($obj); + unset($hydrator); + unset($obj); + + return $data; + } + + public static function convertArrayToObj($data, $prototype = null) + { + + if ($data instanceof \stdClass) { + $data = (array) $data; + } + + if (!is_array($data)) { + throw new Exception('No array was given'); + } + + + + if (is_null($prototype)) { + $prototype = new \stdClass(); + } + + $hydrate = new ClassMethods(); + + $obj = $hydrate->hydrate($data, $prototype); + unset($hydrate); + unset($unit); + + return $obj; + } + + public static function removeFromArrayNulls($array) + { + if (!is_array($array) || 0 == count($array)) { + return null; + } + + foreach ($array as $key => $val) { + if ($val == NULL || $val == '' || $val == 'NULL' || $key == 'submit') { + unset($array[$key]); + } + } + + return $array; + } + +} diff --git a/module/Utils/src/Service/SessionTrait.php b/module/Utils/src/Service/SessionTrait.php new file mode 100755 index 0000000..5b05b06 --- /dev/null +++ b/module/Utils/src/Service/SessionTrait.php @@ -0,0 +1,29 @@ +sessionContainer)) { + $this->setSession($sessionName); + } + + return $this->sessionContainer; + } + + protected function setSession($sessionName) + { + $this->sessionContainer = new Container($sessionName); + return $this; + } + +} \ No newline at end of file diff --git a/module/Utils/src/Service/StringService.php b/module/Utils/src/Service/StringService.php new file mode 100755 index 0000000..20f6da9 --- /dev/null +++ b/module/Utils/src/Service/StringService.php @@ -0,0 +1,18 @@ +