Skip to content

Instantly share code, notes, and snippets.

@hopeseekr
Forked from cythrawll/Author.php
Created July 18, 2012 14:37
Show Gist options
  • Save hopeseekr/3136563 to your computer and use it in GitHub Desktop.
Save hopeseekr/3136563 to your computer and use it in GitHub Desktop.
Example of three layered Service
<?php
class Author {
public $name;
public $penNames;
}
<?php
class AuthorDao extends Dao {
public function getBooks($id) {
$stmt = $this->db->prepare('...');
$stmt->execute(array($id));
return SomeMapper::mapResultToBooks($stmt);
}
public function updateAuthor(Author $author) {
$stmt = $this->db->prepare('..');
$stmt->execute(array($author->name, $author->penNames));
}
}
<?php
// Copyright (c) 2012 Theodore R.Smith (theodore@phpexperts.pro)
// DSA-1024 Fingerprint: 10A0 6372 9092 85A2 BB7F 907B CB8B 654B E33B F1ED
// Provided by the PHP University (www.phpu.cc) and PHPExperts.pro (www.phpexperts.pro)
//
// This file is dually licensed under the terms of the following licenses:
// * Primary License: OSSAL v1.0 - Open Source Software Alliance License
// * Key points:
// 5.Redistributions of source code in any non-textual form (i.e.
// binary or object form, etc.) must not be linked to software that is
// released with a license that requires disclosure of source code
// (ex: the GPL).
// 6.Redistributions of source code must be licensed under more than one
// license and must not have the terms of the OSSAL removed.
// * See http://people.freebsd.org/~seanc/ossal/ for complete details.
//
// * Secondary License: Creative Commons Attribution License v3.0
// * Key Points:
// * You are free:
// * to copy, distribute, display, and perform the work
// * to make non-commercial or commercial use of the work in its original form
// * Under the following conditions:
// * Attribution. You must give the original author credit. You must retain all
// Copyright notices and you must include the sentence, "Based upon work from
// PHPExperts.pro (www.phpexperts.pro).", wherever you list contributors.
// * See http://creativecommons.org/licenses/by/3.0/us/ for complete details.
class AuthorManager
{
/** @var \DataAccessLayer */
protected $dal;
public function __construct(DataAccessLayer $dal)
{
$this->dal = $dal;
}
public function add($name, array $penNames)
{
$author = new Author;
$author->name = $name;
$author->penNames = $penNames;
$this->dal->add('authors', 'name=' . $name, $author);
}
public function get($name)
{
$this->dal->get('authors', 'name=' . $name);
}
public function update(Author $author)
{
$this->dal->update('authors', 'name=' . $author->name, $author);
}
public function delete($name)
{
$this->dal->delete('authors', 'name=' . $name);
}
}
<?php
interface AuthorService {
public function getBooksByAuthor($id);
public function updateAuthor(Author $author);
}
class AuthorServiceDaoImpl implements AuthorService {
private $authorDao;
public function getAuthorDao(AuthorDao $dao) {
$this->authorDao = $authorDao;
}
public function getBooks($id) {
return $this->authorDao->getBooks($id);
}
public function updateAuthor(Author $author) {
try {
$this->authorDao->getDB()->startTransaction();
$this->authorDao->updateAuthor($author);
$this->authorDao->commit();
} catch(Exception $ex) {
$this->authorDao->getDB()->rollback();
throw $ex;
}
}
}
<?php
//omitting setting exception mode, don't forget it!
$db = new PDO(...);
$authorDao = new AuthorDao($db);
$authorService = new AuthorServiceDaoImpl($authorDao);
$authorService->getBooks($author->getPrimaryKey());
//tip: you can simplify construction by using a Builder pattern, or a Factory pattern if you want to handle more than one implementation of the service.
<?php
class Dao {
private $db;
public function __construct(PDO $db) {
$this->db = $db;
}
public function getDB() {
return $this->db;
}
}
<?php
// Copyright (c) 2012 Theodore R.Smith (theodore@phpexperts.pro)
// DSA-1024 Fingerprint: 10A0 6372 9092 85A2 BB7F 907B CB8B 654B E33B F1ED
// Provided by the PHP University (www.phpu.cc) and PHPExperts.pro (www.phpexperts.pro)
//
// This file is dually licensed under the terms of the following licenses:
// * Primary License: OSSAL v1.0 - Open Source Software Alliance License
// * Key points:
// 5.Redistributions of source code in any non-textual form (i.e.
// binary or object form, etc.) must not be linked to software that is
// released with a license that requires disclosure of source code
// (ex: the GPL).
// 6.Redistributions of source code must be licensed under more than one
// license and must not have the terms of the OSSAL removed.
// * See http://people.freebsd.org/~seanc/ossal/ for complete details.
//
// * Secondary License: Creative Commons Attribution License v3.0
// * Key Points:
// * You are free:
// * to copy, distribute, display, and perform the work
// * to make non-commercial or commercial use of the work in its original form
// * Under the following conditions:
// * Attribution. You must give the original author credit. You must retain all
// Copyright notices and you must include the sentence, "Based upon work from
// PHPExperts.pro (www.phpexperts.pro).", wherever you list contributors.
// * See http://creativecommons.org/licenses/by/3.0/us/ for complete details.
interface DAL_Driver
{
public function getPrimaryActor();
public function add($namespace, $key, $value);
public function get($namespace, $key);
public function update($namespace, $key, $value);
public function delete($namespace, $key);
}
// Uses the Composite design pattern.
class DataAccessLayer implements DAL_Driver
{
/** @var DAL_Driver */
protected $driver;
public function __construct(DAL_Driver $driver)
{
$this->driver = $driver;
}
public function getPrimaryActor()
{
return $this->driver->getPrimaryActor();
}
public function add($namespace, $key, $value)
{
return $this->driver->add($namespace, $key, $value);
}
public function get($namespace, $key)
{
return $this->driver->get($namespace, $key);
}
public function update($namespace, $key, $value)
{
return $this->driver->update($namespace, $key, $value);
}
public function delete($namespace, $key)
{
return $this->driver->delete($namespace, $key);
}
}
<?php
class Entity {
private $primaryKey;
public getPrimaryKey() {
return $this->primaryKey;
}
public setPrimaryKey($primaryKey) {
$this->primaryKey = $primaryKey;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment