Esempio n. 1
0
 /**
  * gets all the trail ratings for a trail by trailId
  *
  * @param PDO $pdo PDO Connection object
  * @param int $trailId trail id to trail-search for
  * @returns SplFixedArray all ratings for the trail
  * @throws PDOException when mySql related error occurs
  */
 public static function getRatingValueByTrailId(PDO $pdo, $trailId)
 {
     // sanitize the 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 trailId, userId, ratingValue FROM rating WHERE trailId = :trailId";
     $statement = $pdo->prepare($query);
     // bind the trail id to the place holder in the template
     $parameters = array("trailId" => $trailId);
     $statement->execute($parameters);
     // build an array of trail ratings
     $ratings = new SPLFixedArray($statement->rowcount());
     $statement->setFetchMode(PDO::FETCH_ASSOC);
     while (($row = $statement->fetch()) !== false) {
         try {
             $rating = new Rating($row["trailId"], $row["userId"], $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;
 }
Esempio n. 2
0
/**
 * Loops through the gcode file, continuously analysing a buffer to see
 * if it is a valid circle within a tolerance.
 * @param  SPLFixedArray $gcode The gcode as an SPLFixedArray of lines
 * @return string               The resulting GCode string
 */
function processGcode($gcode)
{
    global $debug;
    global $lookahead;
    global $start;
    // Keeps track of the amount of valid buffers (valid circles)
    $totalValidBuffers = 0;
    // The output gcode
    $output = "";
    // The looping buffer
    $buffer = new SplQueue();
    // go back to the start of our gcode
    $gcode->rewind();
    // prefill the buffer
    do {
        $buffer->enqueue($gcode->current());
        $gcode->next();
    } while ($buffer->count() < $lookahead);
    // keeps track of the last valid buffer.
    $lastValid = false;
    // Loop through our gcode
    for (; $gcode->valid(); $gcode->next()) {
        // Don't process for too long, and during debugging, only do a couple of buffers (finding circles
        // takes a long time)
        if (microtime(true) - $start > 120 || $debug === true && $totalValidBuffers > 100) {
            $output .= $gcode->current() . "\n";
            continue;
        }
        // check to see if we have a 'valid' buffer
        $valid = bufferValid($buffer);
        // if the buffer is no longer valid and we have more items in our buffer than
        // the lookahead value, then we must have had a valid buffer at the last step
        if ($valid === false) {
            if ($buffer->count() > $lookahead) {
                // The last element made the buffer invalid, so we pop it so that we
                // can stick it back on the end later
                $temp = $buffer->pop();
                // Our last buffer was a valid circle, so we take that
                $processed = $lastValid;
                // Double check that we had a valid buffer...
                if ($processed !== false) {
                    // creates a line array
                    $lines = getLines($buffer);
                    // If we're debugging, we draw the 'result' on a canvas.
                    if ($debug) {
                        drawResult($processed, $lines, 'gcodeview' . $gcode->key());
                    }
                    // Generate a gcode arc from the lines
                    $output .= generateGcodeArc($processed, $lines) . "\n";
                } else {
                    // otherwise we just stick the buffer on the output.
                    foreach ($buffer as $num => $buffer) {
                        $output .= $buffer . "\n";
                    }
                }
                // Now, we re-initialise the buffer and fill it up
                $buffer = new SplQueue();
                do {
                    $buffer->enqueue($gcode->current());
                    $gcode->next();
                } while ($buffer->count() < $lookahead && $gcode->valid());
                // Increase the amount of valid buffers
                $totalValidBuffers++;
                // Stick our previously popped temp value on the end
                $output .= $temp . "\n";
            } else {
                // otherwise, we dequeue a value off the buffer
                $output .= $buffer->dequeue() . "\n";
            }
        }
        // record the last valid buffer
        $lastValid = $valid;
        // If we still have code to process, stick it on the buffer!
        if ($gcode->valid()) {
            $buffer->enqueue($gcode->current());
        }
    }
    // We're done!
    printf("Total Valid Buffers: %d | ", $totalValidBuffers);
    return $output;
}
Esempio n. 3
0
 /**
  * gets all users
  *
  * @param \PDO $pdo PDO connection object
  * @return \splFixedArray splFixedArray of users 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 getAllUsers(\PDO $pdo)
 {
     //create query update
     $query = "SELECT userId, userCompanyId, userCrewId, userAccessId, userPhone, userFirstName, userLastName, userEmail, userActivation, userHash, userSalt\n\t\t\t\t\t\tFROM user\n\t\t\t\t\t\tWHERE user.userId\n\t\t\t\t\t\tIN (SELECT userId FROM user WHERE userCompanyId = :companyId)";
     $statement = $pdo->prepare($query);
     $parameters = ["companyId" => self::injectCompanyId()];
     $statement->execute($parameters);
     //build an array of user
     $users = new \SPLFixedArray($statement->rowCount());
     $statement->setFetchMode(\PDO::FETCH_ASSOC);
     while (($row = $statement->fetch()) !== false) {
         try {
             $user = new User($row["userId"], $row["userCompanyId"], $row["userCrewId"], $row["userAccessId"], $row["userPhone"], $row["userFirstName"], $row["userLastName"], $row["userEmail"], $row["userActivation"], $row["userHash"], $row["userSalt"]);
             $users[$users->key()] = $user;
             $users->next();
         } catch (\Exception $exception) {
             // if the row couldn't be converted, rethrow it
             throw new \PDOException($exception->getMessage(), 0, $exception);
         }
     }
     return $users;
 }
Esempio n. 4
0
/**
 * Parses gcode string
 * @param  SPLFixedArray $gcode The gcode as an SPLFixedArray of lines
 * @return array                An array of lines where each entry is split into command and parameters
 */
function parseGcode($gcode)
{
    global $debug;
    global $lookahead;
    global $start;
    $output = array();
    $gcode->rewind();
    $lastValid = false;
    for (; $gcode->valid(); $gcode->next()) {
        $output[] = parseLine($gcode->current());
    }
    return array_filter($output);
}