1
0
mirror of synced 2024-12-14 07:06:04 +03:00
doctrine2/lib/Doctrine/Hook.php

225 lines
7.1 KiB
PHP
Raw Normal View History

2006-12-15 23:10:23 +03:00
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Hook
*
* @package Doctrine
* @subpackage Hook
2006-12-15 23:10:23 +03:00
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
2006-12-29 17:40:47 +03:00
class Doctrine_Hook
{
2006-12-15 23:10:23 +03:00
/**
* @var Doctrine_Query $query the base query
*/
2006-12-24 01:45:36 +03:00
protected $query;
2006-12-15 23:10:23 +03:00
/**
* @var array $joins the optional joins of the base query
*/
2006-12-24 01:45:36 +03:00
protected $joins;
2006-12-24 01:45:36 +03:00
/**
* @var array $hooks hooks array
*/
protected $hooks = array(
'where',
'orderby',
'limit',
'offset'
);
2006-12-24 01:45:36 +03:00
/**
2007-01-05 02:52:18 +03:00
* @var array $fieldParsers custom field parsers array
* keys as field names in the format componentAlias.FieldName
* values as parser names / objects
2006-12-24 01:45:36 +03:00
*/
protected $fieldParsers = array();
/**
2007-01-05 02:52:18 +03:00
* @var array $typeParsers type parsers array
* keys as type names and values as parser names / objects
2006-12-24 01:45:36 +03:00
*/
protected $typeParsers = array(
'char' => 'Doctrine_Hook_WordLike',
'string' => 'Doctrine_Hook_WordLike',
2007-03-09 02:22:59 +03:00
'varchar' => 'Doctrine_Hook_WordLike',
2006-12-24 01:45:36 +03:00
'integer' => 'Doctrine_Hook_Integer',
2007-08-03 01:27:42 +04:00
'enum' => 'Doctrine_Hook_Integer',
2007-01-05 02:52:18 +03:00
'time' => 'Doctrine_Hook_Time',
'date' => 'Doctrine_Hook_Date',
2006-12-24 01:45:36 +03:00
);
2006-12-15 23:10:23 +03:00
/**
* @param Doctrine_Query $query the base query
*/
2006-12-29 17:40:47 +03:00
public function __construct($query)
{
2006-12-29 17:01:31 +03:00
if (is_string($query)) {
2006-12-15 23:10:23 +03:00
$this->query = new Doctrine_Query();
$this->query->parseQuery($query);
2006-12-29 17:01:31 +03:00
} elseif ($query instanceof Doctrine_Query) {
2006-12-15 23:10:23 +03:00
$this->query = $query;
2007-03-17 01:17:32 +03:00
} else {
throw new Doctrine_Exception('Constructor argument should be either Doctrine_Query object or valid DQL query');
2006-12-15 23:10:23 +03:00
}
2007-07-06 03:47:48 +04:00
$this->query->getQuery();
2006-12-15 23:10:23 +03:00
}
/**
* getQuery
*
* @return Doctrine_Query returns the query object associated with this hook
*/
2006-12-29 17:40:47 +03:00
public function getQuery()
{
2006-12-24 01:45:36 +03:00
return $this->query;
}
2007-01-05 02:52:18 +03:00
/**
* setTypeParser
*
* @param string $type type name
* @param string|object $parser parser name or custom parser object
*/
public function setTypeParser($type, $parser)
2006-12-29 17:40:47 +03:00
{
2007-01-05 02:52:18 +03:00
$this->typeParsers[$type] = $parser;
2006-12-15 23:10:23 +03:00
}
2007-01-05 02:52:18 +03:00
/**
* setFieldParser
*
* @param string $field field name
* @param string|object $parser parser name or custom parser object
*/
public function setFieldParser($field, $parser)
2006-12-29 17:40:47 +03:00
{
2007-01-05 02:52:18 +03:00
$this->fieldParsers[$field] = $parser;
2006-12-15 23:10:23 +03:00
}
2006-12-24 01:45:36 +03:00
/**
* hookWhere
2006-12-26 23:33:40 +03:00
* builds DQL query where part from given parameter array
2006-12-24 01:45:36 +03:00
*
2006-12-29 17:01:31 +03:00
* @param array $params an associative array containing field
2006-12-26 23:33:40 +03:00
* names and their values
* @return boolean whether or not the hooking was
2006-12-24 01:45:36 +03:00
*/
2006-12-29 17:40:47 +03:00
public function hookWhere($params)
{
2006-12-29 17:01:31 +03:00
if ( ! is_array($params)) {
2006-12-26 23:33:40 +03:00
return false;
2006-12-29 17:01:31 +03:00
}
foreach ($params as $name => $value) {
2007-08-03 01:27:42 +04:00
if ($value === '' || $value === '-') {
continue;
}
2006-12-24 01:45:36 +03:00
$e = explode('.', $name);
2006-12-26 23:33:40 +03:00
2006-12-29 17:01:31 +03:00
if (count($e) == 2) {
2006-12-24 01:45:36 +03:00
list($alias, $column) = $e;
2007-07-06 03:47:48 +04:00
2007-05-26 20:49:58 +04:00
$map = $this->query->getAliasDeclaration($alias);
$table = $map['table'];
2006-12-26 23:33:40 +03:00
2007-03-17 01:17:32 +03:00
if ( ! $table) {
2007-07-06 03:47:48 +04:00
throw new Doctrine_Exception('Unknown alias ' . $alias);
2007-03-17 01:17:32 +03:00
}
2006-12-29 17:01:31 +03:00
if ($def = $table->getDefinitionOf($column)) {
2007-07-06 03:47:48 +04:00
2007-06-19 14:15:44 +04:00
$def[0] = gettype($value);
2006-12-29 17:01:31 +03:00
if (isset($this->typeParsers[$def[0]])) {
2006-12-24 01:45:36 +03:00
$name = $this->typeParsers[$def[0]];
$parser = new $name;
}
$parser->parse($alias, $column, $value);
$this->query->addWhere($parser->getCondition(), $parser->getParams());
2007-07-06 03:47:48 +04:00
}
2006-12-24 01:45:36 +03:00
}
}
2006-12-29 17:01:31 +03:00
2006-12-26 23:33:40 +03:00
return true;
2006-12-15 23:10:23 +03:00
}
2006-12-24 01:45:36 +03:00
/**
2006-12-26 23:33:40 +03:00
* hookOrderBy
* builds DQL query orderby part from given parameter array
*
* @param array $params an array containing all fields which the built query
* should be ordered by
2007-01-27 13:08:06 +03:00
* @return boolean whether or not the hooking was successful
2006-12-24 01:45:36 +03:00
*/
2006-12-29 17:40:47 +03:00
public function hookOrderby($params)
{
2006-12-29 17:01:31 +03:00
if ( ! is_array($params)) {
2006-12-26 23:33:40 +03:00
return false;
2006-12-29 17:01:31 +03:00
}
foreach ($params as $name) {
2006-12-26 23:33:40 +03:00
$e = explode(' ', $name);
$order = 'ASC';
2006-12-29 17:01:31 +03:00
if (count($e) > 1) {
2006-12-26 23:33:40 +03:00
$order = ($e[1] == 'DESC') ? 'DESC' : 'ASC';
}
$e = explode('.', $e[0]);
2006-12-29 17:01:31 +03:00
if (count($e) == 2) {
2006-12-26 23:33:40 +03:00
list($alias, $column) = $e;
2007-07-06 03:47:48 +04:00
2007-05-26 20:49:58 +04:00
$map = $this->query->getAliasDeclaration($alias);
$table = $map['table'];
2006-12-26 23:33:40 +03:00
2007-07-06 03:47:48 +04:00
if ($def = $table->getDefinitionOf($column)) {
2006-12-26 23:33:40 +03:00
$this->query->addOrderBy($alias . '.' . $column . ' ' . $order);
2007-07-06 03:47:48 +04:00
}
2006-12-26 23:33:40 +03:00
}
}
2007-01-27 13:08:06 +03:00
return true;
2006-12-15 23:10:23 +03:00
}
2006-12-15 23:10:23 +03:00
/**
* @param integer $limit
*/
2006-12-29 17:40:47 +03:00
public function hookLimit($limit)
{
2006-12-26 23:33:40 +03:00
$this->query->limit((int) $limit);
2006-12-15 23:10:23 +03:00
}
2006-12-15 23:10:23 +03:00
/**
* @param integer $offset
*/
2006-12-29 17:40:47 +03:00
public function hookOffset($offset)
{
2006-12-26 23:33:40 +03:00
$this->query->offset((int) $offset);
2006-12-15 23:10:23 +03:00
}
}