Skip to content

Commit

Permalink
- Updated Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
hayk-manasyan committed Aug 22, 2017
1 parent 6092054 commit c161be5
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 0 deletions.
6 changes: 6 additions & 0 deletions module/Utils/README.md
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.
5 changes: 5 additions & 0 deletions module/Utils/config/module.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Utils;

return [];
121 changes: 121 additions & 0 deletions module/Utils/src/Mapper/DbMapper.php
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");

}
}
11 changes: 11 additions & 0 deletions module/Utils/src/Module.php
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';
}
}
26 changes: 26 additions & 0 deletions module/Utils/src/Service/CsvParser.php
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;
}
}
58 changes: 58 additions & 0 deletions module/Utils/src/Service/HostService.php
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();
}
}
79 changes: 79 additions & 0 deletions module/Utils/src/Service/ObjectService.php
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;
}

}
29 changes: 29 additions & 0 deletions module/Utils/src/Service/SessionTrait.php
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;
}

}
18 changes: 18 additions & 0 deletions module/Utils/src/Service/StringService.php
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;
}
}

0 comments on commit c161be5

Please sign in to comment.