Skip to content

Commit

Permalink
1) добавлен глобальный обработчик ошибок валидации
Browse files Browse the repository at this point in the history
2) правки отправки данных
3) мелкие доработки
  • Loading branch information
viktor-melnikov committed Aug 18, 2017
1 parent f429111 commit 8563591
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 61 deletions.
32 changes: 32 additions & 0 deletions src/Exceptions/RequesterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Created by PhpStorm.
* User: viktorthegreat
* Date: 25.02.17
* Time: 16:53
*/

namespace Requester\Exceptions;

use Exception;

class RequesterException extends Exception
{
public $options = [];

public function __construct( $message = "", $code = 200, $options = [], Exception $previous = null )
{
if ( !empty( $options ) )
{
$this->options = $options;
}

$this->code = $code == 0 ? 400 : $code;
$this->message = $message;
}

public function getOptions( $option )
{
return array_key_exists( $option, $this->options ) ? $this->options[ $option ] : null;
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/ValidateInputException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Created by PhpStorm.
* User: viktorthegreat
* Date: 25.02.17
* Time: 16:53
*/

namespace Requester\Exceptions;

class ValidateInputException extends RequesterException
{
}
28 changes: 18 additions & 10 deletions src/Handler/CacheLogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public function initializeCache()

//если логгер был инициализирован прежде кэша, значит пропихнем его туда.
//в противном случае логи все равно выключены.
if ( $this->log instanceof LoggerAbstract ) {
if ( $this->log instanceof LoggerAbstract )
{
$this->cache->setLogger( $this->log->getLogger() );
}

Expand All @@ -81,13 +82,16 @@ public function initializeCache()
*/
public function searchAlias( $alias, array $aliases = [] )
{
if ( isset( $aliases[ $alias ] ) ) {
if ( isset( $aliases[ $alias ] ) )
{
$ttl = $aliases[ $alias ];
}
else if ( array_search( $alias, (array)$aliases )!==false ) {
else if ( array_search( $alias, (array)$aliases ) !== false )
{
$ttl = 3600;
}
else {
else
{
return false;
}

Expand All @@ -113,11 +117,13 @@ public function beforeExecute()
*/
public function beforeExecuteReturn()
{
if ( $this->request->config->has( 'cache.aliases' ) && array_key_exists( $this->request->alias, $this->request->config->get( 'cache.aliases' )->toArray() ) ) {
if ( $this->request->config->has( 'cache.aliases' ) && array_key_exists( $this->request->alias, $this->request->config->get( 'cache.aliases' )->toArray() ) )
{

$item = $this->cache->getItem( $this->request->alias );

if ( !$item->isMiss() ) {
if ( !$item->isMiss() )
{

return $item->get();
}
Expand All @@ -128,11 +134,13 @@ public function beforeExecuteReturn()

public function afterExecuteReturn( $body )
{
if ( $this->request->config->has( 'cache.aliases' ) && array_key_exists( $this->request->alias, $this->request->config->get( 'cache.aliases' )->toArray() ) ) {
if ( $this->request->config->has( 'cache.aliases' ) && array_key_exists( $this->request->alias, $this->request->config->get( 'cache.aliases' )->toArray() ) )
{

$hasSubqueryOrLifetime = $this->searchAlias( $this->request->alias, $this->request->config->get( 'cache.aliases' )->toArray() );

if ( $hasSubqueryOrLifetime ) {
if ( $hasSubqueryOrLifetime )
{
$item = $this->cache->getItem( $this->request->alias );

$this->cache->save( $item->expiresAfter( $hasSubqueryOrLifetime )->set( $body ) );
Expand Down Expand Up @@ -181,14 +189,14 @@ private function getLogDefaultConfig()
{
return [
'channel' => 'no_channel',
'path' => storage_path( 'logs' ),
'path' => app( 'path.storage' ) . 'logs',
];
}

private function getCacheDefaultConfig()
{
return [
'path' => storage_path( 'cache' ),
'path' => app( 'path.storage' ) . 'cache',
];
}
}
41 changes: 20 additions & 21 deletions src/HelperFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@

use DateTime;
use IntlDateFormatter;
use Requester\Exceptions\ValidateInputException;

class HelperFormatter
{
public function formatDate($date, $pattern = '')
public function formatDate( $date, $pattern = '' )
{
if( ! $date instanceof DateTime)
if ( !$date instanceof DateTime )
{
$date = new DateTime($date);
$date = new DateTime( $date );
}

$intl = ( new IntlDateFormatter('ru_RU', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Europe/Moscow') );
$intl = ( new IntlDateFormatter( 'ru_RU', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Europe/Moscow' ) );

$intl->setPattern($pattern);
$intl->setPattern( $pattern );

return $intl->format($date);
return $intl->format( $date );
}

public function getNoun( $num, $one, $two, $five )
Expand All @@ -50,19 +51,6 @@ public function getNoun( $num, $one, $two, $five )
return $five;
}

public function getAdj( $num, $one, $two )
{
$num %= 100;

if($num == 11) return $two;

$num %= 10;

if($num == 1) return $one;

return $two;
}

/**
* Форматирование телефонного номера (убираем все спец символы и оставляем только цифры)
*
Expand All @@ -84,14 +72,25 @@ public static function clearAllSymbols( $string )
* @param string $phone
*
* @return bool|string
* @throws ValidateInputException
* @internal param array|string $format
*/
public static function phone_format( $phone )
{
$phone = preg_replace( '/[^0-9]/', '', $phone );

if ( !empty($phone) && $phone[ 0 ]=='8' ) {
$phone[ 0 ] = '7';
if ( strlen( $phone ) < 10 )
{
throw new ValidateInputException( null, 400, [ 'error_code' => 14, 'error_params' => [ 'номер телефона' ] ] );
}

if ( !empty( $phone ) && strlen( $phone ) == 11 && $phone[0] == '8' )
{
$phone[0] = '7';
}
elseif ( strlen( $phone ) == 10 )
{
$phone = '7' . $phone;
}

return $phone;
Expand Down
Loading

0 comments on commit 8563591

Please sign in to comment.