Skip to content

Instantly share code, notes, and snippets.

@elmarputz
Created December 16, 2016 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elmarputz/1ae391e2892f921fa1c1026830a9a8a6 to your computer and use it in GitHub Desktop.
Save elmarputz/1ae391e2892f921fa1c1026830a9a8a6 to your computer and use it in GitHub Desktop.
DataManager.php
<?php
// mock data
$__categories = array(1 => new Category(1, "Mobile & Wireless Computing"),
2 => new Category(2, "Functional Programming"),
3 => new Category(3, "C / C++"),
4 => new Category(4, "<< New Publications >>"));
$__books = array(1 => new Book(1, 1, "Hello, Android:\nIntroducing Google's Mobile Development Platform", "Ed Burnette", 19.97),
2 => new Book(2, 1, "Android Wireless Application Development", "Shane Conder, Lauren Darcey", 31.22),
5 => new Book(5, 1, "Professional Flash Mobile Development", "Richard Wagner", 19.90),
7 => new Book(7, 1, "Mobile Web Design For Dummies", "Janine Warner, David LaFontaine", 16.32),
11 => new Book(11, 2, "Introduction to Functional Programming using Haskell", "Richard Bird", 74.75),
//book with bad title to show scripting attack - add for scripting attack demo only
12 => new Book(12, 2, "Scripting (Attacks) for Beginners - <script type=\"text/javascript\">alert('All your base are belong to us!');</script>", "John Doe", 9.99),
14 => new Book(14, 2, "Expert F# (Expert's Voice in .NET)", "Antonio Cisternino, Adam Granicz, Don Syme", 47.64),
16 => new Book(16, 3, "C Programming Language\n(2nd Edition)", "Brian W. Kernighan, Dennis M. Ritchie", 48.36),
27 => new Book(27, 3, "C++ Primer Plus\n(5th Edition)", "Stephan Prata", 36.94),
29 => new Book(29, 3, "The C++ Programming Language", "Bjarne Stroustrup", 67.49));
$__users = array(1 => new User(1, "scm4", "a8af855d47d091f0376664fe588207f334cdad22")); //USER = scm4; PASSWORD = scm4
//
// /mock data
/**
* DataManager
* Mock Version
*
*
* @package
* @subpackage
* @author John Doe <jd@fbi.gov>
*/
class DataManager {
/**
* get the categories
*
* note: global …; -> suboptimal
*
* @return array of Category-items
*/
public static function getCategories() {
global $__categories;
return $__categories;
}
/**
* get the books per category
*
* @param integer $categoryId numeric id of the category
* @return array of Book-items
*/
public static function getBooksForCategory($categoryId) {
global $__books;
$res = array();
foreach ($__books as $book) {
if ($book->getCategoryId() === (int) $categoryId) {
$res[] = $book;
}
}
return $res;
}
/**
* get the books per search term
*
* note: stripos() – returns index (also 0!) on success and -1 on error
* 0 == false but 0 === true -> thus test for !== false
*
* "This function may return Boolean FALSE, but may also return a
* non-Boolean value which evaluates to FALSE. Please read the section
* on Booleans for more information. Use the === operator for testing
* the return value of this function." -- http://php.net/manual/en/function.stripos.php
*
* @param string $term search term: book title string match
* @return array of Book-items
*/
public static function getBooksForSearchCriteria($term) {
global $__books;
$res = array();
foreach ($__books as $book) {
$termOk = (
$term == '' || stripos($book->getTitle(), $term) !== false
);
if ($termOk) {
$res[] = $book;
}
}
return $res;
}
/**
* get the books per search term – paginated set only
*
* @param string $term search term: book title string match
* @param integer $offset start at the nth item
* @param integer $numPerPage number of items per page
* @return array of Book-items
*/
public static function getBooksForSearchCriteriaWithPaging($term, $offset, $numPerPage) {
$res = self::getBooksForSearchCriteria($term);
return new PagingResult(array_slice($res, $offset, $numPerPage), $offset, sizeof($res));
}
/**
* get the User item by id
*
* @param integer $userId uid of that user
* @return User | false
*/
public static function getUserForId($userId) {
global $__users;
return array_key_exists($userId, $__users) ? $__users[$userId] : null;
}
/**
* get the User item by name
*
* note: show for case sensitive and insensitive options
*
* @param string $userName name of that user - must be exact match
* @return User | false
*/
public static function getUserForUserName($userName) {
global $__users;
foreach ($__users as $u) {
if ($u->getUserName() == $userName) {
return $u;
}
}
return null;
}
/**
* place to order with the shopping cart items
*
* note: nothing to do here without the database
* random order id can be used to demonstrate that POST is only executed once
*
* @param integer $userId id of the ordering user
* @param array $bookIds integers of book ids
* @param string $nameOnCard cc name
* @param string $cardNumber cc number
* @return integer
*/
public static function createOrder($userId, array $bookIds, $nameOnCard, $cardNumber) {
return rand();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment