- changed NeatArray to Narray, it's shorter, easier to use,

- Time::daysAsSql returns SQL limits for a set of days, perhaps should be in DBO,
- cleanups

git-svn-id: https://svn.cakephp.org/repo/trunk/cake@259 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
pies 2005-06-20 23:08:59 +00:00
parent 847d6df3a3
commit b717167c4f
14 changed files with 173 additions and 1119 deletions

View file

@ -86,12 +86,12 @@ define ('MODULES', ROOT.'modules'.DS);
/**
* Path to the public directory.
*/
define ('WWW_ROOT', ROOT.'public'.DS);
define ('WWW_ROOT', ROOT.'public'.DS);
/**
* Path to the public directory.
*/
define ('CSS', WWW_ROOT.'css'.DS);
define ('CSS', WWW_ROOT.'css'.DS);
/**
* Path to the scripts direcotry.

View file

@ -48,9 +48,11 @@ define('YEAR', 365 * DAY);
* @uses APP
* @uses MODELS
*/
function loadModels () {
function loadModels ()
{
require (APP.'app_model.php');
foreach (listClasses(MODELS) as $model_fn) {
foreach (listClasses(MODELS) as $model_fn)
{
require (MODELS.$model_fn);
}
}
@ -63,14 +65,17 @@ function loadModels () {
* @uses HELPERS
* @uses CONTROLLERS
*/
function loadControllers () {
function loadControllers ()
{
require (APP.'app_controller.php');
foreach (listClasses(HELPERS) as $helper) {
foreach (listClasses(HELPERS) as $helper)
{
require (HELPERS.$helper.'.php');
}
foreach (listClasses(CONTROLLERS) as $controller) {
foreach (listClasses(CONTROLLERS) as $controller)
{
require (CONTROLLERS.$controller.'.php');
}
}
@ -81,7 +86,8 @@ function loadControllers () {
* @param string $name
* @return boolean
*/
function loadController ($name) {
function loadController ($name)
{
$controller_fn = CONTROLLERS.Inflector::underscore($name).'_controller.php';
$helper_fn = HELPERS.Inflector::underscore($name).'_helper.php';
@ -108,18 +114,22 @@ function listClasses($path)
/**
* Loads configuration files
*/
function config () {
function config ()
{
$args = func_get_args();
foreach ($args as $arg) {
foreach ($args as $arg)
{
if (('database' == $arg) && file_exists(CONFIGS.$arg.'.php'))
{
include_once(CONFIGS.$arg.'.php');
}
elseif (file_exists(CONFIGS.$arg.'.php')) {
elseif (file_exists(CONFIGS.$arg.'.php'))
{
include (CONFIGS.$arg.'.php');
if (count($args) == 1) return true;
}
else {
else
{
if (count($args) == 1) return false;
}
}
@ -180,13 +190,15 @@ if (!function_exists('getMicrotime'))
*
* @return integer
*/
function getMicrotime() {
function getMicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
}
if (!function_exists('sortByKey')) {
if (!function_exists('sortByKey'))
{
/**
* Sorts given $array by key $sortby.
*
@ -196,30 +208,31 @@ if (!function_exists('sortByKey')) {
* @param integer $type
* @return mixed
*/
function sortByKey(&$array, $sortby, $order='asc', $type=SORT_NUMERIC) {
if (is_array($array)) {
foreach ($array as $key => $val)
$sa[$key] = $val[$sortby];
if ($order == 'asc')
asort($sa, $type);
else
arsort($sa, $type);
foreach ($sa as $key=>$val)
$out[] = $array[$key];
return $out;
}
else
function sortByKey(&$array, $sortby, $order='asc', $type=SORT_NUMERIC)
{
if (!is_array($array))
return null;
foreach ($array as $key => $val)
{
$sa[$key] = $val[$sortby];
}
$order == 'asc'
? asort($sa, $type)
: arsort($sa, $type);
foreach ($sa as $key=>$val)
{
$out[] = $array[$key];
}
return $out;
}
}
if (!function_exists('array_combine')) {
if (!function_exists('array_combine'))
{
/**
* Combines given identical arrays by using the first array's values as keys,
* and the second one's values as values. (Implemented for back-compatibility with PHP4.)
@ -228,18 +241,20 @@ if (!function_exists('array_combine')) {
* @param array $a2
* @return mixed Outputs either combined array or false.
*/
function array_combine($a1, $a2) {
function array_combine($a1, $a2)
{
$a1 = array_values($a1);
$a2 = array_values($a2);
$c1 = count($a1);
$c2 = count($a2);
if ($c1 != $c2) return false; // different lenghts
if ($c1 <= 0) return false; // arrays are the same and both are empty
if ($c1 <= 0) return false; // arrays are the same and both are empty
$output = array();
for ($i = 0; $i < $c1; $i++) {
for ($i = 0; $i < $c1; $i++)
{
$output[$a1[$i]] = $a2[$i];
}

View file

@ -16,7 +16,6 @@
/**
* Purpose: Controller
* Application controller (controllers are where you put all the actual code)
* based on RoR (www.rubyonrails.com).
* Provides basic functionality, such as rendering views (aka displaying templates).
* Automatically selects model name from on singularized object class name
* and creates the model object if proper class exists.

View file

@ -75,11 +75,10 @@ class Dispatcher extends Object
parent::__construct();
}
/**
* Enter description here...
*
* @param unknown_type $url
* @param string $url
* @return unknown
*/
function dispatch($url)

View file

@ -289,6 +289,11 @@ class Flay extends Object
return strip_tags(html_entity_decode($text, ENT_QUOTES));
}
function toParsedAndClean ($text)
{
return Flay::toClean(Flay::toHtml($text));
}
/**
* Enter description here...
*

View file

@ -297,7 +297,7 @@ class Model extends Object
function loadInfo ()
{
if (empty($this->_table_info))
$this->_table_info = new NeatArray($this->db->fields($this->table));
$this->_table_info = new Narray($this->db->fields($this->table));
return $this->_table_info;
}
@ -337,6 +337,7 @@ class Model extends Object
{
if ($conditions)
{
$conditions = $this->parseConditions($conditions);
$data = $this->find($conditions);
return $data[$name];
}
@ -552,7 +553,6 @@ class Model extends Object
*/
function findAll ($conditions = null, $fields = null, $order = null, $limit=50, $page=1)
{
$conditions = $this->parseConditions($conditions);
if (is_array($fields))
@ -576,9 +576,10 @@ class Model extends Object
$conditions .= ($conditions && $whers? ' AND ': null).$whers;
$offset = $page > 1? $page*$limit: 0;
$limit = $limit? $limit: '-1';
$limit_str = $this->db->selectLimit($limit, $offset);
$limit_str = $limit
? $this->db->selectLimit($limit, $offset)
: '';
$sql =
"SELECT "

View file

@ -7,9 +7,9 @@
* @subpackage cake.libs
* @since Cake v 0.2.9
*/
class NeatArray {
class Narray {
/**
* Value of NeatArray.
* Value of Narray.
*
* @var array
* @access public
@ -21,20 +21,20 @@ class NeatArray {
*
* @param array $value
* @access public
* @uses NeatArray::value
* @uses Narray::value
*/
function NeatArray ($value=array()) {
function Narray ($value=array()) {
$this->value = $value;
}
/**
* Checks whether $fieldName with $value exists in this NeatArray object.
* Finds and returns records with $fieldName equal $value from this Narray.
*
* @param string $fieldName
* @param string $value
* @return mixed
* @access public
* @uses NeatArray::value
* @uses Narray::value
*/
function findIn ($fieldName, $value)
{
@ -44,8 +44,10 @@ class NeatArray {
}
$out = false;
foreach ($this->value as $k=>$v) {
if (isset($v[$fieldName]) && ($v[$fieldName] == $value)) {
foreach ($this->value as $k=>$v)
{
if (isset($v[$fieldName]) && ($v[$fieldName] == $value))
{
$out[$k] = $v;
}
}
@ -57,7 +59,7 @@ class NeatArray {
* Checks if $this->value is array, and removes all empty elements.
*
* @access public
* @uses NeatArray::value
* @uses Narray::value
*/
function cleanup () {
$out = is_array($this->value)? array(): null;
@ -75,7 +77,7 @@ class NeatArray {
* @param string $value
* @return bool
* @access public
* @uses NeatArray::value
* @uses Narray::value
*/
function add ($value) {
return ($this->value = $this->plus($value))? true: false;
@ -84,10 +86,10 @@ class NeatArray {
/**
* Returns itself merged with given array.
*
* @param array $value Array to add to NeatArray.
* @param array $value Array to add to Narray.
* @return array
* @access public
* @uses NeatArray::value
* @uses Narray::value
*/
function plus ($value) {
return array_merge($this->value, (is_array($value)? $value: array($value)));
@ -99,7 +101,7 @@ class NeatArray {
* @param int $sortedBy A value of 1 sorts by values, a value of 2 sorts by keys. Defaults to null (no sorting).
* @return array
* @access public
* @uses NeatArray::value
* @uses Narray::value
*/
function totals ($sortedBy=1,$reverse=true) {
$out = array();
@ -132,7 +134,7 @@ class NeatArray {
*
* @return array
* @access public
* @uses NeatArray::value
* @uses Narray::value
*/
function walk ($with) {
array_walk($this->value, $with);
@ -154,7 +156,7 @@ class NeatArray {
*
* @return array
* @access public
* @uses NeatArray::value
* @uses Narray::value
*/
function extract ($name) {
$out = array();
@ -191,7 +193,7 @@ class NeatArray {
* $alice = array('id'=>'1', 'name'=>'Alice');
* $bob = array('id'=>'2', 'name'=>'Bob');
*
* $users = new NeatArray(array($alice, $bob));
* $users = new Narray(array($alice, $bob));
*
* $born = array
* (
@ -219,7 +221,7 @@ class NeatArray {
function joinWith ($his, $onMine, $onHis=null) {
if (empty($onHis)) $onHis = $onMine;
$his = new NeatArray($his);
$his = new Narray($his);
$out = array();
foreach ($this->value as $key=>$val) {
@ -261,4 +263,9 @@ class NeatArray {
}
}
// for backwards-compatibility, remove in 2008
class NeatArray extends Narray
{
}
?>

View file

@ -42,6 +42,13 @@ uses('log');
class Object
{
/**
* Database connection, if available.
*
* @var DBO
*/
var $db = null;
/**
* A hack to support __construct() on PHP 4
* Hint: descendant classes have no PHP4 class_name() constructors,

View file

@ -30,7 +30,7 @@
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
uses('object');
uses('object', 'narray');
/**
* Parses the request URL into controller, action, and parameters.
@ -156,7 +156,7 @@ class Router extends Object {
// unnamed elements go in as 'pass'
else
{
$pass = new NeatArray(explode('/', $found));
$pass = new Narray(explode('/', $found));
$pass->cleanup();
$out['pass'] = $pass->value;
}

View file

@ -1022,7 +1022,9 @@ class Template extends Object
function remoteFunction ($options=null)
{
$javascript_options = $this->__optionsForAjax($options);
$func = isset($options['update']) ? "new Ajax.Updater('{$options['update']}', " : "new Ajax.Request(";
$func = isset($options['update'])
? "new Ajax.Updater('{$options['update']}', "
: "new Ajax.Request(";
$func .= "'" . $this->urlFor($options['url']) . "'";
$func .= ", $javascript_options)";

File diff suppressed because it is too large Load diff

View file

@ -86,6 +86,19 @@ class Time extends Object {
return date('Y-m-d', $date) == date('Y-m-d', time());
}
function daysAsSql ($begin, $end, $field_name)
{
$begin = date('Y-m-d', $begin).' 00:00:00';
$end = date('Y-m-d', $end). ' 23:59:59';
return "($field_name >= '$begin') AND ($field_name <= '$end')";
}
function dayAsSql ($date, $field_name)
{
return Time::daysAsSql($date, $date, $field_name);
}
/**
* Returns true if given datetime string is within current year.
*

View file

@ -57,7 +57,7 @@ require_once (ROOT.'config/paths.php');
require_once (ROOT.'libs/basics.php');
require_once (ROOT.'libs/log.php');
require_once (ROOT.'libs/object.php');
require_once (ROOT.'libs/neat_array.php');
require_once (ROOT.'libs/narray.php');
require_once (ROOT.'libs/inflector.php');
DEBUG? error_reporting(E_ALL): error_reporting(0);

60
tests/libs/narray.php Normal file
View file

@ -0,0 +1,60 @@
<?PHP
uses('narray');
class NarrayTest extends UnitTestCase
{
var $narray;
// constructor of the test suite
function NarrayTest()
{
$this->UnitTestCase('Narray test');
}
// called before the test functions will be executed
// this function is defined in PHPUnit_TestCase and overwritten
// here
function setUp()
{
$this->narray = new Narray();
}
// called after the test functions are executed
// this function is defined in PHPUnit_TestCase and overwritten
// here
function tearDown()
{
unset($this->narray);
}
function testInArray()
{
$a = array('foo'=>' bar ', 'i-am'=>'a');
$b = array('foo'=>'bar ', 'i-am'=>'b');
$c = array('foo'=>' bar', 'i-am'=>'c');
$d = array('foo'=>'bar', 'i-am'=>'d');
$n = new Narray(array($a, $b, $c, $d));
$result = $n->findIn('foo', ' bar ');
$expected = array(0=>$a);
$this->assertEqual($result, $expected);
$result = $n->findIn('foo', 'bar ');
$expected = array(1=>$b);
$this->assertEqual($result, $expected);
$result = $n->findIn('foo', ' bar');
$expected = array(2=>$c);
$this->assertEqual($result, $expected);
$result = $n->findIn('foo', 'bar');
$expected = array(3=>$d);
$this->assertEqual($result, $expected);
}
}
?>