diff --git a/DBQuery/DBQuery.php b/DBQuery/DBQuery.php new file mode 100644 index 0000000..4b214be --- /dev/null +++ b/DBQuery/DBQuery.php @@ -0,0 +1,85 @@ +_db = new PDO("mysql:dbname=$dbname;host=$host", + $username, $password); + } + + public function __get($query) { + $this->_query = $query; + $this->_result = $this->_db->query($query); + return $this->_num_rows = $this->_result->rowCount(); + } + + public function quote($value) { + return PDO::quote($value); + } + + public function __call($query, $values) { + $this->_query = $query; + $this->_result = $this->_db->prepare($this->_query); + $this->_result->execute($values[0]); + return $this->_num_rows = $this->_result->rowCount(); + } + + public function clear() { + $this->_index = 0; + $this->_num_rows = 0; + $this->_query = ''; + $this->_result->closeCursor(); + } + + public function rewind() { + $this->_index = 0; + } + + public function current() { + return $this->_result->fetch(PDO::FETCH_ASSOC, + PDO::FETCH_ORI_ABS, $this->_index); + } + + public function key() { + return $this->_index; + } + + public function next() { + $this->_index++; + } + + public function valid() { + return ($this->_index < $this->_num_rows); + } + + public function __toString() { + return $this->_query; + } +} + diff --git a/DBQuery/README b/DBQuery/README new file mode 100644 index 0000000..def018a --- /dev/null +++ b/DBQuery/README @@ -0,0 +1,3 @@ +The DBQuery class presents an "interesting" approach to interfacing with a +database. See http://zaemis.blogspot.com/2009/01/database-access.html for +more information. diff --git a/Fibonacci/Fibonacci.php b/Fibonacci/Fibonacci.php new file mode 100644 index 0000000..c0424c5 --- /dev/null +++ b/Fibonacci/Fibonacci.php @@ -0,0 +1,79 @@ +i = 0; + $this->a = 1; + $this->b = 0; + } + + public function current() { + return $this->b; + } + + public function key() { + return $this->i; + } + + public function next() { + $this->i++; + $tmp = $this->a + $this->b; + $this->a = $this->b; + $this->b = $tmp; + } + + public function valid() { + return $this->offsetExists($this->i); + } + + public function offsetExists($i) { + // Fib(1476) = float(1.30698922376E+308) + // Fib(1477) = float(INF) + return $i > -1 && $i < 1478; + } + + public function offsetGet($i) { + // http://en.wikipedia.org/wiki/Fibonacci_number#Computation_by_rounding + return floor((pow(self::PHI, $i) / sqrt(5)) + 0.5); + } + + public function offsetSet($i, $val) { + throw new Exception("sequence is read-only"); + } + + public function offsetUnset($i) { + throw new Exception("sequence is read-only"); + } +} + diff --git a/Fibonacci/README b/Fibonacci/README new file mode 100644 index 0000000..b9219b3 --- /dev/null +++ b/Fibonacci/README @@ -0,0 +1,4 @@ +The Fibonacci class is a lazily-iterable list of Fibonacci numbers which +also supports random array access. For more information visit +http://zaemis.tumblr.com/post/6179755099/lazy-fibonacci-array + diff --git a/README b/README index e69de29..e35da20 100644 --- a/README +++ b/README @@ -0,0 +1,12 @@ +This is a collection of various PHP experiments. + + * DBQuery- "Interesting" database interfacing. For more information + visit http://zaemis.blogspot.com/2009/01/database-access.html + + * Fibonacci- A lazily-iterable list of Fibonacci numbers which also + supports random array access. For more information visit + http://zaemis.tumblr.com/post/6179755099/lazy-fibonacci-array + + * Random String- An intentionally over-engineered random string generator. + For more information see http://zaemis.blogspot.com/2009/10/pop-quiz.html + diff --git a/Random-String/README b/Random-String/README new file mode 100644 index 0000000..59f719e --- /dev/null +++ b/Random-String/README @@ -0,0 +1,4 @@ +This is a tongue-in-cheek random string generator highlighting the absurdity +of overzealous OO design. See http://zaemis.blogspot.com/2009/10/pop-quiz.html +for more information. + diff --git a/Random-String/RandomCharacterSequenceGenerator.php b/Random-String/RandomCharacterSequenceGenerator.php new file mode 100644 index 0000000..937344e --- /dev/null +++ b/Random-String/RandomCharacterSequenceGenerator.php @@ -0,0 +1,43 @@ +setMembers($strValue); + return $this; + } + + public function getChars() { + return $this->getMembers(); + } + + public function generate($limit) { + $strBuffer = ""; + $this->setLimit($limit); + foreach ($this as $char) { + $strBuffer .= $char; + } + return $strBuffer; + } +} + diff --git a/Random-String/RandomGeneratorBase.php b/Random-String/RandomGeneratorBase.php new file mode 100644 index 0000000..d1f0146 --- /dev/null +++ b/Random-String/RandomGeneratorBase.php @@ -0,0 +1,44 @@ +generate($limit); + } +} + diff --git a/Random-String/RandomLowerCaseAlphaStringGenerator.php b/Random-String/RandomLowerCaseAlphaStringGenerator.php new file mode 100644 index 0000000..28566f9 --- /dev/null +++ b/Random-String/RandomLowerCaseAlphaStringGenerator.php @@ -0,0 +1,39 @@ +setChars(self::VALID_MEMBERS); + } + + public static function getInstance() { + if (empty(self::$instance)) { + self::$instance = new self(); + } + return self::$instance; + } +} + diff --git a/Random-String/RandomMixedCaseAlphaNumericStringGenerator.php b/Random-String/RandomMixedCaseAlphaNumericStringGenerator.php new file mode 100644 index 0000000..0548501 --- /dev/null +++ b/Random-String/RandomMixedCaseAlphaNumericStringGenerator.php @@ -0,0 +1,40 @@ +setChars( + RandomNumericStringGenerator::VALID_MEMBERS . + self::$generator->getChars()); + } + + public static function getInstance() { + if (empty(self::$instance)) { + self::$instance = new self(); + } + return self::$instance; + } +} + diff --git a/Random-String/RandomMixedCaseAlphaStringGenerator.php b/Random-String/RandomMixedCaseAlphaStringGenerator.php new file mode 100644 index 0000000..112297a --- /dev/null +++ b/Random-String/RandomMixedCaseAlphaStringGenerator.php @@ -0,0 +1,39 @@ +setChars( + RandomUpperCaseAlphaStringGenerator::VALID_MEMBERS . + RandomLowerCaseAlphaStringGenerator::VALID_MEMBERS); + } + + public static function getInstance() { + if (empty(self::$instance)) { + self::$instance = new self(); + } + return self::$instance; + } +} + diff --git a/Random-String/RandomNumericStringGenerator.php b/Random-String/RandomNumericStringGenerator.php new file mode 100644 index 0000000..7a7fcd7 --- /dev/null +++ b/Random-String/RandomNumericStringGenerator.php @@ -0,0 +1,39 @@ +setChars(self::VALID_MEMBERS); + } + + public static function getInstance() { + if (empty(self::$instance)) { + self::$instance = new self(); + } + return self::$instance; + } +} + diff --git a/Random-String/RandomSequenceIterator.php b/Random-String/RandomSequenceIterator.php new file mode 100644 index 0000000..565626d --- /dev/null +++ b/Random-String/RandomSequenceIterator.php @@ -0,0 +1,83 @@ +setMembers(null) + ->setLimit(0) + ->rewind(); + } + + protected function setMembers($strValue) { + $this->seqMembers = $strValue; + return $this; + } + + protected function getMembers() { + return $this->seqMembers; + } + + protected function setLimit($intValue) { + $this->limit = $intValue; + return $this; + } + + protected function getLimit() { + if (empty($this->limit)) { + return 0; + } + else { + return $this->limit; + } + } + + public function current() { + $index = rand(0, strlen($this->getMembers()) - 1); + return substr($this->getMembers(), $index, 1); + } + + public function valid() { + return $this->key() < $this->getLimit(); + } + + public function key() { + return $this->key; + } + + public function next() { + $this->key++; + } + + public function reset() { + $this->rewind(); + } + + public function rewind() { + $this->key = 0; + } +} + diff --git a/Random-String/RandomUpperCaseAlphaStringGenerator.php b/Random-String/RandomUpperCaseAlphaStringGenerator.php new file mode 100644 index 0000000..88ecba7 --- /dev/null +++ b/Random-String/RandomUpperCaseAlphaStringGenerator.php @@ -0,0 +1,39 @@ +setChars(self::VALID_MEMBERS); + } + + public static function getInstance() { + if (empty(self::$instance)) { + self::$instance = new self(); + } + return self::$instance; + } +} + diff --git a/Random-String/index.php b/Random-String/index.php new file mode 100644 index 0000000..7b36800 --- /dev/null +++ b/Random-String/index.php @@ -0,0 +1,29 @@ +generate(8); +echo "$str\n"; +