-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6092054
commit c161be5
Showing
9 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace Utils; | ||
|
||
return []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
namespace Utils\Mapper; | ||
|
||
use Zend\Db\Adapter\AdapterInterface; | ||
use Zend\Db\Adapter\Driver\ResultInterface; | ||
use Zend\Db\ResultSet\HydratingResultSet; | ||
use Zend\Db\ResultSet\ResultSet; | ||
use Zend\Db\Sql\Sql; | ||
use Zend\Hydrator\HydratorInterface; | ||
use Exception; | ||
|
||
abstract class DbMapper | ||
{ | ||
|
||
const RETURN_TYPE_ARRAY = 'array'; | ||
const RETURN_TYPE_OBJECT = 'object'; | ||
|
||
/** | ||
* @var AdapterInterface | ||
*/ | ||
protected $dbAdapter; | ||
|
||
/** | ||
* @var HydratorInterface | ||
*/ | ||
protected $hydrator; | ||
|
||
/** | ||
* @var | ||
*/ | ||
protected $entityPrototype; | ||
|
||
private $SQL; | ||
|
||
public function __construct(AdapterInterface $dbAdapter, HydratorInterface $hydrator, $entityPrototype) | ||
{ | ||
$this->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"); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Utils; | ||
|
||
class Module | ||
{ | ||
public function getConfig() | ||
{ | ||
return include __DIR__ . '/../config/module.config.php'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Utils\Service; | ||
|
||
|
||
class CsvParser | ||
{ | ||
|
||
public static function parseCsv($filePath) | ||
{ | ||
$row = 1; | ||
$fileData = []; | ||
if (($handle = fopen($filePath, "r")) !== FALSE) { | ||
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { | ||
$num = count($data); | ||
$row++; | ||
for ($c=0; $c < $num; $c++) { | ||
$fileData[] = $data[$c]; | ||
} | ||
} | ||
fclose($handle); | ||
} | ||
|
||
return $fileData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: hayk | ||
* Date: 8/21/17 | ||
* Time: 1:31 PM | ||
*/ | ||
|
||
namespace Utils\Service; | ||
|
||
|
||
use Zend\Http\Client; | ||
use Zend\Http\Headers; | ||
use Zend\Http\Request; | ||
|
||
class HostService | ||
{ | ||
public static function makeApiCall($url, $params = null, $isPost = false, $contentType = null) | ||
{ | ||
$client = new Client(); | ||
|
||
$client->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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
|
||
/** | ||
* Description of ObjectService | ||
* | ||
* @author hayk | ||
*/ | ||
|
||
namespace Utils\Service; | ||
|
||
use Exception; | ||
use Zend\Hydrator\ClassMethods; | ||
|
||
class ObjectService { | ||
|
||
public static function convertObjToArray($obj) | ||
{ | ||
if (!is_object($obj)) { | ||
throw new Exception('No object was given'); | ||
} | ||
if ($obj instanceof \stdClass) { | ||
return (array) $obj; | ||
} | ||
$hydrator = new ClassMethods(); | ||
$data = $hydrator->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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Utils\Service; | ||
|
||
|
||
use Zend\Session\Container; | ||
|
||
trait SessionTrait | ||
{ | ||
|
||
|
||
protected $sessionContainer; | ||
|
||
public function getSession($sessionName = 'GifterySession') | ||
{ | ||
if(is_null($this->sessionContainer)) { | ||
$this->setSession($sessionName); | ||
} | ||
|
||
return $this->sessionContainer; | ||
} | ||
|
||
protected function setSession($sessionName) | ||
{ | ||
$this->sessionContainer = new Container($sessionName); | ||
return $this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
namespace Utils\Service; | ||
|
||
|
||
class StringService | ||
{ | ||
/** | ||
* | ||
* @param $string | ||
* @return array | ||
*/ | ||
public static function explodeStringWhiteSpaces($string) | ||
{ | ||
$parts = preg_split('/\s+/', $string); | ||
|
||
return $parts; | ||
} | ||
} |