This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Committing various PHP experiments: DBQuery, Fibonacci, and Random St…
…ring.
- Loading branch information
1 parent
af61c93
commit 44e3647
Showing
15 changed files
with
582 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/* | ||
* Copyright (c) 2009, Timothy Boronczyk | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. The names of the authors may not be used to endorse or promote products | ||
* derived from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
class DBQuery implements Iterator | ||
{ | ||
protected $_db; | ||
protected $_query; | ||
protected $_result; | ||
protected $_index; | ||
protected $_num_rows; | ||
|
||
public function __construct($host, $dbname, $username, $password) { | ||
$this->_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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/* | ||
* Copyright (c) 2011, Timothy Boronczyk | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. The names of the authors may not be used to endorse or promote products | ||
* derived from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
class Fibonacci implements Iterator, ArrayAccess | ||
{ | ||
const PHI = 1.618033989; | ||
|
||
private $i; | ||
|
||
private $a; | ||
private $b; | ||
|
||
public function __construct() { | ||
} | ||
|
||
public function rewind() { | ||
$this->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"); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/* | ||
* Copyright (c) 2009, Timothy Boronczyk | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. The names of the authors may not be used to endorse or promote products | ||
* derived from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
class RandomCharacterSequenceGenerator extends RandomSequenceIterator | ||
{ | ||
public function setChars($strValue) { | ||
$this->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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/* | ||
* Copyright (c) 2009, Timothy Boronczyk | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. The names of the authors may not be used to endorse or promote products | ||
* derived from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
abstract class RandomGeneratorBase | ||
{ | ||
protected static $instance; | ||
protected static $generator; | ||
|
||
protected function __construct() { | ||
self::$generator = new RandomCharacterSequenceGenerator(); | ||
} | ||
|
||
// abstract public static function getInstance(); | ||
public static function getInstance() { | ||
if (empty(self::$instance)) { | ||
self::$instance = new self(); | ||
} | ||
return self::$instance; | ||
} | ||
|
||
public function generate($limit) { | ||
return self::$generator->generate($limit); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/* | ||
* Copyright (c) 2009, Timothy Boronczyk | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. The names of the authors may not be used to endorse or promote products | ||
* derived from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
class RandomLowerCaseAlphaStringGenerator extends RandomGeneratorBase | ||
{ | ||
const VALID_MEMBERS = "abcdefghijklmnopqrstuvwxyz"; | ||
|
||
protected function __construct() { | ||
parent::__construct(); | ||
self::$generator->setChars(self::VALID_MEMBERS); | ||
} | ||
|
||
public static function getInstance() { | ||
if (empty(self::$instance)) { | ||
self::$instance = new self(); | ||
} | ||
return self::$instance; | ||
} | ||
} | ||
|
40 changes: 40 additions & 0 deletions
40
Random-String/RandomMixedCaseAlphaNumericStringGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/* | ||
* Copyright (c) 2009, Timothy Boronczyk | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. The names of the authors may not be used to endorse or promote products | ||
* derived from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
class RandomMixedCaseAlphaNumericStringGenerator extends | ||
RandomMixedCaseAlphaStringGenerator | ||
{ | ||
protected function __construct() { | ||
parent::__construct(); | ||
self::$generator->setChars( | ||
RandomNumericStringGenerator::VALID_MEMBERS . | ||
self::$generator->getChars()); | ||
} | ||
|
||
public static function getInstance() { | ||
if (empty(self::$instance)) { | ||
self::$instance = new self(); | ||
} | ||
return self::$instance; | ||
} | ||
} | ||
|
Oops, something went wrong.