Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Решение тестовой задачи #41

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions php/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/composer.lock
/composer.phar
/vendor/
/vendor
56 changes: 0 additions & 56 deletions php/1.php

This file was deleted.

90 changes: 0 additions & 90 deletions php/2.php

This file was deleted.

21 changes: 21 additions & 0 deletions php/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"minimum-stability": "stable",
"require": {
"php": ">=7.4",
"ext-pdo": "*",
"ext-json": "*"
},
"require-dev": {
},
"autoload": {
"psr-4": {
"\\": "src/"
}
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
}
25 changes: 25 additions & 0 deletions php/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '3.3'
services:
db:
image: mysql:8
restart: always
environment:
MYSQL_DATABASE: 'db'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'dbuser'
# You can use whatever password you like
MYSQL_PASSWORD: 'dbpass'
# Password for root access
MYSQL_ROOT_PASSWORD: 'password'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- my-db:/var/lib/mysql
# Names our volume
volumes:
my-db:
3 changes: 2 additions & 1 deletion php/readme.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
### Тестовое задание для разработчика на PHP
Мы ожидаем, что Вы найдете все возможные ошибки (синтаксические, проектирования, безопасности и т.д.)

Насчёт ошибок проектирования сложновато судить, так как непонятна конечная цель в виду отсутствия конкретной задачи. Поэтому оставил примерно как есть.
140 changes: 140 additions & 0 deletions php/src/Db/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace Db;

class Command
{
private const TYPE_MAP = [
'boolean' => \PDO::PARAM_BOOL,
'integer' => \PDO::PARAM_INT,
'string' => \PDO::PARAM_STR,
'resource' => \PDO::PARAM_LOB,
'NULL' => \PDO::PARAM_NULL,
];
/**
* @var string|null
*/
private ?string $sql;
/**
* @var array
*/
private array $params;
/**
* @var \PDOStatement|null
*/
private ?\PDOStatement $pdoStatement = null;

public function __construct(?string $sql = null, array $params = [])
{
$this->sql = $sql;
$this->params = $params;
}

public function queryAll(): array
{
return $this->queryInternal('fetchAll');
}

public function queryOne(): ?array
{
$result = $this->queryInternal('fetch');
return (false === $result) ? null : $result;
}

public function execute(): void
{
$this->prepare();
$this->pdoStatement()->execute();
}

public function insert()
{
$this->execute();
return Db::i()->pdo()->lastInsertId();
}

/**
* @param array $params
*
* @return $this
*/
public function setParams(array $params): self
{
$this->params = $params;
return $this;
}

/**
* @param array $params
*
* @return $this
*/
public function addParams(array $params): self
{
foreach ($params as $param => $value) {
$this->params[$param] = $value;
}
return $this;
}

/**
* @return \PDOStatement
*/
public function pdoStatement(): \PDOStatement
{
return $this->pdoStatement ?? ($this->pdoStatement = Db::i()->pdo()->prepare($this->requireSql()));
}

/**
* @param string $sql
*
* @return $this
*/
public function setSql(string $sql): self
{
$this->sql = $sql;
return $this;
}

/**
* @return string
*/
public function requireSql(): string
{
if (null === $this->sql) {
throw new \RuntimeException('SQL statement required');
}

return $this->sql;
}

private function prepare(): void
{
$this->bindParams();
}

private function queryInternal(string $method)
{
$this->prepare();
$pdoStatement = $this->pdoStatement();
$result = $pdoStatement->$method(\PDO::FETCH_ASSOC);
$pdoStatement->closeCursor();

return $result;
}

private function bindParams(): void
{
$pdoStatement = $this->pdoStatement();
foreach ($this->params as $param => $value) {
$pdoStatement->bindParam($param, $value, $this->paramType($value));
}
}

private function paramType($value)
{
$type = \gettype($value);

return self::TYPE_MAP[$type] ?? \PDO::PARAM_STR;
}
}
32 changes: 32 additions & 0 deletions php/src/Db/Db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Db;

use PDO;

class Db
{
/**
* @var static|null
*/
private static ?Db $instance = null;
/**
* @var \PDO|null
*/
private ?PDO $pdo = null;

public static function i(): self
{
return self::$instance ?? (self::$instance = new static());
}

public static function command(string $sql, array $params = []): Command
{
return new Command($sql, $params);
}

public function pdo(): PDO
{
return $this->pdo ?? ($this->pdo = new PDO('mysql:dbname=db;host=127.0.0.1', 'dbuser', 'dbpass'));
}
}
Loading