/** * gets the Favorite by tweet it id * * @param \PDO $pdo PDO connection object * @param int $tweetId tweet id to search for * @return \SplFixedArray array of Favorites found or null if not found * @throws \PDOException when mySQL related errors occur * @throws \TypeError when variables are not the correct data type **/ public static function getFavoriteByTweetId(\PDO $pdo, int $tweetId) { // sanitize the tweet id $tweetId = filter_var($tweetId, FILTER_VALIDATE_INT); if ($tweetId <= 0) { throw new \PDOException("tweet id is not positive"); } // create query template $query = "SELECT profileId, tweetId, favoriteDate FROM favorite WHERE tweetId = :tweetId"; $statement = $pdo->prepare($query); // bind the member variables to the place holders in the template $parameters = ["tweetId" => $tweetId]; $statement->execute($parameters); // build an array of favorites $favorites = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $favorite = new Favorite($row["tweetId"], $row["profileId"], $row["favoriteDate"]); $favorites[$favorites->key()] = $favorite; $favorites->next(); } catch (\Exception $exception) { // if the row couldn't be converted, rethrow it throw new \PDOException($exception->getMessage(), 0, $exception); } } return $favorites; }
/** * function to store multiple database results into an SplFixedArray * * @param PDOStatement $statement pdo statement object * @return SPLFixedArray all message obtained from database * @throws PDOException if mySQL related errors occur */ public static function storeSQLResultsInArray(PDOStatement $statement) { //build an array of messages, as an SPLFixedArray object //set the size of the object to the number of retrieved rows $retrievedMessages = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); //while rows can still be retrieved from the result while (($row = $statement->fetch()) !== false) { try { $message = new Message($row["messageId"], $row["listingId"], $row["orgId"], $row["messageText"]); //place result in the current field, then advance the key $retrievedMessages[$retrievedMessages->key()] = $message; $retrievedMessages->next(); } catch (Exception $exception) { //rethrow the exception if retrieval failed throw new PDOException($exception->getMessage(), 0, $exception); } } return $retrievedMessages; }
<?php $array = new SplFixedArray(3); $array[0] = "Hello"; $array[1] = "world"; $array[2] = "elePHPant"; foreach ($array as $value) { echo $array->key(array("this", "should", "not", "execute")); }
/** * getAllImages * * @param \PDO $pdo PDO connection object * @return \SplFixedArray SplFixedArray of images found or null if not found * @throws \PDOException when mySQL related errors occur * @throws \TypeError when variables are not the correct data type **/ public static function getAllImages(\PDO $pdo) { // create query template $query = "SELECT imageId, imageProfileId, imageFileName, imageType FROM image"; $statement = $pdo->prepare($query); $statement->execute(); // build an array of images $images = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $image = new Image($row["imageId"], $row["imageProfileId"], $row["imageFileName"], $row["imageType"]); $images[$images->key()] = $image; $images->next(); } catch (\Exception $exception) { // if the row couldn't be converted, rethrow it throw new \PDOException($exception->getMessage(), 0, $exception); } } return $images; }
/** * gets all ratings * * @param PDO $pdo PDO connection object * @return SplFixedArray all ratings found * @throws PDOException when mySQL errors occur */ public static function getAllRatings(PDO $pdo) { //create query template $query = "select trailId, userId, rating FROM rating"; $statement = $pdo->prepare($query); $statement->execute(); // build an array of rating values $ratings = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $rating = new Rating($row["userId"], $row["trailId"], $row["ratingValue"]); $ratings[$ratings->key()] = $rating; $ratings->next(); } catch (Exception $exception) { // if the row couldn't be converted rethrow it throw new PDOException($exception->getMessage(), 0, $exception); } } return $ratings; }
public function key() { echo "A::key\n"; return parent::key(); }
/** * gets segment by segmentStopElevation * * @param PDO $pdo pointer to PDO connection * @param int $segmentStopElevation stop elevation to trail-search for * @return mixed segment found or null if not found * @throws PDOException when mySQL related errors occur * @throws RangeException when range is invalid * @throws Exception for other exception */ public static function getSegmentBySegmentStopElevation(PDO &$pdo, $segmentStopElevation) { //sanitize the int before searching try { $segmentStopElevation = Filter::filterInt($segmentStopElevation, "segment stop", false); } catch (InvalidArgumentException $invalidArgument) { throw new PDOException($invalidArgument->getMessage(), 0, $invalidArgument); } catch (RangeException $range) { throw new RangeException($range->getMessage(), 0, $range); } catch (Exception $exception) { throw new Exception($exception->getMessage(), 0, $exception); } //create query template $query = "SELECT segmentId, ST_AsWKT(segmentStop) AS segmentStop, ST_AsWKT(segmentStart) AS segmentStart, segmentStartElevation, segmentStopElevation FROM segment WHERE segmentStopElevation = :segmentStopElevation"; $statement = $pdo->prepare($query); //binds segmentStopElevation to placeholder $parameters = array("segmentStopElevation" => $segmentStopElevation); $statement->execute($parameters); //build an array of segments $segments = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $segmentStartJSON = Gisconverter::wktToGeojson($row["segmentStart"]); $segmentStopJSON = Gisconverter::wktToGeojson($row["segmentStop"]); $segmentStartGenericObject = json_decode($segmentStartJSON); $segmentStopGenericObject = json_decode($segmentStopJSON); $segmentStart = new Point($segmentStartGenericObject->coordinates[0], $segmentStartGenericObject->coordinates[1]); $segmentStop = new Point($segmentStopGenericObject->coordinates[0], $segmentStopGenericObject->coordinates[1]); $segment = new Segment($row["segmentId"], $segmentStart, $segmentStop, $row["segmentStartElevation"], $row["segmentStopElevation"]); $segments[$segments->key()] = $segment; $segments->next(); } catch (Exception $e) { //if the row couldn't be converter, rethrow it throw new PDOException($e->getMessage(), 0, $e); } } return $segments; }
/** * gets all Tweets * * @param PDO $pdo pointer to PDO connection, by reference * @return mixed SplFixedArray of Tweets found or null if not found * @throws PDOException when mySQL related errors occur **/ public static function getAllTweets(PDO &$pdo) { // create query template $query = "SELECT tweetId, profileId, tweetContent, tweetDate FROM tweet"; $statement = $pdo->prepare($query); $statement->execute(); // build an array of tweets $tweets = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $tweet = new Tweet($row["tweetId"], $row["profileId"], $row["tweetContent"], $row["tweetDate"]); $tweets[$tweets->key()] = $tweet; $tweets->next(); } catch (Exception $exception) { // if the row couldn't be converted, rethrow it throw new PDOException($exception->getMessage(), 0, $exception); } } // count the results in the array and return: // 1) null if 0 results // 2) the entire array if >= 1 result $numberOfTweets = count($tweets); if ($numberOfTweets === 0) { return null; } else { return $tweets; } }
/** * retrieves all listingtypes * * @param PDO $pdo pdo connection object * @return SplFixedArray all organizations * @throws PDOException if mySQL errors occur */ public static function getAllListingTypes(PDO $pdo) { //create query template and execute $query = "SELECT listingTypeId, listingType FROM listingType"; $statement = $pdo->prepare($query); $statement->execute(); //build an array of the retrieved results //set the size of the object to the number of retrieved rows $retrievedTypes = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); //while rows can still be retrieved from the result while (($row = $statement->fetch()) !== false) { try { $listingType = new ListingType($row["listingTypeId"], $row["listingType"]); //place result in the current field, then advance the key $retrievedTypes[$retrievedTypes->key()] = $listingType; $retrievedTypes->next(); } catch (Exception $exception) { //rethrow the exception if retrieval failed throw new PDOException($exception->getMessage(), 0, $exception); } } return $retrievedTypes; }
/** * gets all ProductLocations * * @param PDO $pdo pointer to PDO connection, by reference * @return SplFixedArray all productLocations found * @throws PDOException when mySQL related errors occur **/ public static function getAllProductLocations(PDO &$pdo) { // create query template $query = "SELECT locationId, productId, unitId, quantity FROM productLocation"; $statement = $pdo->prepare($query); $statement->execute(); // build an array of ProductLocation(s) $productLocations = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $productLocation = new ProductLocation($row["locationId"], $row["productId"], $row["unitId"], $row["quantity"]); $productLocations[$productLocations->key()] = $productLocation; $productLocations->next(); } catch (PDOException $exception) { // if the row couldn't be converted, rethrow it throw new PDOException($exception->getMessage(), 0, $exception); } } return $productLocations; }
/** * gets all Game * * @param \PDO $pdo PDO connection object * @return \SplFixedArray SplFixedArray of Game found or null if not found * @throws \PDOException when mySQL related errors occur * @throws \TypeError when variables are not the correct data type **/ public static function getAllGame(\PDO $pdo) { //create query template $query = "SELECT gameId, gameFirstTeamId, gameSecondTeamId, gameTime FROM game "; $statement = $pdo->prepare($query); $statement->execute(); //build an array of game $games = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $game = new Game($row["gameId"], $row["gameFirstTeamId"], $row["gameSecondTeamId"], $row["gameTime"]); $games[$games->key()] = $game; $games->next(); } catch (\Exception $exception) { // if the row couldn't be converted rethrow it throw new \PDOException($exception->getMessage(), 0, $exception); } } return $games; }
public static function getAllProfiles(\PDO $pdo) { // Create query template and execute $query = "SELECT profileId, profileAdmin, profileNameFirst, profileNameLast, profileEmail, profileUserName, profileSalt, profileHash, profileActivationToken FROM profile"; $statement = $pdo->prepare($query); $statement->execute(); // Build an array of matches $profiles = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $profile = new Profile($row["profileId"], $row["profileAdmin"], $row["profileNameFirst"], $row["profileNameLast"], $row["profileEmail"], $row["profileUserName"], $row["profileSalt"], $row["profileHash"], $row["profileActivationToken"]); $profiles[$profiles->key()] = $profile; $profiles->next(); } catch (\Exception $exception) { throw new \PDOException($exception->getMessage(), 0, $exception); } } return $profiles; }
/** * gets the Follower relationship by the person being followed * * @param \PDO $pdo PDO connection object * @param int $followerFollowedId the person being followed * @return \SplFixedArray SplFixedArray of Follower relationships found or null if not found * @throws \PDOException when mySQL related errors occur * @throws \TypeError when variables are not the correct data type **/ public static function getFollowerByFollowedId(\PDO $pdo, int $followerFollowedId) { //Sanitize the followed id if ($followerFollowedId <= 0) { throw new \PDOException("Get Follower by Followed: Followed Id is not positive"); } //Create a query template $query = "SELECT followerFollowerId, followerFollowedId FROM follower WHERE followerFollowedId = :followerFollowedId"; $statement = $pdo->prepare($query); //Search based on the one being followed $parameters = ["followerFollowedId" => $followerFollowedId]; $statement->execute($parameters); //Build an array of follower relationships $followers = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $follower = new Follower($row["followerFollowerId"], $row["followerFollowedId"]); $followers[$followers->key()] = $follower; $followers->next(); } catch (\Exception $exception) { //If the row couldn't be converted, rethrow it throw new \PDOException($exception->getMessage(), 0, $exception); } } return $followers; }
/** * gets the list of this profiles favorite teams * * @param \PDO $pdo connection object * @param int $allFavoriteTeams search for * @return \SplFixedArray SplFixedArray of all teams favorited found * @throws \PDOException when db related errors occur * @throws \TypeError when variables are not correct data type **/ public static function getFavoriteTeamsByFavoriteTeamProfileId(\PDO $pdo, int $favoriteTeamProfileId) { // sanitize the favoriteTeamProfileId if ($favoriteTeamProfileId <= 0) { throw new \PDOException("that profile doesnt exist"); } // create a query template $query = "SELECT favoriteTeamProfileId, favoriteTeamTeamId FROM favoriteTeam WHERE favoriteTeamProfileId = :favoriteTeamProfileId"; $statement = $pdo->prepare($query); // bind the favoriteTeamTeamId to the place holder in the template $parameters = ["favoriteTeamProfileId" => $favoriteTeamProfileId]; $statement->execute($parameters); // build an array of favorite teams $favoriteTeams = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $favoriteTeam = new FavoriteTeam($row["favoriteTeamProfileId"], $row["favoriteTeamTeamId"]); $favoriteTeams[$favoriteTeams->key()] = $favoriteTeam; $favoriteTeams->next(); } catch (\Exception $exception) { // if the row can't be converted, rethrow it throw new \PDOException($exception->getMessage(), 0, $exception); } } return $favoriteTeams; }
public static function getTrailByTrailUuid(PDO &$pdo, $trailUuid) { //sanitize the trailUuid before searching try { $trailUuid = Filter::filterString($trailUuid, "trailUuid"); } catch (InvalidArgumentException $invalidArgument) { throw new PDOException($invalidArgument->getMessage(), 0, $invalidArgument); } catch (RangeException $range) { throw new PDOException($range->getMessage(), 0, $range); } catch (Exception $exception) { throw new PDOException($exception->getMessage(), 0, $exception); } //create query template $query = "SELECT trailId, userId, browser, createDate, ipAddress, submitTrailId, trailAmenities, trailCondition,trailDescription, trailDifficulty, trailDistance, trailName, trailSubmissionType,\ntrailTerrain, trailTraffic, trailUse, trailUuid FROM trail WHERE trailUuid = :trailUuid"; $statement = $pdo->prepare($query); //bind trailUuid to placeholder $parameters = array("trailUuid" => $trailUuid); $statement->execute($parameters); //build an array of trails $trails = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { //new trail ($trailId, $userId, $submitTrailId, $browser, $createDate, $ipAddress, $trailAccessibility, $trailAmenities, $trailCondition,$trailDescription, $trailDifficulty, $trailDistance, $trailSubmissionType,$trailTerrain, $trailName, $trailTraffic, $trailUse, $trailUuId) $trail = new Trail($row["trailId"], $row["userId"], $row["browser"], $row["createDate"], $row["ipAddress"], $row["submitTrailId"], $row["trailAmenities"], $row["trailCondition"], $row["trailDescription"], $row["trailDifficulty"], $row["trailDistance"], $row["trailName"], $row["trailSubmissionType"], $row["trailTerrain"], $row["trailTraffic"], $row["trailUse"], $row["trailUuid"]); $trails[$trails->key()] = $trail; $trails->next(); } catch (Exception $e) { //if the row couldn't be converted, rethrow it throw new PDOException($e->getMessage(), 0, $e); } } return $trails; }
/** * gets all Statistic * * @param \PDO $pdo PDO connection object * @return \SplFixedArray SplFixedArray of Statistic found or null if not found * @throws \PDOException when mySQL related errors occur * @throws \TypeError when variables are not the correct data type **/ public static function getAllStatistic(\PDO $pdo) { // create query template $query = "SELECT statisticId, statisticName FROM statistic"; $statement = $pdo->prepare($query); $statement->execute(); //build array of statistic $statistics = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $statistic = new Statistic($row["statisticId"], $row["statisticName"]); $statistics[$statistics->key()] = $statistic; $statistics->next(); } catch (\Exception $exception) { //if the row couldn't be converted rethrow it throw new \PDOException($exception->getMessage(), 0, $exception); } } return $statistics; }
public static function getAllPosts(\PDO $pdo) { // Create query template and execute $query = "SELECT postId, postProfileUserName, postSubmission, postTime FROM post"; $statement = $pdo->prepare($query); $statement->execute(); // Build an array of matches $posts = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $post = new Post($row["postId"], $row["postProfileUserName"], $row["postSubmission"], \DateTime::createFromFormat("Y-m-d H:i:s", $row["postTime"])); $posts[$posts->key()] = $post; $posts->next(); } catch (\Exception $exception) { throw new \PDOException($exception->getMessage(), 0, $exception); } } return $posts; }
/** * gets all passwordResets * * @param \PDO $pdo PDO connection object * @return \SplFixedArray SplFixedArray of PasswordResets found or null if not found * @throws \PDOException when mySQL related errors occur * @throws \TypeError when variables are not the correct data type **/ public static function getAllPasswordResets(\PDO $pdo) { // create query template $query = "SELECT passwordResetId, passwordResetProfileId, passwordResetProfileEmail, passwordResetToken, passwordResetTime FROM passwordReset"; $statement = $pdo->prepare($query); $statement->execute(); // build an array of passwordResets $passwordResets = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $passwordReset = new PasswordReset($row["passwordResetId"], $row["passwordResetProfileId"], $row["passwordResetProfileEmail"], $row["passwordResetToken"], \DateTime::createFromFormat("Y-m-d H:i:s", $row["passwordResetTime"])); $passwordResets[$passwordResets->key()] = $passwordReset; $passwordResets->next(); } catch (\Exception $exception) { // if the row couldn't be converted, rethrow it throw new \PDOException($exception->getMessage(), 0, $exception); } } return $passwordResets; }
/** * gets all sport Names * @param \PDO $pdo PDO connection object * @return \SplFixedArray SplFixedArray of sports found or null if nothing was found * @throws \PDOException when db related errors occur * @throws \TypeError when variables are not the correct data type **/ public static function getAllSportNames(\PDO $pdo) { // create a query template $query = "SELECT sportId, sportLeague, sportName FROM sport"; $statement = $pdo->prepare($query); $statement->execute(); // build an array of sports $allSportNames = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $sport = new Sport($row["sportId"], $row["sportLeague"], $row["sportName"]); $allSportNames[$allSportNames->key()] = $sport; } catch (\Exception $exception) { //if the row can't be converted, rethrow it throw new \PDOException($exception->getMessage(), 0, $exception); } } return $allSportNames; }
/** * * Decodes geoJson file, converts to string, sifts through the string and inserts the data into the database * * @param string $url * @throws PDOException PDO related errors * @throws Exception catch-all exception **/ public static function readTrailSegmentsGeoJson($url) { $context = stream_context_create(array("http" => array("ignore_errors" => true, "method" => "GET"))); try { $pdo = connectToEncryptedMySQL("/var/www/trailquail/encrypted-mysql/trailquail.ini"); if (($jsonData = file_get_contents($url, null, $context)) !== false) { if (($jsonFd = @fopen("php://memory", "wb+")) === false) { throw new RuntimeException("Memory Error: I can't remember"); } //decode the geoJson file $jsonConverted = json_decode($jsonData); $jsonFeatures = $jsonConverted->features; // create array from converted geoJson file $properties = new SplFixedArray(count($jsonFeatures)); //loop through array to get to json properties foreach ($jsonFeatures as $jsonFeature) { $properties[$properties->key()] = $jsonFeature->properties; $properties->next(); } //create an array of trails and a SplObjectStorage for the trail guide $trails = []; $trailGuide = new SplObjectStorage(); //loop through array to get trail name and trail use foreach ($properties as $property) { $trailName = $property->name; $trailUse = ""; //set $trailUse string based on information in geoJson properties field. if ($property->bicycle === "yes") { $trailUse = $trailUse . "bicycle: yes, "; } else { $trailUse = $trailUse . "bicycle: no, "; } if ($property->foot === "yes") { $trailUse = $trailUse . "foot: yes, "; } else { $trailUse = $trailUse . "foot: no, "; } if ($property->wheelchair === "yes") { $trailUse = $trailUse . "wheelchair: yes "; } else { $trailUse = $trailUse . "wheelchair: no "; } //get the trail by name and set the trail use try { $candidateTrails = Trail::getTrailByTrailName($pdo, $trailName); foreach ($candidateTrails as $trail) { $trail->setTrailUse($trailUse); $trail->update($pdo); $trails[] = $trail; } } catch (PDOException $pdoException) { $sqlStateCode = "23000"; $errorInfo = $pdoException->errorInfo; if ($errorInfo[0] === $sqlStateCode) { } else { throw new PDOException($pdoException->getMessage(), 0, $pdoException); } } catch (Exception $exception) { throw new Exception($exception->getMessage(), 0, $exception); } } try { $trailIndex = 0; foreach ($jsonFeatures as $jsonFeature) { $jsonCoordinates = $jsonFeature->geometry->coordinates; if ($jsonFeature->geometry->type === "LineString") { $coordinates = new SplFixedArray(count($jsonCoordinates)); foreach ($jsonCoordinates as $coordinate) { $coordinates[$coordinates->key()] = $coordinate; $coordinates->next(); } $trailGuide[$trails[$trailIndex]] = $coordinates; $trailIndex++; } else { if ($jsonFeature->geometry->type === "MultiLineString") { $trailClones = []; $trails[$trailIndex]->delete($pdo); for ($i = 1; $i <= count($jsonCoordinates); $i++) { $trail = clone $trails[$trailIndex]; $trail->setTrailId(null); $trail->setTrailName($trail->getTrailName() . " {$i}"); $trail->insert($pdo); $trailClones[] = $trail; } array_splice($trails, $trailIndex, 1, $trailClones); foreach ($jsonCoordinates as $lineCoordinates) { $trailGuide[$trails[$trailIndex]] = $lineCoordinates; $trailIndex++; } } } } $trailGuide->rewind(); foreach ($trailGuide as $map) { $trail = $trailGuide->current(); $geo = $trailGuide->getInfo(); for ($indexTwo = 0; $indexTwo < count($geo) - 1; $indexTwo++) { $segmentStartX = $geo[$indexTwo][0]; $segmentStartY = $geo[$indexTwo][1]; // $segmentStartElevation = $geo[$indexTwo][2]; $segmentStopX = $geo[$indexTwo + 1][0]; $segmentStopY = $geo[$indexTwo + 1][1]; // $segmentStopElevation = $geo[$indexTwo + 1][2]; $segmentStart = new Point($segmentStartX, $segmentStartY); $segmentStop = new Point($segmentStopX, $segmentStopY); try { $segment = new Segment(null, $segmentStart, $segmentStop, 0, 0); $segment->insert($pdo); $relationship = new TrailRelationship($segment->getSegmentId(), $trail->getTrailId(), "T"); $relationship->insert($pdo); } catch (PDOException $pdoException) { $sqlStateCode = "23000"; $errorInfo = $pdoException->errorInfo; if ($errorInfo[0] === $sqlStateCode) { } else { throw new PDOException($pdoException->getMessage(), 0, $pdoException); } } catch (Exception $exception) { throw new Exception($exception->getMessage(), 0, $exception); } } } } catch (PDOException $pdoException) { $sqlStateCode = "23000"; $errorInfo = $pdoException->errorInfo; if ($errorInfo[0] === $sqlStateCode) { } else { throw new PDOException($pdoException->getMessage(), 0, $pdoException); } } catch (Exception $exception) { throw new Exception($exception->getMessage(), 0, $exception); } } fclose($jsonFd); } catch (PDOException $pdoException) { throw new PDOException($pdoException->getMessage(), 0, $pdoException); } catch (Exception $exception) { throw new Exception($exception->getMessage(), 0, $exception); } }
/** * Gets all profiles * * @param PDO $pdo pointer to PDO connection, by reference * @return SplFixedArray of Profiles found * @throws PDOException when MySQL related errors occur */ public static function getAllProfiles(PDO &$pdo) { // Create query template $query = "SELECT profileId, username, passwordHash FROM profile"; $statement = $pdo->prepare($query); $statement->execute(); // Build an array of profiles $profiles = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $profile = new Profile($row["profileId"], $row["username"], $row["passwordHash"]); $profiles[$profiles->key()] = $profile; $profiles->next(); } catch (Exception $e) { // If the row couldn't be converted, rethrow it throw new PDOException($e->getMessage(), 0, $e); } } return $profiles; }
/** * gets all FinishedProduct(s) * * @param PDO $pdo pointer to PDO connection, by reference * @return SplFixedArray all FinishedProducts found * @throws PDOException when mySQL related errors occur **/ public static function getAllFinishedProducts(PDO &$pdo) { // create query template $query = "SELECT finishedProductId, rawMaterialId, rawQuantity FROM finishedProduct"; $statement = $pdo->prepare($query); $statement->execute(); // build an array of FinishedProduct(s) $finishedProducts = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $finishedProduct = new FinishedProduct($row["finishedProductId"], $row["rawMaterialId"], $row["rawQuantity"]); $finishedProducts[$finishedProducts->key()] = $finishedProduct; $finishedProducts->next(); } catch (PDOException $exception) { // if the row couldn't be converted, rethrow it throw new PDOException($exception->getMessage(), 0, $exception); } } return $finishedProducts; }
/** * gets all Players by teamId * * @param \PDO $pdo PDO connection object * @return \SplFixedArray SplFixedArray of players found or null if not found * @throws \PDOException when mySQL related errors occur * @throws \TypeError when variables are not the correct data type **/ public static function getAllPlayersByTeamId(\PDO $pdo, int $playerTeamId) { $query = "SELECT playerId, playerApiId, playerTeamId, playerSportId, playerName FROM player WHERE playerTeamId = :playerTeamId"; $statement = $pdo->prepare($query); $parameters = ["playerTeamId" => $playerTeamId]; $statement->execute($parameters); // build an array of Players $players = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $player = new Player($row["playerId"], $row["playerApiId"], $row["playerTeamId"], $row["playerSportId"], $row["playerName"]); $players[$players->key()] = $player; $players->next(); } catch (\Exception $exception) { // if the row couldn't be converted, rethrow it throw new \PDOException($exception->getMessage(), 0, $exception); } } return $players; }
/** * gets all Schedules * * @param \PDO $pdo PDO connection object * @return \SplFixedArray SplFixedArray of Schedules found or null if not found * @throws \PDOException when mySQL related errors occur * @throws \TypeError when variables are not the correct data type **/ public static function getAllSchedules(\PDO $pdo) { // create query template $query = "SELECT scheduleId, scheduleCrewId, scheduleStartDate\n\t\t\t\t\t FROM schedule\n\t\t\t\t\t WHERE schedule.scheduleCrewId\n\t\t\t\t\t IN (SELECT crewId FROM crew WHERE crewCompanyId = :companyId)"; $statement = $pdo->prepare($query); $parameters = ["companyId" => self::injectCompanyId()]; $statement->execute($parameters); // build an array of schedules $schedules = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $schedule = new Schedule($row["scheduleId"], $row["scheduleCrewId"], $row["scheduleStartDate"]); $schedules[$schedules->key()] = $schedule; $schedules->next(); } catch (\Exception $exception) { // if the row couldn't be converted, rethrow it throw new \PDOException($exception->getMessage(), 0, $exception); } } return $schedules; }
/** * getProductAlertByProductId * * @param PDO $pdo pointer to PDO connection, by reference * @param int $productId * @return mixed ProductAlert * @throws PDOException if productId is not an integer * @throws PDOException if productId is not positive **/ public static function getProductAlertByProductId(PDO &$pdo, $productId) { // sanitize the productId before searching $productId = filter_var($productId, FILTER_VALIDATE_INT); if ($productId === false) { throw new PDOException("productId is not an integer"); } if ($productId <= 0) { throw new PDOException("productId is not positive"); } //create query template $query = "SELECT alertId, productId, alertEnabled FROM productAlert WHERE productId = :productId"; $statement = $pdo->prepare($query); //bind the productId to the place holder in the template $parameters = array("productId" => $productId); $statement->execute($parameters); // build an array of ProductAlert(s) $productAlerts = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $productAlert = new ProductAlert($row["alertId"], $row["productId"], $row["alertEnabled"]); $productAlerts[$productAlerts->key()] = $productAlert; $productAlerts->next(); } catch (PDOException $exception) { // if the row couldn't be converted, rethrow it throw new PDOException($exception->getMessage(), 0, $exception); } } return $productAlerts; }
/** * gets all ram chips * * @param PDO $pdo PDO connection object * @return SplFixedArray all RamChips found * @throws PDOException when mySQL related errors occur **/ public static function getAllRamChips(\PDO $pdo) { // create query template $query = "SELECT productId, productName, manufacturerName, modelName, price FROM ramChip"; $statement = $pdo->prepare($query); $statement->execute(); // build an array of ram chips $ramChips = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $ramChip = new RamChip($row["productId"], $row["productName"], $row["manufacturerName"], $row["modelName"], $row["price"]); $ramChips[$ramChips->key()] = $ramChip; $ramChip->next(); } catch (Exception $exception) { // if the row couldn't be converted, rethrow it throw new PDOException($exception->getMessage(), 0, $exception); } } return $ramChips; }
/** * get a favorite player by profile id * * @param \PDO $pdo PDO connection object * @param int $profileId * @return \SplFixedArray SplFixedArray of Favorite Players found or null if not found * @throws \PDOException when mySQL related errors occur * @throws \TypeError when variables are not the correct data type **/ public static function getFavoritePlayersByFavoritePlayerProfileId(\PDO $pdo, int $favoritePlayerProfileId) { if ($favoritePlayerProfileId <= 0) { throw new \PDOException("this profile doesn't exist"); } // create query template $query = "SELECT favoritePlayerProfileId, favoritePlayerPlayerId FROM favoritePlayer WHERE favoritePlayerProfileId = :favoritePlayerProfileId"; $statement = $pdo->prepare($query); $parameters = ["favoritePlayerProfileId" => $favoritePlayerProfileId]; $statement->execute($parameters); // build an array of favorite players $favoritePlayers = new \SplFixedArray($statement->rowCount()); $statement->setFetchMode(\PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $favoritePlayer = new FavoritePlayer($row["favoritePlayerProfileId"], $row["favoritePlayerPlayerId"]); $favoritePlayers[$favoritePlayers->key()] = $favoritePlayer; $favoritePlayers->next(); } catch (\Exception $exception) { // if the row couldn't be converted, rethrow it throw new \PDOException($exception->getMessage(), 0, $exception); } } return $favoritePlayers; }
/** * @return int */ public function key() { return $this->set->key(); }
/** * gets a Trail Relationship by trailId * * @param PDO $pdo PDO connection object * @param int $trailId trail id to trail-search for * @return mixed Trail Relationship found or null if not found * @throws PDOException when mySQL related errors occur **/ public static function getTrailRelationshipByTrailId(PDO $pdo, $trailId) { // sanitize the trailId before searching $trailId = filter_var($trailId, FILTER_VALIDATE_INT); if ($trailId === false) { throw new PDOException("trailId is not an integer"); } if ($trailId <= 0) { throw new PDOException("trailId is not positive"); } //create query template $query = "SELECT segmentId, trailId, segmentType FROM trailRelationship WHERE trailId = :trailId"; $statement = $pdo->prepare($query); //bind the trailId to the placeholder in the template $parameters = array("trailId" => $trailId); $statement->execute($parameters); // build an array of Trail Relationships $trailRelationships = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $trailRelationship = new TrailRelationship($row["segmentId"], $row["trailId"], $row["segmentType"]); $trailRelationships[$trailRelationships->key()] = $trailRelationship; $trailRelationships->next(); } catch (Exception $e) { //if the row couldn't be converted, rethrow it throw new PDOException($e->getMessage(), 0, $e); } } // // grab the Trail Relationship from mySQL // try { // $trailRelationship = null; // $statement->setFetchMode(PDO::FETCH_ASSOC); // $row = $statement->fetch(); // if($row !== false) { // $trailRelationship = new TrailRelationship($row["segmentId"], $row["trailId"], $row["segmentType"]); // } // } catch(Exception $exception) { // // if the row couldn't be converted, rethrow it // throw(new PDOException($exception->getMessage(), 0, $exception)); // } return $trailRelationships; }
/** * Get all Locations * @param PDO $pdo pointer to PDO connection, by reference * @return mixed|location **/ public static function getAllLocations(PDO &$pdo) { // create query template $query = "SELECT locationId, storageCode, description FROM location"; $statement = $pdo->prepare($query); $statement->execute(); // build an array of movements $locations = new SplFixedArray($statement->rowCount()); $statement->setFetchMode(PDO::FETCH_ASSOC); while (($row = $statement->fetch()) !== false) { try { $location = new Location($row["locationId"], $row["storageCode"], $row["description"]); $locations[$locations->key()] = $location; $locations->next(); } catch (Exception $exception) { // if the row couldn't be converted, rethrow it throw new PDOException($exception->getMessage(), 0, $exception); } } return $locations; }