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

Паттерны работы с данными #630

Open
wants to merge 2 commits into
base: VAdzhieva/main
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions hw12/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types = 1);

require __DIR__ . '/vendor/autoload.php';

$host = '';
$database = '';

try {
$user = new UserController($host, $database);
} catch (Exception $exception) {
echo $exception->getMessage();
}
48 changes: 48 additions & 0 deletions hw12/src/controller/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types = 1);

use Veraadzhieva\Hw12;
use Veraadzhieva\Hw12\Model\User;
use Veraadzhieva\Hw12\Model\UserMapper;

class UserController
{
private $mapper;

public function __construct($host, $database) {
$this->mapper = new UserMapper($host, $database);
}

public function insertUser($params) {
try {
$this->mapper->insert($params['username'], $params['phone']);
} catch (Exception $e) {
return $e->getMessage();
}
}

public function updateUser($params) {
try {
$this->mapper->update($params);
} catch (Exception $e) {
return $e->getMessage();
}
}

public function deleteUser(User $user) {
try {
$this->mapper->delete($user);
} catch (Exception $e) {
return $e->getMessage();
}
}

public function selectUserById($id) {
try {
$this->mapper->findById($id);
} catch (Exception $e) {
return $e->getMessage();
}
}
}
32 changes: 32 additions & 0 deletions hw12/src/model/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types = 1);

namespace Veraadzhieva\Hw12\Model;

class User
{
private $id;
private $username;
private $phone;

public function __construct($username, $phone) {
$this->username = $username;
$this->phone = $phone;
}

public function getId()
{
return $this->id;
}

public function getUsername()
{
return $this->username;
}

public function getPhone()
{
return $this->phone;
}
}
88 changes: 88 additions & 0 deletions hw12/src/model/UserMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types = 1);

namespace Veraadzhieva\Hw12\Model;

use Veraadzhieva\Hw12\Model\User;
use PDO;

class UserMapper
{
private $connection;

public function __construct($host, $database) {
$this->connection = new PDO("mysql:host=$host;dbname=$database");
}


/**
* Создание нового пользователя.
*
* @param string $username
* @param int $phone
*/
public function insert($username, $phone)
{
return new User($username, $phone);
}

/**
* Обновление пользователя.
*
* @param array $params
*/
public function update($params)
{
$user = $this->findById($params['id']);
if ($params['username'] && $params['username'] !== $user->getUsername()) {
$query = 'UPDATE users SET username=? WHERE id=?';
$this->connection->query($query,
array(
$params['username'],
$user->getId()
));
}
if ($params['phone'] && $params['phone'] !== $user->getPhone()) {
$query = 'UPDATE users SET phone=? WHERE id=?';
$this->connection->query($query,
array(
$params['phone'],
$user->getId()
));
}
}

/**
* Удаление пользователя из бд.
*
* @param User $user
*/
public function delete(User $user)
{
$query = 'DELETE FROM users WHERE id = ?';
$this->connection->query($query, array($user->getId()));
}

/**
* Поиск пользователя по id.
*
* @param int $userId
* @return User|null
*/
public function findById($userId)
{
$sql = "SELECT id, username, phone FROM users WHERE id = ?";

$data = $this->connection->fetchRow($sql, array($userId), Zend_Db::FETCH_ASSOC);

$user = null;

if ($data != false)
{
$user = $this->insert($data);
}

return $user;
}
}