Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
.idea/*
code/*
nginx/*
redis/*
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# PHP2021
### старый код в папке old, новый в new

После рефакторинга кода согласно принципам solid
проверка правильности ввода данных выведена в отдельный
класс Check из Application.

В новый класс Check перенесены переменные:

private $openBracket = '('; -> static $openBracket = '(';

private $closeBracket = ')'; -> static $closeBracket = ')';

Так же функция проверки пары checkBracketPairs() перенесена
в класс Check;


31 changes: 31 additions & 0 deletions new/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App;

use App\Check;

class Application
{

public function __construct()
{
if (!RequestValidator::checkRequestType('POST')) {
throw new \Exception('Wrong request method');
}
if (RequestValidator::checkRequestIsEmpty($_POST)) {
throw new \Exception('Empty request');
}
}

public function run()
{
if (!isset($_POST['STRING']) || empty($_POST['STRING'])) {
throw new \Exception('No string passed');
}
if (strpos($_POST['STRING'], Check::$openBracket) === false ||
strpos($_POST['STRING'], Check::$closeBracket ) === false) {
throw new \Exception('No brackets passed in string');
}
Check::checkBracketPairs($_POST['STRING']);
}
}
32 changes: 32 additions & 0 deletions new/Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace App;

class Check
{
const MESSAGE = 'Everything is fine';
static $openBracket = '(';
static $closeBracket = ')';


static function checkBracketPairs(string $request) : void
{
$arChars = str_split($request);
$openBracketsCounter = 0;
foreach ($arChars AS $singleChar) {
if ($openBracketsCounter <= 0) {
throw new \Exception('Brackets not paired');
}
if ($singleChar == self::$closeBracket) {
$openBracketsCounter--;
} elseif ($singleChar == self::$openBracket) {
$openBracketsCounter++;
}
}
if ($openBracketsCounter) {
throw new \Exception('Brackets not paired');
}
throw new \Exception(self::MESSAGE);
}
}
16 changes: 16 additions & 0 deletions new/RequestValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App;

class RequestValidator
{
public static function checkRequestType($typeNeeded)
{
return $_SERVER['REQUEST_METHOD'] == $typeNeeded ? true : false;
}

public static function checkRequestIsEmpty($request)
{
return empty($request) ? true : false;
}
}
19 changes: 19 additions & 0 deletions new/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App;

class Response
{
public static function generateResponse($responseText)
{
switch($responseText) {
case Check::MESSAGE:
header('HTTP/1.0 200 Ok');
break;
default:
header('HTTP/1.0 400 Bad Request');
break;
}
echo $responseText.PHP_EOL;
}
}
54 changes: 54 additions & 0 deletions old/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App;

class Application
{
private $request;
private $openBracket = '(';
private $closeBracket = ')';

public function __construct()
{
if (!RequestValidator::checkRequestType('POST')) {
throw new \Exception('Wrong request method');
}
if (RequestValidator::checkRequestIsEmpty($_POST)) {
throw new \Exception('Empty request');
}
$this->request = $_POST;
}

public function run()
{
if (!isset($this->request['STRING']) || empty($this->request['STRING'])) {
throw new \Exception('No string passed');
}
if (strpos($this->request['STRING'], $this->openBracket) === false ||
strpos($this->request['STRING'], $this->closeBracket) === false) {
throw new \Exception('No brackets passed in string');
}
$this->checkBracketPairs();
}

private function checkBracketPairs()
{
$arChars = str_split($this->request['STRING']);
$openBracketsCounter = 0;
foreach($arChars AS $singleChar) {
if ($singleChar == $this->closeBracket) {
if($openBracketsCounter <= 0) {
throw new \Exception('Brackets not paired');
}
$openBracketsCounter--;
}
if ($singleChar == $this->openBracket) {
$openBracketsCounter++;
}
}
if ($openBracketsCounter) {
throw new \Exception('Brackets not paired');
}
throw new \Exception('Everything is fine');
}
}
16 changes: 16 additions & 0 deletions old/RequestValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App;

class RequestValidator
{
public static function checkRequestType($typeNeeded)
{
return $_SERVER['REQUEST_METHOD'] == $typeNeeded ? true : false;
}

public static function checkRequestIsEmpty($request)
{
return empty($request) ? true : false;
}
}
19 changes: 19 additions & 0 deletions old/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App;

class Response
{
public static function generateResponse($responseText)
{
switch($responseText) {
case 'Everything is fine':
header('HTTP/1.0 200 Ok');
break;
default:
header('HTTP/1.0 400 Bad Request');
break;
}
echo $responseText.PHP_EOL;
}
}
Binary file added uml.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.