From b1889a440ba757a1374f6d94fdf3ca7df551e369 Mon Sep 17 00:00:00 2001 From: Benjamin Aerni Date: Tue, 11 Oct 2022 12:46:18 -0700 Subject: [PATCH 1/2] Update RedBeanPHP to 5.7.3 from 5.7 --- src/rb.php | 2129 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 1718 insertions(+), 411 deletions(-) diff --git a/src/rb.php b/src/rb.php index e68fddf1f..8a6811e51 100644 --- a/src/rb.php +++ b/src/rb.php @@ -209,7 +209,7 @@ class Debug extends RDefault implements Logger * Toggles CLI override. By default debugging functions will * output differently based on PHP_SAPI values. This function * allows you to override the PHP_SAPI setting. If you set - * this to TRUE, CLI output will be supressed in favour of + * this to TRUE, CLI output will be suppressed in favour of * HTML output. So, to get HTML on the command line use * setOverrideCLIOutput( TRUE ). * @@ -288,7 +288,7 @@ class Debug extends RDefault implements Logger } /** - * Dependending on the current mode of operation, + * Depending on the current mode of operation, * this method will either log and output to STDIN or * just log. * @@ -304,7 +304,7 @@ class Debug extends RDefault implements Logger $this->logs[] = $str; if ( !$this->mode ) { $highlight = FALSE; - /* just a quick heuritsic to highlight schema changes */ + /* just a quick heuristic to highlight schema changes */ if ( strpos( $str, 'CREATE' ) === 0 || strpos( $str, 'ALTER' ) === 0 || strpos( $str, 'DROP' ) === 0) { @@ -493,7 +493,7 @@ interface Driver * @param string $sql SQL query to execute * @param array $bindings list of values to bind to SQL snippet * - * @return mixed + * @return array */ public function GetAssocRow( $sql, $bindings = array() ); @@ -521,7 +521,7 @@ interface Driver * @param string $sql SQL query to execute * @param array $bindings list of values to bind to SQL snippet * - * @return array Affected Rows + * @return int Affected Rows */ public function Execute( $sql, $bindings = array() ); @@ -547,7 +547,7 @@ interface Driver * @param string $sql SQL query to execute * @param array $bindings list of values to bind to SQL snippet * - * @return mixed + * @return Cursor */ public function GetCursor( $sql, $bindings = array() ); @@ -580,14 +580,14 @@ interface Driver public function setDebugMode( $tf, $customLogger ); /** - * Starts a transaction. + * Commits a transaction. * * @return void */ public function CommitTrans(); /** - * Commits a transaction. + * Starts a transaction. * * @return void */ @@ -617,7 +617,7 @@ interface Driver /** * Sets initialization code for connection. * - * @param callable $code code + * @param callable|NULL $code code * * @return void */ @@ -676,12 +676,12 @@ class RPDO implements Driver protected $loggingEnabled = FALSE; /** - * @var Logger + * @var Logger|NULL */ protected $logger = NULL; /** - * @var PDO + * @var \PDO|NULL */ protected $pdo; @@ -691,7 +691,7 @@ class RPDO implements Driver protected $affectedRows; /** - * @var integer + * @var array */ protected $resultArray; @@ -731,12 +731,12 @@ class RPDO implements Driver protected $stringifyFetches = TRUE; /** - * @var string + * @var string|NULL */ protected $initSQL = NULL; /** - * @var callable + * @var callable|NULL */ protected $initCode = NULL; @@ -745,8 +745,8 @@ class RPDO implements Driver * Query Execution. This method binds parameters as NULL, INTEGER or STRING * and supports both named keys and question mark keys. * - * @param PDOStatement $statement PDO Statement instance - * @param array $bindings values that need to get bound to the statement + * @param \PDOStatement $statement PDO Statement instance + * @param array $bindings values that need to get bound to the statement * * @return void */ @@ -819,7 +819,11 @@ class RPDO implements Driver $this->resultArray = array(); return $statement; } - $this->resultArray = $statement->fetchAll( $fetchStyle ); + if ( is_null( $fetchStyle) ) { + $this->resultArray = $statement->fetchAll(); + } else { + $this->resultArray = $statement->fetchAll( $fetchStyle ); + } if ( $this->loggingEnabled && $this->logger ) { $this->logger->log( 'resultset: ' . count( $this->resultArray ) . ' rows' ); } @@ -876,7 +880,7 @@ class RPDO implements Driver * By default, RedBeanPHP uses this method under the hood to make sure * you use the latest UTF8 encoding possible for your database. * - * @param $db_cap identifier of database capability + * @param string $db_cap identifier of database capability * * @return int|false Whether the database feature is supported, FALSE otherwise. **/ @@ -931,8 +935,8 @@ class RPDO implements Driver * The second example shows how to create an RPDO instance * from an existing PDO object. * - * @param string|object $dsn database connection string - * @param string $user optional, usename to sign in + * @param string|\PDO $dsn database connection string + * @param string $user optional, username to sign in * @param string $pass optional, password for connection login * * @return void @@ -1051,7 +1055,7 @@ class RPDO implements Driver /** * Sets initialization code to execute upon connecting. * - * @param callable $code + * @param callable|NULL $code * * @return void */ @@ -1120,7 +1124,7 @@ class RPDO implements Driver * - RedBeanPHP will ask database driver to throw Exceptions on errors (recommended for compatibility) * - RedBeanPHP will ask database driver to use associative arrays when fetching (recommended for compatibility) * - * @param PDO $pdo PDO instance + * @param \PDO $pdo PDO instance * @param array $options Options to apply * * @return void @@ -1142,7 +1146,7 @@ class RPDO implements Driver if ( isset($options['runInitCode']) ) $runInitCode = $options['runInitCode']; if ( isset($options['stringFetch']) ) $stringFetch = $options['stringFetch']; - if ($connected) $this->connected = $connected; + if ($connected) $this->isConnected = $connected; if ($setEncoding) $this->setEncoding(); if ($setAttributes) { $this->pdo->setAttribute( \PDO::ATTR_ERRMODE,\PDO::ERRMODE_EXCEPTION ); @@ -1224,7 +1228,7 @@ class RPDO implements Driver * @param string $sql SQL * @param array $bindings bindings * - * @return mixed + * @return string|NULL */ public function GetCell( $sql, $bindings = array() ) { @@ -1246,7 +1250,7 @@ class RPDO implements Driver } /** - * @see Driver::Excecute + * @see Driver::Execute */ public function Execute( $sql, $bindings = array() ) { @@ -1445,7 +1449,7 @@ class RPDO implements Driver * $pdo = R::getDatabaseAdapter()->getDatabase()->getPDO(); * * - * @return PDO + * @return \PDO */ public function getPDO() { @@ -1587,6 +1591,7 @@ namespace RedBeanPHP { use RedBeanPHP\QueryWriter\AQueryWriter as AQueryWriter; use RedBeanPHP\BeanHelper as BeanHelper; use RedBeanPHP\RedException as RedException; +use RedBeanPHP\Util\Either as Either; /** * PHP 5.3 compatibility @@ -1680,12 +1685,12 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * rich functionality, otherwise you would have to do everything with R or * external objects. * - * @var BeanHelper + * @var BeanHelper|NULL */ protected $beanHelper = NULL; /** - * @var null + * @var string|NULL */ protected $fetchType = NULL; @@ -1700,12 +1705,12 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable protected $withParams = array(); /** - * @var string + * @var string|NULL */ protected $aliasName = NULL; /** - * @var string + * @var string|NULL */ protected $via = NULL; @@ -1719,11 +1724,16 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable */ protected $all = FALSE; + /** + * @var string|NULL + */ + protected $castProperty = NULL; + /** * If fluid count is set to TRUE then $bean->ownCount() will * return 0 if the table does not exists. * Only for backward compatibility. - * Returns previouds value. + * Returns previous value. * * @param boolean $toggle toggle * @@ -1828,7 +1838,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * fetchAs and setAutoResolve but explicitly. For instance if you register * the alias 'cover' for 'page' a property containing a reference to a * page bean called 'cover' will correctly return the page bean and not - * a (non-existant) cover bean. + * a (non-existent) cover bean. * * * R::aliases( array( 'cover' => 'page' ) ); @@ -2009,8 +2019,8 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * Unless you know what you are doing, do NOT use this method. * This is for advanced users only! * - * @param string $type type of the new bean - * @param BeanHelper $beanhelper bean helper to obtain a toolbox and a model + * @param string $type type of the new bean + * @param BeanHelper|NULL $beanhelper bean helper to obtain a toolbox and a model * * @return void */ @@ -2057,8 +2067,9 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * * Note that not all PHP functions work with the array interface. * - * @return ArrayIterator + * @return \ArrayIterator */ + #[\ReturnTypeWillChange] public function getIterator() { return new \ArrayIterator( $this->properties ); @@ -2067,7 +2078,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable /** * Imports all values from an associative array $array. Chainable. * This method imports the values in the first argument as bean - * propery and value pairs. Use the second parameter to provide a + * property and value pairs. Use the second parameter to provide a * selection. If a selection array is passed, only the entries * having keys mentioned in the selection array will be imported. * Set the third parameter to TRUE to preserve spaces in selection keys. @@ -2123,6 +2134,21 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable return $this; } + /** + * Same as import() but trims all values by default. + * Set the second parameter to apply a different function. + * + * @param array $array what you want to import + * @param callable $function function to apply (default is trim) + * @param string|array $selection selection of values + * @param boolean $notrim if TRUE selection keys will NOT be trimmed + * + * @return OODBBean + */ + public function trimport( $array, $function='trim', $selection = FALSE, $notrim = FALSE ) { + return $this->import( array_map( $function, $array ), $selection, $notrim ); + } + /** * Imports an associative array directly into the * internal property array of the bean as well as the @@ -2300,11 +2326,11 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * Returns the ID of the bean. * If for some reason the ID has not been set, this method will * return NULL. This is actually the same as accessing the - * id property using $bean->id. The ID of a bean is it's primary + * id property using $bean->id. The ID of a bean is its primary * key and should always correspond with a table column named * 'id'. * - * @return string|null + * @return string|NULL */ public function getID() { @@ -2336,6 +2362,19 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable return; } + /** + * Returns the bean wrapped in an Either-instance. + * This allows the user to extract data from the bean using a chain + * of methods without any NULL checks, similar to the ?? operator but also + * in a way that is compatible with older versions of PHP. + * For more details consult the documentation of the Either class. + * + * @return Either + */ + public function either() { + return new Either( $this ); + } + /** * Adds WHERE clause conditions to ownList retrieval. * For instance to get the pages that belong to a book you would @@ -2566,13 +2605,14 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable $this->noLoad = FALSE; $this->all = FALSE; $this->via = NULL; + $this->castProperty = NULL; return $this; } /** * Determines whether a list is opened in exclusive mode or not. * If a list has been opened in exclusive mode this method will return TRUE, - * othwerwise it will return FALSE. + * otherwise it will return FALSE. * * @param string $listName name of the list to check * @@ -2667,7 +2707,12 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable //If exists and no list or exits and list not changed, bail out. if ( $exists && ((!$isOwn && !$isShared ) || (!$hasSQL && !$differentAlias && !$hasAll)) ) { + $castProperty = $this->castProperty; $this->clearModifiers(); + if (!is_null($castProperty)) { + $object = new $castProperty( $this->properties[$property] ); + return $object; + } return $this->properties[$property]; } @@ -2708,7 +2753,6 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable $this->properties[$property] = $beans; $this->__info["sys.shadow.$property"] = $beans; $this->__info['tainted'] = TRUE; - $this->clearModifiers(); return $this->properties[$property]; @@ -2776,8 +2820,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable $this->__info['tainted'] = TRUE; $this->__info['changed'] = TRUE; - /* BoxBilling hack */ - /*array_push( $this->__info['changelist'], $property );*/ + array_push( $this->__info['changelist'], $property ); if ( array_key_exists( $fieldLink, $this->properties ) && !( $value instanceof OODBBean ) ) { if ( is_null( $value ) || $value === FALSE ) { @@ -2801,7 +2844,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable $value = '0'; } elseif ( $value === TRUE ) { $value = '1'; - /* for some reason there is some kind of bug in xdebug so that it doesnt count this line otherwise... */ + /* for some reason there is some kind of bug in xdebug so that it doesn't count this line otherwise... */ } elseif ( $value instanceof \DateTime ) { $value = $value->format( 'Y-m-d H:i:s' ); } $this->properties[$property] = $value; } @@ -2939,6 +2982,29 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable return $this; } + /** + * Captures a dynamic casting. + * Enables you to obtain a bean value as an object by type-hinting + * the desired return object using asX where X is the class you wish + * to use as a wrapper for the property. + * + * Usage: + * + * $dateTime = $bean->asDateTime()->date; + * + * @param string $method method (asXXX)... + * + * @return self|NULL + */ + public function captureDynamicCasting( $method ) + { + if ( strpos( $method, 'as' ) === 0 && ctype_upper( substr( $method, 2, 1) ) === TRUE ) { + $this->castProperty = substr( $method, 2 ); + return $this; + } + return NULL; + } + /** * Sends the call to the registered model. * This method can also be used to override bean behaviour. @@ -2964,7 +3030,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable public function __call( $method, $args ) { if ( empty( $this->__info['model'] ) ) { - return NULL; + return $this->captureDynamicCasting($method); } $overrideDontFail = FALSE; @@ -2975,6 +3041,9 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable if ( !is_callable( array( $this->__info['model'], $method ) ) ) { + $self = $this->captureDynamicCasting($method); + if ($self) return $self; + if ( self::$errorHandlingFUSE === FALSE || $overrideDontFail ) { return NULL; } @@ -3053,6 +3122,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * * @return void */ + #[\ReturnTypeWillChange] public function offsetSet( $offset, $value ) { $this->__set( $offset, $value ); @@ -3070,6 +3140,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * * @return boolean */ + #[\ReturnTypeWillChange] public function offsetExists( $offset ) { return $this->__isset( $offset ); @@ -3081,13 +3152,14 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * Unsets a value from the array/bean. * * Array functions do not reveal x-own-lists and list-alias because - * you dont want duplicate entries in foreach-loops. + * you don't want duplicate entries in foreach-loops. * Also offers a slight performance improvement for array access. * * @param mixed $offset property * * @return void */ + #[\ReturnTypeWillChange] public function offsetUnset( $offset ) { $this->__unset( $offset ); @@ -3099,13 +3171,14 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * Returns value of a property. * * Array functions do not reveal x-own-lists and list-alias because - * you dont want duplicate entries in foreach-loops. + * you don't want duplicate entries in foreach-loops. * Also offers a slight performance improvement for array access. * * @param mixed $offset property * * @return mixed */ + #[\ReturnTypeWillChange] public function &offsetGet( $offset ) { return $this->__get( $offset ); @@ -3183,7 +3256,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * * @param string $property property * @param callable $function function - * @param integer $maxDepth maximum depth for traversal + * @param integer|NULL $maxDepth maximum depth for traversal * * @return OODBBean * @throws RedException @@ -3246,6 +3319,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * * @return integer */ + #[\ReturnTypeWillChange] public function count() { return count( $this->properties ); @@ -3313,7 +3387,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable } /** - * Convience method. + * Convenience method. * Unsets all properties in the internal properties array. * * Usage: @@ -3396,7 +3470,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * * Note that this method will return TRUE if applied to a loaded list. * Also note that this method keeps track of the bean's history regardless whether - * it has been stored or not. Storing a bean does not undo it's history, + * it has been stored or not. Storing a bean does not undo its history, * to clean the history of a bean use: clearHistory(). * * @param string $property name of the property you want the change-status of @@ -3761,9 +3835,9 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * quest beans residing in the $questTarget->target properties * of each element in the xownQuestTargetList. * - * @param string $list the list you wish to process - * @param string $property the property to load - * @param string $type the type of bean residing in this property (optional) + * @param string $list the list you wish to process + * @param string $property the property to load + * @param string|NULL $type the type of bean residing in this property (optional) * * @return array */ @@ -3819,7 +3893,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * * * The example above compares the flavour label 'mocca' with - * the flavour label attachec to the $coffee bean. This illustrates + * the flavour label attached to the $coffee bean. This illustrates * how to use equals() with RedBeanPHP-style enums. * * @param OODBBean|null $bean other bean @@ -3853,6 +3927,7 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable * * @return array */ + #[\ReturnTypeWillChange] public function jsonSerialize() { $json = $this->__call( '@__jsonSerialize', array( ) ); @@ -4011,7 +4086,7 @@ interface Adapter * @param array $bindings array of values to bind to parameters in query string * @param boolean $noevent no event firing * - * @return void + * @return int */ public function exec( $sql, $bindings = array(), $noevent = FALSE ); @@ -4037,7 +4112,7 @@ interface Adapter * @param string $sql string containing SQL code for database * @param array $bindings array of values to bind to parameters in query string * - * @return array + * @return array|NULL */ public function getRow( $sql, $bindings = array() ); @@ -4063,7 +4138,7 @@ interface Adapter * @param string $sql string containing SQL code for database * @param array $bindings array of values to bind to parameters in query string * - * @return string + * @return string|NULL */ public function getCell( $sql, $bindings = array() ); @@ -4082,7 +4157,7 @@ interface Adapter * Executes the SQL query specified in $sql and returns * an associative array where the column names are the keys. * - * @param string $sql Sstring containing SQL code for databaseQL + * @param string $sql String containing SQL code for databaseQL * @param array $bindings values to bind * * @return array @@ -4120,7 +4195,7 @@ interface Adapter * adapter. RedBean will only access the adapter and never to talk * directly to the driver though. * - * @return mixed + * @return Driver */ public function getDatabase(); @@ -4480,7 +4555,7 @@ namespace RedBeanPHP { * Database Cursor Interface. * A cursor is used by Query Writers to fetch Query Result rows * one row at a time. This is useful if you expect the result set to - * be quite large. This interface dscribes the API of a database + * be quite large. This interface describes the API of a database * cursor. There can be multiple implementations of the Cursor, * by default RedBeanPHP offers the PDOCursor for drivers shipping * with RedBeanPHP and the NULLCursor. @@ -4500,7 +4575,7 @@ interface Cursor * Should retrieve the next row of the result set. * This method is used to iterate over the result set. * - * @return array + * @return array|NULL */ public function getNextItem(); @@ -4547,7 +4622,7 @@ use RedBeanPHP\Cursor as Cursor; class PDOCursor implements Cursor { /** - * @var PDOStatement + * @var \PDOStatement */ protected $res; @@ -4559,7 +4634,7 @@ class PDOCursor implements Cursor /** * Constructor, creates a new instance of a PDO Database Cursor. * - * @param PDOStatement $res the PDO statement + * @param \PDOStatement $res the PDO statement * @param string $fetchStyle fetch style constant to use * * @return void @@ -4690,20 +4765,27 @@ class BeanCollection */ protected $type = NULL; + /** + * @var string + */ + protected $mask = NULL; + /** * Constructor, creates a new instance of the BeanCollection. * * @param string $type type of beans in this collection * @param Repository $repository repository to use to generate bean objects * @param Cursor $cursor cursor object to use + * @param string $mask meta mask to apply (optional) * * @return void */ - public function __construct( $type, Repository $repository, Cursor $cursor ) + public function __construct( $type, Repository $repository, Cursor $cursor, $mask = '__meta' ) { $this->type = $type; $this->cursor = $cursor; $this->repository = $repository; + $this->mask = $mask; } /** @@ -4718,7 +4800,7 @@ class BeanCollection { $row = $this->cursor->getNextItem(); if ( $row ) { - $beans = $this->repository->convertToBeans( $this->type, array( $row ) ); + $beans = $this->repository->convertToBeans( $this->type, array( $row ), $this->mask ); return reset( $beans ); } return NULL; @@ -4852,7 +4934,7 @@ interface QueryWriter * @param boolean $firstOfChain is it the join of a chain (or the only join) * @param string $suffix suffix to add for aliasing tables (for joining same table multiple times) * - * @return string $joinSQLSnippet + * @return string */ public function writeJoin( $type, $targetType, $leftRight, $joinType, $firstOfChain, $suffix ); @@ -4882,8 +4964,8 @@ interface QueryWriter * @note A default implementation is available in AQueryWriter * unless a database uses very different SQL this should suffice. * - * @param string $sql SQL Snippet - * @param integer $glue the GLUE type - how to glue (C_GLUE_WHERE or C_GLUE_AND) + * @param string $sql SQL Snippet + * @param integer|NULL $glue the GLUE type - how to glue (C_GLUE_WHERE or C_GLUE_AND) * * @return string */ @@ -4948,7 +5030,7 @@ interface QueryWriter * C_DATA_TYPE_MANUAL (usually 99) which represents a user specified type. Although * no special treatment has been associated with the latter for now. * - * @param string $value value + * @param mixed $value value * @param boolean $alsoScanSpecialForTypes take special types into account * * @return integer @@ -4998,10 +5080,10 @@ interface QueryWriter * This methods selects the records from the database that match the specified * type, conditions (optional) and additional SQL snippet (optional). * - * @param string $type name of the table you want to query - * @param array $conditions criteria ( $column => array( $values ) ) - * @param string $addSql additional SQL snippet - * @param array $bindings bindings for SQL snippet + * @param string $type name of the table you want to query + * @param array $conditions criteria ( $column => array( $values ) ) + * @param string|NULL $addSql additional SQL snippet + * @param array $bindings bindings for SQL snippet * * @return array */ @@ -5012,10 +5094,10 @@ interface QueryWriter * This methods selects the records from the database that match the specified * type, conditions (optional) and additional SQL snippet (optional). * - * @param string $type name of the table you want to query - * @param array $conditions criteria ( $column => array( $values ) ) - * @param string $addSQL additional SQL snippet - * @param array $bindings bindings for SQL snippet + * @param string $type name of the table you want to query + * @param array $conditions criteria ( $column => array( $values ) ) + * @param string|NULL $addSql additional SQL snippet + * @param array $bindings bindings for SQL snippet * * @return Cursor */ @@ -5036,7 +5118,7 @@ interface QueryWriter public function queryRecordRelated( $sourceType, $destType, $linkID, $addSql = '', $bindings = array() ); /** - * Returns the row that links $sourceType $sourcID to $destType $destID in an N-M relation. + * Returns the row that links $sourceType $sourceID to $destType $destID in an N-M relation. * * @param string $sourceType source type, the first part of the link you're looking for * @param string $destType destination type, the second part of the link you're looking for @@ -5051,10 +5133,10 @@ interface QueryWriter * Counts the number of records in the database that match the * conditions and additional SQL. * - * @param string $type name of the table you want to query - * @param array $conditions criteria ( $column => array( $values ) ) - * @param string $addSQL additional SQL snippet - * @param array $bindings bindings for SQL snippet + * @param string $type name of the table you want to query + * @param array $conditions criteria ( $column => array( $values ) ) + * @param string|NULL $addSql additional SQL snippet + * @param array $bindings bindings for SQL snippet * * @return integer */ @@ -5114,12 +5196,12 @@ interface QueryWriter * (count rows) used for countParents and countChildren functions - or you can specify a * string yourself like 'count(distinct brand)'. * - * @param string $type the bean type you want to query rows for - * @param integer $id id of the reference row - * @param boolean $up TRUE to query parent rows, FALSE to query child rows - * @param string $addSql optional SQL snippet to embed in the query - * @param array $bindings parameter bindings for additional SQL snippet - * @param mixed $select Select Snippet to use when querying (optional) + * @param string $type the bean type you want to query rows for + * @param integer $id id of the reference row + * @param boolean $up TRUE to query parent rows, FALSE to query child rows + * @param string|NULL $addSql optional SQL snippet to embed in the query + * @param array $bindings parameter bindings for additional SQL snippet + * @param bool $select Select Snippet to use when querying (optional) * * @return array */ @@ -5133,9 +5215,9 @@ interface QueryWriter * Returns the new ID. * This methods accepts a type and infers the corresponding table name. * - * @param string $type name of the table to update - * @param array $updatevalues list of update values - * @param integer $id optional primary key ID value + * @param string $type name of the table to update + * @param array $updatevalues list of update values + * @param integer|NULL $id optional primary key ID value * * @return integer */ @@ -5150,7 +5232,7 @@ interface QueryWriter * @param string $addSql additional SQL * @param array $bindings bindings * - * @return void + * @return int */ public function deleteRecord( $type, $conditions = array(), $addSql = '', $bindings = array() ); @@ -5166,7 +5248,7 @@ interface QueryWriter public function deleteRelations( $sourceType, $destType, $sourceID ); /** - * @see QueryWriter::addUniqueConstaint + * @see QueryWriter::addUniqueConstraint */ public function addUniqueIndex( $type, $columns ); @@ -5222,7 +5304,7 @@ interface QueryWriter * @param string $targetType points to this type * @param string $property field that contains the foreign key value * @param string $targetProperty field where the fk points to - * @param string $isDep whether target is dependent and should cascade on update/delete + * @param bool $isDep whether target is dependent and should cascade on update/delete * * @return void */ @@ -5445,9 +5527,9 @@ abstract class AQueryWriter * If no template can be found for the specified type, the template for * '*' will be returned instead. * - * @param string $type ( 'createTable' | 'widenColumn' | 'addColumn' ) - * @param string $beanType ( type of bean or '*' to apply to all types ) - * @param string $property specify if you're looking for a property-specific template + * @param string $type ( 'createTable' | 'widenColumn' | 'addColumn' ) + * @param string $beanType ( type of bean or '*' to apply to all types ) + * @param string|NULL $property specify if you're looking for a property-specific template * * @return string */ @@ -5743,9 +5825,9 @@ abstract class AQueryWriter * In previous versions you could only store one key-entry, I have changed this to * improve caching efficiency (issue #400). * - * @param string $cacheTag cache tag (secondary key) - * @param string $key key to store values under - * @param array $values content to be stored + * @param string $cacheTag cache tag (secondary key) + * @param string $key key to store values under + * @param array|int $values rows or count to be stored * * @return void */ @@ -5877,7 +5959,7 @@ abstract class AQueryWriter /** * Determines whether a string can be considered JSON or not. * This is used by writers that support JSON columns. However - * we dont want that code duplicated over all JSON supporting + * we don't want that code duplicated over all JSON supporting * Query Writers. * * @param string $value value to determine 'JSONness' of. @@ -6041,7 +6123,7 @@ abstract class AQueryWriter * Used for UUID support. * * @param integer $dataTypeID magic number constant assigned to this data type - * @param string $SQLDefinition SQL column definition (i.e. INT(11)) + * @param string $SQLDefinition SQL column definition (e.g. INT(11)) * * @return self */ @@ -6171,6 +6253,10 @@ abstract class AQueryWriter { static $snippetCache = array(); + if ( is_null( $sql ) ) { + return ''; + } + if ( trim( $sql ) === '' ) { return $sql; } @@ -7353,7 +7439,7 @@ class MySQL extends AQueryWriter implements QueryWriter ADD UNIQUE INDEX $name (" . implode( ',', $columns ) . ")"; $this->adapter->exec( $sql ); } catch ( SQLException $e ) { - //do nothing, dont use alter table ignore, this will delete duplicate records in 3-ways! + //do nothing, don't use alter table ignore, this will delete duplicate records in 3-ways! return FALSE; } return TRUE; @@ -7377,7 +7463,6 @@ class MySQL extends AQueryWriter implements QueryWriter /** * @see QueryWriter::addFK - * @return bool */ public function addFK( $type, $targetType, $property, $targetProperty, $isDependent = FALSE ) { @@ -7453,6 +7538,938 @@ class MySQL extends AQueryWriter implements QueryWriter } } +namespace RedBeanPHP\QueryWriter { + +use RedBeanPHP\QueryWriter\AQueryWriter as AQueryWriter; +use RedBeanPHP\QueryWriter as QueryWriter; +use RedBeanPHP\Adapter\DBAdapter as DBAdapter; +use RedBeanPHP\Adapter as Adapter; +use RedBeanPHP\RedException\SQL as SQLException; + +/** + * RedBeanPHP SQLiteWriter with support for SQLite types + * This is a QueryWriter class for RedBeanPHP. + * This QueryWriter provides support for the SQLite database platform. + * + * @file RedBeanPHP/QueryWriter/SQLiteT.php + * @author Gabor de Mooij and the RedBeanPHP Community + * @license BSD/GPLv2 + * + * @copyright + * (c) copyright G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community. + * This source file is subject to the BSD/GPLv2 License that is bundled + * with this source code in the file license.txt. + */ +class SQLiteT extends AQueryWriter implements QueryWriter +{ + /** + * Data types + */ + const C_DATATYPE_INTEGER = 0; + const C_DATATYPE_NUMERIC = 1; + const C_DATATYPE_TEXT = 2; + const C_DATATYPE_SPECIFIED = 99; + + /** + * @var DBAdapter + */ + protected $adapter; + + /** + * @var string + */ + protected $quoteCharacter = '`'; + + /** + * @var array + */ + protected $DDLTemplates = array( + 'addColumn' => array( + '*' => 'ALTER TABLE `%s` ADD `%s` %s' + ), + 'createTable' => array( + '*' => 'CREATE TABLE %s ( id INTEGER PRIMARY KEY AUTOINCREMENT )' + ), + 'widenColumn' => array( + '*' => ',`%s` %s ' + ) + ); + + /** + * Gets all information about a table (from a type). + * + * Format: + * array( + * name => name of the table + * columns => array( name => datatype ) + * indexes => array() raw index information rows from PRAGMA query + * keys => array() raw key information rows from PRAGMA query + * ) + * + * @param string $type type you want to get info of + * + * @return array + */ + protected function getTable( $type ) + { + $tableName = $this->esc( $type, TRUE ); + $columns = $this->getColumns( $type ); + $indexes = $this->getIndexes( $type ); + $keys = $this->getKeyMapForType( $type ); + + $table = array( + 'columns' => $columns, + 'indexes' => $indexes, + 'keys' => $keys, + 'name' => $tableName + ); + + $this->tableArchive[$tableName] = $table; + + return $table; + } + + /** + * Puts a table. Updates the table structure. + * In SQLite we can't change columns, drop columns, change or add foreign keys so we + * have a table-rebuild function. You simply load your table with getTable(), modify it and + * then store it with putTable()... + * + * @param array $tableMap information array + * + * @return void + */ + protected function putTable( $tableMap ) + { + $table = $tableMap['name']; + $q = array(); + $q[] = "DROP TABLE IF EXISTS tmp_backup;"; + + $oldColumnNames = array_keys( $this->getColumns( $table ) ); + + foreach ( $oldColumnNames as $k => $v ) $oldColumnNames[$k] = "`$v`"; + + $q[] = "CREATE TEMPORARY TABLE tmp_backup(" . implode( ",", $oldColumnNames ) . ");"; + $q[] = "INSERT INTO tmp_backup SELECT * FROM `$table`;"; + $q[] = "PRAGMA foreign_keys = 0 "; + $q[] = "DROP TABLE `$table`;"; + + $newTableDefStr = ''; + foreach ( $tableMap['columns'] as $column => $type ) { + if ( $column != 'id' ) { + $newTableDefStr .= sprintf( $this->getDDLTemplate( 'widenColumn', $table, $column ), $column, $type ); + } + } + + $fkDef = ''; + foreach ( $tableMap['keys'] as $key ) { + $fkDef .= ", FOREIGN KEY(`{$key['from']}`) + REFERENCES `{$key['table']}`(`{$key['to']}`) + ON DELETE {$key['on_delete']} ON UPDATE {$key['on_update']}"; + } + + $q[] = "CREATE TABLE `$table` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT $newTableDefStr $fkDef );"; + + foreach ( $tableMap['indexes'] as $name => $index ) { + if ( strpos( $name, 'UQ_' ) === 0 ) { + $cols = explode( '__', substr( $name, strlen( 'UQ_' . $table ) ) ); + foreach ( $cols as $k => $v ) $cols[$k] = "`$v`"; + $q[] = "CREATE UNIQUE INDEX $name ON `$table` (" . implode( ',', $cols ) . ")"; + } else $q[] = "CREATE INDEX $name ON `$table` ({$index['name']}) "; + } + + $q[] = "INSERT INTO `$table` SELECT * FROM tmp_backup;"; + $q[] = "DROP TABLE tmp_backup;"; + $q[] = "PRAGMA foreign_keys = 1 "; + + foreach ( $q as $sq ) $this->adapter->exec( $sq ); + } + + /** + * Returns the an array describing the indexes for type $type. + * + * @param string $type type to describe indexes of + * + * @return array + */ + protected function getIndexes( $type ) + { + $table = $this->esc( $type, TRUE ); + $indexes = $this->adapter->get( "PRAGMA index_list('$table')" ); + + $indexInfoList = array(); + foreach ( $indexes as $i ) { + $indexInfoList[$i['name']] = $this->adapter->getRow( "PRAGMA index_info('{$i['name']}') " ); + + $indexInfoList[$i['name']]['unique'] = $i['unique']; + } + + return $indexInfoList; + } + + /** + * Adds a foreign key to a type. + * Note: cant put this in try-catch because that can hide the fact + * that database has been damaged. + * + * @param string $type type you want to modify table of + * @param string $targetType target type + * @param string $field field of the type that needs to get the fk + * @param string $targetField field where the fk needs to point to + * @param integer $buildopt 0 = NO ACTION, 1 = ON DELETE CASCADE + * + * @return boolean + */ + protected function buildFK( $type, $targetType, $property, $targetProperty, $constraint = FALSE ) + { + $table = $this->esc( $type, TRUE ); + $targetTable = $this->esc( $targetType, TRUE ); + $column = $this->esc( $property, TRUE ); + $targetColumn = $this->esc( $targetProperty, TRUE ); + + $tables = $this->getTables(); + if ( !in_array( $targetTable, $tables ) ) return FALSE; + + if ( !is_null( $this->getForeignKeyForTypeProperty( $table, $column ) ) ) return FALSE; + $t = $this->getTable( $table ); + $consSQL = ( $constraint ? 'CASCADE' : 'SET NULL' ); + $label = 'from_' . $column . '_to_table_' . $targetTable . '_col_' . $targetColumn; + $t['keys'][$label] = array( + 'table' => $targetTable, + 'from' => $column, + 'to' => $targetColumn, + 'on_update' => $consSQL, + 'on_delete' => $consSQL + ); + $this->putTable( $t ); + return TRUE; + } + + /** + * @see AQueryWriter::getKeyMapForType + */ + protected function getKeyMapForType( $type ) + { + $table = $this->esc( $type, TRUE ); + $keys = $this->adapter->get( "PRAGMA foreign_key_list('$table')" ); + $keyInfoList = array(); + foreach ( $keys as $k ) { + $label = $this->makeFKLabel( $k['from'], $k['table'], $k['to'] ); + $keyInfoList[$label] = array( + 'name' => $label, + 'from' => $k['from'], + 'table' => $k['table'], + 'to' => $k['to'], + 'on_update' => $k['on_update'], + 'on_delete' => $k['on_delete'] + ); + } + return $keyInfoList; + } + + /** + * Constructor + * Most of the time, you do not need to use this constructor, + * since the facade takes care of constructing and wiring the + * RedBeanPHP core objects. However if you would like to + * assemble an OODB instance yourself, this is how it works: + * + * Usage: + * + * + * $database = new RPDO( $dsn, $user, $pass ); + * $adapter = new DBAdapter( $database ); + * $writer = new PostgresWriter( $adapter ); + * $oodb = new OODB( $writer, FALSE ); + * $bean = $oodb->dispense( 'bean' ); + * $bean->name = 'coffeeBean'; + * $id = $oodb->store( $bean ); + * $bean = $oodb->load( 'bean', $id ); + * + * + * The example above creates the 3 RedBeanPHP core objects: + * the Adapter, the Query Writer and the OODB instance and + * wires them together. The example also demonstrates some of + * the methods that can be used with OODB, as you see, they + * closely resemble their facade counterparts. + * + * The wiring process: create an RPDO instance using your database + * connection parameters. Create a database adapter from the RPDO + * object and pass that to the constructor of the writer. Next, + * create an OODB instance from the writer. Now you have an OODB + * object. + * + * @param Adapter $adapter Database Adapter + */ + public function __construct( Adapter $adapter ) + { + $this->typeno_sqltype = array( + SQLiteT::C_DATATYPE_INTEGER => 'INTEGER', + SQLiteT::C_DATATYPE_NUMERIC => 'NUMERIC', + SQLiteT::C_DATATYPE_TEXT => 'TEXT', + ); + + $this->sqltype_typeno = array(); + + foreach ( $this->typeno_sqltype as $k => $v ) { + $this->sqltype_typeno[$v] = $k; + } + + $this->adapter = $adapter; + $this->adapter->setOption( 'setInitQuery', ' PRAGMA foreign_keys = 1 ' ); + } + + /** + * This method returns the datatype to be used for primary key IDS and + * foreign keys. Returns one if the data type constants. + * + * @return integer $const data type to be used for IDS. + */ + public function getTypeForID() + { + return self::C_DATATYPE_INTEGER; + } + + /** + * @see QueryWriter::scanType + */ + public function scanType( $value, $flagSpecial = FALSE ) + { + $this->svalue = $value; + + if ( $value === NULL ) return self::C_DATATYPE_INTEGER; + if ( $value === INF ) return self::C_DATATYPE_TEXT; + + if ( $this->startsWithZeros( $value ) ) return self::C_DATATYPE_TEXT; + + if ( $value === TRUE || $value === FALSE ) return self::C_DATATYPE_INTEGER; + + if ( is_numeric( $value ) && ( intval( $value ) == $value ) && $value < 2147483648 && $value > -2147483648 ) return self::C_DATATYPE_INTEGER; + + if ( ( is_numeric( $value ) && $value < 2147483648 && $value > -2147483648) + || preg_match( '/\d{4}\-\d\d\-\d\d/', $value ) + || preg_match( '/\d{4}\-\d\d\-\d\d\s\d\d:\d\d:\d\d/', $value ) + ) { + return self::C_DATATYPE_NUMERIC; + } + + return self::C_DATATYPE_TEXT; + } + + /** + * @see QueryWriter::addColumn + */ + public function addColumn( $table, $column, $type ) + { + $column = $this->check( $column ); + $table = $this->check( $table ); + $type = $this->typeno_sqltype[$type]; + + $this->adapter->exec( sprintf( $this->getDDLTemplate( 'addColumn', $table, $column ), $table, $column, $type ) ); + } + + /** + * @see QueryWriter::code + */ + public function code( $typedescription, $includeSpecials = FALSE ) + { + $r = ( ( isset( $this->sqltype_typeno[$typedescription] ) ) ? $this->sqltype_typeno[$typedescription] : 99 ); + + return $r; + } + + /** + * @see QueryWriter::widenColumn + */ + public function widenColumn( $type, $column, $datatype ) + { + $t = $this->getTable( $type ); + + $t['columns'][$column] = $this->typeno_sqltype[$datatype]; + + $this->putTable( $t ); + } + + /** + * @see QueryWriter::getTables(); + */ + public function getTables() + { + return $this->adapter->getCol( "SELECT name FROM sqlite_master + WHERE type='table' AND name!='sqlite_sequence';" ); + } + + /** + * @see QueryWriter::createTable + */ + public function createTable( $type ) + { + $table = $this->esc( $type ); + + $sql = sprintf( $this->getDDLTemplate( 'createTable', $type ), $table ); + + $this->adapter->exec( $sql ); + } + + /** + * @see QueryWriter::getColumns + */ + public function getColumns( $table ) + { + $table = $this->esc( $table, TRUE ); + + $columnsRaw = $this->adapter->get( "PRAGMA table_info('$table')" ); + + $columns = array(); + foreach ( $columnsRaw as $r ) $columns[$r['name']] = $r['type']; + + return $columns; + } + + /** + * @see QueryWriter::addUniqueIndex + */ + public function addUniqueConstraint( $type, $properties ) + { + $tableNoQ = $this->esc( $type, TRUE ); + $name = 'UQ_' . $this->esc( $type, TRUE ) . implode( '__', $properties ); + $t = $this->getTable( $type ); + $t['indexes'][$name] = array( 'name' => $name ); + try { + $this->putTable( $t ); + } catch( SQLException $e ) { + return FALSE; + } + return TRUE; + } + + /** + * @see QueryWriter::sqlStateIn + */ + public function sqlStateIn( $state, $list, $extraDriverDetails = array() ) + { + $stateMap = array( + '23000' => QueryWriter::C_SQLSTATE_INTEGRITY_CONSTRAINT_VIOLATION + ); + if ( $state == 'HY000' + && isset($extraDriverDetails[1]) + && $extraDriverDetails[1] == 1 + && ( in_array( QueryWriter::C_SQLSTATE_NO_SUCH_TABLE, $list ) + || in_array( QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, $list ) + )) { + return TRUE; + } + return in_array( ( isset( $stateMap[$state] ) ? $stateMap[$state] : '0' ), $list ); + } + + /** + * Sets an SQL snippet to be used for the next queryRecord() operation. + * SQLite has no SELECT-FOR-UPDATE and filters this. + * + * @param string $sqlSelectSnippet SQL snippet to use in SELECT statement. + * + * @return self + */ + public function setSQLSelectSnippet( $sqlSelectSnippet = '' ) { + if ( $sqlSelectSnippet === AQueryWriter::C_SELECT_SNIPPET_FOR_UPDATE) $sqlSelectSnippet = ''; + $this->sqlSelectSnippet = $sqlSelectSnippet; + return $this; + } + + /** + * @see QueryWriter::addIndex + */ + public function addIndex( $type, $name, $column ) + { + $columns = $this->getColumns( $type ); + if ( !isset( $columns[$column] ) ) return FALSE; + + $table = $this->esc( $type ); + $name = preg_replace( '/\W/', '', $name ); + $column = $this->esc( $column, TRUE ); + + try { + $t = $this->getTable( $type ); + $t['indexes'][$name] = array( 'name' => $column ); + $this->putTable( $t ); + return TRUE; + } catch( SQLException $exception ) { + return FALSE; + } + } + + /** + * @see QueryWriter::wipe + */ + public function wipe( $type ) + { + $table = $this->esc( $type ); + + $this->adapter->exec( "DELETE FROM $table " ); + } + + /** + * @see QueryWriter::addFK + */ + public function addFK( $type, $targetType, $property, $targetProperty, $isDep = FALSE ) + { + return $this->buildFK( $type, $targetType, $property, $targetProperty, $isDep ); + } + + /** + * @see QueryWriter::wipeAll + */ + public function wipeAll() + { + if (AQueryWriter::$noNuke) throw new \Exception('The nuke() command has been disabled using noNuke() or R::feature(novice/...).'); + $this->adapter->exec( 'PRAGMA foreign_keys = 0 ' ); + + foreach ( $this->getTables() as $t ) { + try { $this->adapter->exec( "DROP TABLE IF EXISTS `$t`" ); } catch ( SQLException $e ) { ; } + try { $this->adapter->exec( "DROP TABLE IF EXISTS `$t`" ); } catch ( SQLException $e ) { ; } + } + + $this->adapter->exec( 'PRAGMA foreign_keys = 1 ' ); + } +} +} + +namespace RedBeanPHP\QueryWriter { + +use RedBeanPHP\QueryWriter\AQueryWriter as AQueryWriter; +use RedBeanPHP\QueryWriter as QueryWriter; +use RedBeanPHP\Adapter\DBAdapter as DBAdapter; +use RedBeanPHP\Adapter as Adapter; +use RedBeanPHP\RedException\SQL as SQLException; + +/** + * RedBeanPHP PostgreSQL Query Writer. + * This is a QueryWriter class for RedBeanPHP. + * This QueryWriter provides support for the PostgreSQL database platform. + * + * @file RedBeanPHP/QueryWriter/PostgreSQL.php + * @author Gabor de Mooij and the RedBeanPHP Community + * @license BSD/GPLv2 + * + * @copyright + * (c) copyright G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community. + * This source file is subject to the BSD/GPLv2 License that is bundled + * with this source code in the file license.txt. + */ +class PostgreSQL extends AQueryWriter implements QueryWriter +{ + /** + * Data types + */ + const C_DATATYPE_INTEGER = 0; + const C_DATATYPE_DOUBLE = 1; + const C_DATATYPE_TEXT = 3; + const C_DATATYPE_SPECIAL_DATE = 80; + const C_DATATYPE_SPECIAL_DATETIME = 81; + const C_DATATYPE_SPECIAL_TIME = 82; //TIME (no zone) only manual + const C_DATATYPE_SPECIAL_TIMEZ = 83; //TIME (plus zone) only manual + const C_DATATYPE_SPECIAL_POINT = 90; + const C_DATATYPE_SPECIAL_LSEG = 91; + const C_DATATYPE_SPECIAL_CIRCLE = 92; + const C_DATATYPE_SPECIAL_MONEY = 93; + const C_DATATYPE_SPECIAL_POLYGON = 94; + const C_DATATYPE_SPECIAL_MONEY2 = 95; //Numbers only money, i.e. fixed point numeric + const C_DATATYPE_SPECIAL_JSON = 96; //JSON support (only manual) + const C_DATATYPE_SPECIFIED = 99; + + /** + * @var DBAdapter + */ + protected $adapter; + + /** + * @var string + */ + protected $quoteCharacter = '"'; + + /** + * @var string + */ + protected $defaultValue = 'DEFAULT'; + + /** + * @var array + */ + protected $DDLTemplates = array( + 'addColumn' => array( + '*' => 'ALTER TABLE %s ADD %s %s ' + ), + 'createTable' => array( + '*' => 'CREATE TABLE %s (id SERIAL PRIMARY KEY) ' + ), + 'widenColumn' => array( + '*' => 'ALTER TABLE %s ALTER COLUMN %s TYPE %s' + ) + ); + + /** + * Returns the insert suffix SQL Snippet + * + * @param string $table table + * + * @return string $sql SQL Snippet + */ + protected function getInsertSuffix( $table ) + { + return 'RETURNING id '; + } + + /** + * @see AQueryWriter::getKeyMapForType + */ + protected function getKeyMapForType( $type ) + { + $table = $this->esc( $type, TRUE ); + $keys = $this->adapter->get( ' + SELECT + information_schema.key_column_usage.constraint_name AS "name", + information_schema.key_column_usage.column_name AS "from", + information_schema.constraint_table_usage.table_name AS "table", + information_schema.constraint_column_usage.column_name AS "to", + information_schema.referential_constraints.update_rule AS "on_update", + information_schema.referential_constraints.delete_rule AS "on_delete" + FROM information_schema.key_column_usage + INNER JOIN information_schema.constraint_table_usage + ON ( + information_schema.key_column_usage.constraint_name = information_schema.constraint_table_usage.constraint_name + AND information_schema.key_column_usage.constraint_schema = information_schema.constraint_table_usage.constraint_schema + AND information_schema.key_column_usage.constraint_catalog = information_schema.constraint_table_usage.constraint_catalog + ) + INNER JOIN information_schema.constraint_column_usage + ON ( + information_schema.key_column_usage.constraint_name = information_schema.constraint_column_usage.constraint_name + AND information_schema.key_column_usage.constraint_schema = information_schema.constraint_column_usage.constraint_schema + AND information_schema.key_column_usage.constraint_catalog = information_schema.constraint_column_usage.constraint_catalog + ) + INNER JOIN information_schema.referential_constraints + ON ( + information_schema.key_column_usage.constraint_name = information_schema.referential_constraints.constraint_name + AND information_schema.key_column_usage.constraint_schema = information_schema.referential_constraints.constraint_schema + AND information_schema.key_column_usage.constraint_catalog = information_schema.referential_constraints.constraint_catalog + ) + WHERE + information_schema.key_column_usage.table_catalog = current_database() + AND information_schema.key_column_usage.table_schema = ANY( current_schemas( FALSE ) ) + AND information_schema.key_column_usage.table_name = ? + ', array( $type ) ); + $keyInfoList = array(); + foreach ( $keys as $k ) { + $label = $this->makeFKLabel( $k['from'], $k['table'], $k['to'] ); + $keyInfoList[$label] = array( + 'name' => $k['name'], + 'from' => $k['from'], + 'table' => $k['table'], + 'to' => $k['to'], + 'on_update' => $k['on_update'], + 'on_delete' => $k['on_delete'] + ); + } + return $keyInfoList; + } + + /** + * Constructor + * Most of the time, you do not need to use this constructor, + * since the facade takes care of constructing and wiring the + * RedBeanPHP core objects. However if you would like to + * assemble an OODB instance yourself, this is how it works: + * + * Usage: + * + * + * $database = new RPDO( $dsn, $user, $pass ); + * $adapter = new DBAdapter( $database ); + * $writer = new PostgresWriter( $adapter ); + * $oodb = new OODB( $writer, FALSE ); + * $bean = $oodb->dispense( 'bean' ); + * $bean->name = 'coffeeBean'; + * $id = $oodb->store( $bean ); + * $bean = $oodb->load( 'bean', $id ); + * + * + * The example above creates the 3 RedBeanPHP core objects: + * the Adapter, the Query Writer and the OODB instance and + * wires them together. The example also demonstrates some of + * the methods that can be used with OODB, as you see, they + * closely resemble their facade counterparts. + * + * The wiring process: create an RPDO instance using your database + * connection parameters. Create a database adapter from the RPDO + * object and pass that to the constructor of the writer. Next, + * create an OODB instance from the writer. Now you have an OODB + * object. + * + * @param Adapter $adapter Database Adapter + */ + public function __construct( Adapter $adapter ) + { + $this->typeno_sqltype = array( + self::C_DATATYPE_INTEGER => ' integer ', + self::C_DATATYPE_DOUBLE => ' double precision ', + self::C_DATATYPE_TEXT => ' text ', + self::C_DATATYPE_SPECIAL_DATE => ' date ', + self::C_DATATYPE_SPECIAL_TIME => ' time ', + self::C_DATATYPE_SPECIAL_TIMEZ => ' time with time zone ', + self::C_DATATYPE_SPECIAL_DATETIME => ' timestamp without time zone ', + self::C_DATATYPE_SPECIAL_POINT => ' point ', + self::C_DATATYPE_SPECIAL_LSEG => ' lseg ', + self::C_DATATYPE_SPECIAL_CIRCLE => ' circle ', + self::C_DATATYPE_SPECIAL_MONEY => ' money ', + self::C_DATATYPE_SPECIAL_MONEY2 => ' numeric(10,2) ', + self::C_DATATYPE_SPECIAL_POLYGON => ' polygon ', + self::C_DATATYPE_SPECIAL_JSON => ' json ', + ); + + $this->sqltype_typeno = array(); + + foreach ( $this->typeno_sqltype as $k => $v ) { + $this->sqltype_typeno[trim( strtolower( $v ) )] = $k; + } + + $this->adapter = $adapter; + } + + /** + * This method returns the datatype to be used for primary key IDS and + * foreign keys. Returns one if the data type constants. + * + * @return integer + */ + public function getTypeForID() + { + return self::C_DATATYPE_INTEGER; + } + + /** + * @see QueryWriter::getTables + */ + public function getTables() + { + return $this->adapter->getCol( 'SELECT table_name FROM information_schema.tables WHERE table_schema = ANY( current_schemas( FALSE ) )' ); + } + + /** + * @see QueryWriter::createTable + */ + public function createTable( $type ) + { + $table = $this->esc( $type ); + + $this->adapter->exec( sprintf( $this->getDDLTemplate( 'createTable', $type ), $table ) ); + } + + /** + * @see QueryWriter::getColumns + */ + public function getColumns( $table ) + { + $table = $this->esc( $table, TRUE ); + + $columnsRaw = $this->adapter->get( "SELECT column_name, data_type FROM information_schema.columns WHERE table_name='$table' AND table_schema = ANY( current_schemas( FALSE ) )" ); + + $columns = array(); + foreach ( $columnsRaw as $r ) { + $columns[$r['column_name']] = $r['data_type']; + } + + return $columns; + } + + /** + * @see QueryWriter::scanType + */ + public function scanType( $value, $flagSpecial = FALSE ) + { + $this->svalue = $value; + + if ( $value === INF ) return self::C_DATATYPE_TEXT; + + if ( $flagSpecial && $value ) { + if ( preg_match( '/^\d{4}\-\d\d-\d\d$/', $value ) ) { + return PostgreSQL::C_DATATYPE_SPECIAL_DATE; + } + + if ( preg_match( '/^\d{4}\-\d\d-\d\d\s\d\d:\d\d:\d\d(\.\d{1,6})?$/', $value ) ) { + return PostgreSQL::C_DATATYPE_SPECIAL_DATETIME; + } + + if ( preg_match( '/^\([\d\.]+,[\d\.]+\)$/', $value ) ) { + return PostgreSQL::C_DATATYPE_SPECIAL_POINT; + } + + if ( preg_match( '/^\[\([\d\.]+,[\d\.]+\),\([\d\.]+,[\d\.]+\)\]$/', $value ) ) { + return PostgreSQL::C_DATATYPE_SPECIAL_LSEG; + } + + if ( preg_match( '/^\<\([\d\.]+,[\d\.]+\),[\d\.]+\>$/', $value ) ) { + return PostgreSQL::C_DATATYPE_SPECIAL_CIRCLE; + } + + if ( preg_match( '/^\((\([\d\.]+,[\d\.]+\),?)+\)$/', $value ) ) { + return PostgreSQL::C_DATATYPE_SPECIAL_POLYGON; + } + + if ( preg_match( '/^\-?(\$|€|¥|£)[\d,\.]+$/', $value ) ) { + return PostgreSQL::C_DATATYPE_SPECIAL_MONEY; + } + + if ( preg_match( '/^-?\d+\.\d{2}$/', $value ) ) { + return PostgreSQL::C_DATATYPE_SPECIAL_MONEY2; + } + if ( self::$flagUseJSONColumns && $this->isJSON( $value ) ) { + return self::C_DATATYPE_SPECIAL_JSON; + } + } + + if ( is_float( $value ) ) return self::C_DATATYPE_DOUBLE; + + if ( $this->startsWithZeros( $value ) ) return self::C_DATATYPE_TEXT; + + if ( $value === FALSE || $value === TRUE || $value === NULL || ( is_numeric( $value ) + && AQueryWriter::canBeTreatedAsInt( $value ) + && $value < 2147483648 + && $value > -2147483648 ) + ) { + return self::C_DATATYPE_INTEGER; + } elseif ( is_numeric( $value ) ) { + return self::C_DATATYPE_DOUBLE; + } else { + return self::C_DATATYPE_TEXT; + } + } + + /** + * @see QueryWriter::code + */ + public function code( $typedescription, $includeSpecials = FALSE ) + { + $r = ( isset( $this->sqltype_typeno[$typedescription] ) ) ? $this->sqltype_typeno[$typedescription] : 99; + + if ( $includeSpecials ) return $r; + + if ( $r >= QueryWriter::C_DATATYPE_RANGE_SPECIAL ) { + return self::C_DATATYPE_SPECIFIED; + } + + return $r; + } + + /** + * @see QueryWriter::widenColumn + */ + public function widenColumn( $beanType, $column, $datatype ) + { + $table = $beanType; + $type = $datatype; + + $table = $this->esc( $table ); + $column = $this->esc( $column ); + + $newtype = $this->typeno_sqltype[$type]; + + $this->adapter->exec( sprintf( $this->getDDLTemplate( 'widenColumn', $beanType, $column ), $table, $column, $newtype ) ); + + } + + /** + * @see QueryWriter::addUniqueIndex + */ + public function addUniqueConstraint( $type, $properties ) + { + $tableNoQ = $this->esc( $type, TRUE ); + $columns = array(); + foreach( $properties as $key => $column ) $columns[$key] = $this->esc( $column ); + $table = $this->esc( $type ); + sort( $columns ); //else we get multiple indexes due to order-effects + $name = "UQ_" . sha1( $table . implode( ',', $columns ) ); + $sql = "ALTER TABLE {$table} + ADD CONSTRAINT $name UNIQUE (" . implode( ',', $columns ) . ")"; + try { + $this->adapter->exec( $sql ); + } catch( SQLException $e ) { + return FALSE; + } + return TRUE; + } + + /** + * @see QueryWriter::sqlStateIn + */ + public function sqlStateIn( $state, $list, $extraDriverDetails = array() ) + { + $stateMap = array( + '42P01' => QueryWriter::C_SQLSTATE_NO_SUCH_TABLE, + '42703' => QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, + '23505' => QueryWriter::C_SQLSTATE_INTEGRITY_CONSTRAINT_VIOLATION, + '55P03' => QueryWriter::C_SQLSTATE_LOCK_TIMEOUT + ); + return in_array( ( isset( $stateMap[$state] ) ? $stateMap[$state] : '0' ), $list ); + } + + /** + * @see QueryWriter::addIndex + */ + public function addIndex( $type, $name, $property ) + { + $table = $this->esc( $type ); + $name = preg_replace( '/\W/', '', $name ); + $column = $this->esc( $property ); + + try { + $this->adapter->exec( "CREATE INDEX {$name} ON $table ({$column}) " ); + return TRUE; + } catch ( SQLException $e ) { + return FALSE; + } + } + + /** + * @see QueryWriter::addFK + */ + public function addFK( $type, $targetType, $property, $targetProperty, $isDep = FALSE ) + { + $table = $this->esc( $type ); + $targetTable = $this->esc( $targetType ); + $field = $this->esc( $property ); + $targetField = $this->esc( $targetProperty ); + $tableNoQ = $this->esc( $type, TRUE ); + $fieldNoQ = $this->esc( $property, TRUE ); + if ( !is_null( $this->getForeignKeyForTypeProperty( $tableNoQ, $fieldNoQ ) ) ) return FALSE; + try{ + $delRule = ( $isDep ? 'CASCADE' : 'SET NULL' ); + $this->adapter->exec( "ALTER TABLE {$table} + ADD FOREIGN KEY ( {$field} ) REFERENCES {$targetTable} + ({$targetField}) ON DELETE {$delRule} ON UPDATE {$delRule} DEFERRABLE ;" ); + return TRUE; + } catch ( SQLException $e ) { + return FALSE; + } + } + + /** + * @see QueryWriter::wipeAll + */ + public function wipeAll() + { + if (AQueryWriter::$noNuke) throw new \Exception('The nuke() command has been disabled using noNuke() or R::feature(novice/...).'); + $this->adapter->exec( 'SET CONSTRAINTS ALL DEFERRED' ); + + foreach ( $this->getTables() as $t ) { + $t = $this->esc( $t ); + //Some plugins (PostGIS have unremovable tables/views), avoid exceptions. + try { $this->adapter->exec( "DROP TABLE IF EXISTS $t CASCADE " ); }catch( \Exception $e ) {} + } + + $this->adapter->exec( 'SET CONSTRAINTS ALL IMMEDIATE' ); + } +} +} + namespace RedBeanPHP\QueryWriter { use RedBeanPHP\QueryWriter\AQueryWriter as AQueryWriter; use RedBeanPHP\QueryWriter as QueryWriter; @@ -7876,6 +8893,8 @@ class SQL extends RedException /** * @param array $driverDetails + * + * @return void */ public function setDriverDetails($driverDetails) { @@ -7932,7 +8951,7 @@ use RedBeanPHP\Cursor\NullCursor as NullCursor; * Abstract Repository. * * OODB manages two repositories, a fluid one that - * adjust the database schema on-the-fly to accomodate for + * adjust the database schema on-the-fly to accommodate for * new bean types (tables) and new properties (columns) and * a frozen one for use in a production environment. OODB * allows you to swap the repository instances using the freeze() @@ -7954,18 +8973,18 @@ abstract class Repository */ protected $stash = NULL; - /* + /** * @var integer */ protected $nesting = 0; /** - * @var DBAdapter + * @var QueryWriter */ protected $writer; /** - * @var boolean + * @var boolean|array */ protected $partialBeans = FALSE; @@ -7981,11 +9000,10 @@ abstract class Repository * * @param boolean|array $yesNoBeans List of type names or 'all' * - * @return mixed + * @return boolean|array */ public function usePartialBeans( $yesNoBeans ) { - $yesNoBeans = false; $oldValue = $this->partialBeans; $this->partialBeans = $yesNoBeans; return $oldValue; @@ -8075,7 +9093,7 @@ abstract class Repository * A bean may contain lists. This * method handles shared addition lists; i.e. * the $bean->sharedObject properties. - * Shared beans will be associated with eachother using the + * Shared beans will be associated with each other using the * Association Manager. * * @param OODBBean $bean the bean @@ -8311,7 +9329,7 @@ abstract class Repository * @param int $number number of beans you would like to get * @param boolean $alwaysReturnArray if TRUE always returns the result as an array * - * @return OODBBean + * @return OODBBean|OODBBean[] */ public function dispense( $type, $number = 1, $alwaysReturnArray = FALSE ) { @@ -8491,7 +9509,7 @@ abstract class Repository * New in 4.3.2: meta mask. The meta mask is a special mask to send * data from raw result rows to the meta store of the bean. This is * useful for bundling additional information with custom queries. - * Values of every column whos name starts with $mask will be + * Values of every column who's name starts with $mask will be * transferred to the meta section of the bean under key 'data.bundle'. * * @param string $type type of beans you would like to have @@ -8590,7 +9608,7 @@ abstract class Repository * * @param OODBBean|SimpleModel $bean bean you want to remove from database * - * @return void + * @return int */ public function trash( $bean ) { @@ -8608,7 +9626,7 @@ abstract class Repository } } try { - $deleted = $this->writer->deleteRecord( $bean->getMeta( 'type' ), array( 'id' => array( $bean->id ) ), NULL ); + $deleted = $this->writer->deleteRecord( $bean->getMeta( 'type' ), array( 'id' => array( $bean->id ) ) ); } catch ( SQLException $exception ) { $this->handleException( $exception ); } @@ -8672,7 +9690,7 @@ use RedBeanPHP\Repository as Repository; /** * Fluid Repository. * OODB manages two repositories, a fluid one that - * adjust the database schema on-the-fly to accomodate for + * adjust the database schema on-the-fly to accommodate for * new bean types (tables) and new properties (columns) and * a frozen one for use in a production environment. OODB * allows you to swap the repository instances using the freeze() @@ -8747,9 +9765,10 @@ class Fluid extends Repository * * This method will also apply indexes, unique constraints and foreign keys. * - * @param OODBBean $bean bean to get cast data from and store meta in - * @param string $property property to store - * @param mixed $value value to store + * @param OODBBean $bean bean to get cast data from and store meta in + * @param string $property property to store + * @param mixed $value value to store + * @param array|NULL &$columns * * @return void */ @@ -8995,7 +10014,7 @@ use RedBeanPHP\Repository as Repository; /** * Frozen Repository. * OODB manages two repositories, a fluid one that - * adjust the database schema on-the-fly to accomodate for + * adjust the database schema on-the-fly to accommodate for * new bean types (tables) and new properties (columns) and * a frozen one for use in a production environment. OODB * allows you to swap the repository instances using the freeze() @@ -9209,7 +10228,7 @@ class OODB extends Observable protected $nesting = 0; /** - * @var DBAdapter + * @var QueryWriter */ protected $writer; @@ -9219,12 +10238,12 @@ class OODB extends Observable protected $isFrozen = FALSE; /** - * @var FacadeBeanHelper + * @var BeanHelper|NULL */ protected $beanhelper = NULL; /** - * @var AssociationManager + * @var AssociationManager|NULL */ protected $assocManager = NULL; @@ -9234,12 +10253,12 @@ class OODB extends Observable protected $repository = NULL; /** - * @var FrozenRepo + * @var FrozenRepo|NULL */ protected $frozenRepository = NULL; /** - * @var FluidRepo + * @var FluidRepo|NULL */ protected $fluidRepository = NULL; @@ -9265,7 +10284,7 @@ class OODB extends Observable * Unboxes a bean from a FUSE model if needed and checks whether the bean is * an instance of OODBBean. * - * @param OODBBean $bean bean you wish to unbox + * @param OODBBean|SimpleModel $bean bean you wish to unbox * * @return OODBBean */ @@ -9327,7 +10346,7 @@ class OODB extends Observable /** * Toggles fluid or frozen mode. In fluid mode the database - * structure is adjusted to accomodate your objects. In frozen mode + * structure is adjusted to accommodate your objects. In frozen mode * this is not the case. * * You can also pass an array containing a selection of frozen types. @@ -9371,7 +10390,7 @@ class OODB extends Observable /** * Returns the current mode of operation of RedBean. * In fluid mode the database - * structure is adjusted to accomodate your objects. + * structure is adjusted to accommodate your objects. * In frozen mode * this is not the case. * @@ -9411,7 +10430,7 @@ class OODB extends Observable * @param string $number number of beans you would like to get * @param boolean $alwaysReturnArray if TRUE always returns the result as an array * - * @return OODBBean + * @return OODBBean|OODBBean[] */ public function dispense( $type, $number = 1, $alwaysReturnArray = FALSE ) { @@ -9440,7 +10459,7 @@ class OODB extends Observable * Returns the current bean helper. * Bean helpers assist beans in getting a reference to a toolbox. * - * @return BeanHelper + * @return BeanHelper|NULL */ public function getBeanHelper() { @@ -9462,7 +10481,7 @@ class OODB extends Observable } /** - * Searches the database for a bean that matches conditions $conditions and sql $addSQL + * Searches the database for a bean that matches conditions $conditions and sql $sql * and returns an array containing all the beans that have been found. * * Conditions need to take form: @@ -9480,10 +10499,10 @@ class OODB extends Observable * Note that you can use property names; the columns will be extracted using the * appropriate bean formatter. * - * @param string $type type of beans you are looking for - * @param array $conditions list of conditions - * @param string $sql SQL to be used in query - * @param array $bindings a list of values to bind to query parameters + * @param string $type type of beans you are looking for + * @param array $conditions list of conditions + * @param string|NULL $sql SQL to be used in query + * @param array $bindings a list of values to bind to query parameters * * @return array */ @@ -9495,9 +10514,9 @@ class OODB extends Observable /** * Same as find() but returns a BeanCollection. * - * @param string $type type of beans you are looking for - * @param string $sql SQL to be used in query - * @param array $bindings a list of values to bind to query parameters + * @param string $type type of beans you are looking for + * @param string|NULL $sql SQL to be used in query + * @param array $bindings a list of values to bind to query parameters * * @return BeanCollection */ @@ -9585,7 +10604,7 @@ class OODB extends Observable * * @param OODBBean|SimpleModel $bean bean you want to remove from database * - * @return void + * @return int */ public function trash( $bean ) { @@ -9716,10 +10735,10 @@ class OODB extends Observable * MySQL spatial columns, because they need to be processed first using * the asText/GeomFromText functions. * - * @param string $mode mode to set function for, i.e. read or write - * @param string $field field (table.column) to bind SQL function to - * @param string $function SQL function to bind to field - * @param boolean $isTemplate TRUE if $function is an SQL string, FALSE for just a function name + * @param string $mode mode to set function for, i.e. read or write + * @param string $field field (table.column) to bind SQL function to + * @param string|NULL $function SQL function to bind to field + * @param boolean $isTemplate TRUE if $function is an SQL string, FALSE for just a function name * * @return void */ @@ -10104,9 +11123,9 @@ class Finder * array parameter; you can either use the question mark notation * or the slot-notation (:keyname). * - * @param string $type type the type of bean you are looking for - * @param string $sql sql SQL query to find the desired bean, starting right after WHERE clause - * @param array $bindings values array of values to be bound to parameters in query + * @param string $type type the type of bean you are looking for + * @param string|NULL $sql sql SQL query to find the desired bean, starting right after WHERE clause + * @param array $bindings values array of values to be bound to parameters in query * * @return array */ @@ -10130,9 +11149,9 @@ class Finder * * @see Finder::find * - * @param string $type type the type of bean you are looking for - * @param string $sql sql SQL query to find the desired bean, starting right after WHERE clause - * @param array $bindings values array of values to be bound to parameters in query + * @param string $type type the type of bean you are looking for + * @param string|NULL $sql sql SQL query to find the desired bean, starting right after WHERE clause + * @param array $bindings values array of values to be bound to parameters in query * * @return array */ @@ -10153,9 +11172,9 @@ class Finder * * @see Finder::find * - * @param string $type type the type of bean you are looking for - * @param string $sql sql SQL query to find the desired bean, starting right after WHERE clause - * @param array $bindings values array of values to be bound to parameters in query + * @param string $type type the type of bean you are looking for + * @param string|NULL $sql sql SQL query to find the desired bean, starting right after WHERE clause + * @param array $bindings values array of values to be bound to parameters in query * * @return OODBBean|NULL */ @@ -10179,9 +11198,9 @@ class Finder * * @see Finder::find * - * @param string $type the type of bean you are looking for - * @param string $sql SQL query to find the desired bean, starting right after WHERE clause - * @param array $bindings values array of values to be bound to parameters in query + * @param string $type the type of bean you are looking for + * @param string|NULL $sql SQL query to find the desired bean, starting right after WHERE clause + * @param array $bindings values array of values to be bound to parameters in query * * @return OODBBean|NULL */ @@ -10203,9 +11222,9 @@ class Finder * * @see Finder::find * - * @param string $type the type of bean you are looking for - * @param string $sql SQL query to find the desired bean, starting right after WHERE clause - * @param array $bindings values array of values to be bound to parameters in query + * @param string $type the type of bean you are looking for + * @param string|NULL $sql SQL query to find the desired bean, starting right after WHERE clause + * @param array $bindings values array of values to be bound to parameters in query * * @return array */ @@ -10473,7 +11492,7 @@ class Finder } else { list($field1, $field2) = $matcher; foreach( $beans[$b] as $key => $bean ) { - $beans[$b][$key]->{$field1} = $beans[$a][$bean->{$field2}]; + $beans[$b][$key]->{$field1} = (isset($beans[$a][$bean->{$field2}]) ? $beans[$a][$bean->{$field2}] : NULL); } } } @@ -10700,7 +11719,7 @@ class AssociationManager extends Observable * * @param OODBBean|array $bean a bean object or an array of beans * @param string $type type of bean you're interested in - * @param string $sql SQL snippet (optional) + * @param string|NULL $sql SQL snippet (optional) * @param array $bindings bindings for your SQL string * * @return integer @@ -10819,7 +11838,7 @@ class AssociationManager extends Observable * Since 3.2, you can now also pass an array of beans instead just one * bean as the first parameter. * - * @param OODBBean|array $bean the bean you have + * @param OODBBean $bean the bean you have * @param string $type the type of beans you want * @param string $sql SQL snippet for extra filtering * @param array $bindings values to be inserted in SQL slots @@ -10857,7 +11876,7 @@ use RedBeanPHP\OODBBean as OODBBean; * Interface for Bean Helper. * A little bolt that glues the whole machinery together. * The Bean Helper is passed to the OODB RedBeanPHP Object to - * faciliatte the creation of beans and providing them with + * facilitate the creation of beans and providing them with * a toolbox. The Helper also facilitates the FUSE feature, * determining how beans relate to their models. By overriding * the getModelForBean method you can tune the FUSEing to @@ -10934,7 +11953,7 @@ class SimpleFacadeBeanHelper implements BeanHelper /** * Factory function to create instance of Simple Model, if any. * - * @var \Closure + * @var callable|NULL */ private static $factory = null; @@ -10956,7 +11975,7 @@ class SimpleFacadeBeanHelper implements BeanHelper * Sets the factory function to create the model when using FUSE * to connect a bean to a model. * - * @param \Closure $factory factory function + * @param callable|NULL $factory factory function * * @return void */ @@ -10981,6 +12000,28 @@ class SimpleFacadeBeanHelper implements BeanHelper $model = $bean->getMeta( 'type' ); $prefix = defined( 'REDBEAN_MODEL_PREFIX' ) ? REDBEAN_MODEL_PREFIX : '\\Model_'; + return $this->resolveModel($prefix, $model, $bean); + } + + /** + * Resolves the model associated with the bean using the model name (type), + * the prefix and the bean. + * + * @note + * If REDBEAN_CLASS_AUTOLOAD is defined this will be passed to class_exist as + * autoloading flag. + * + * @param string $prefix Prefix to use for resolution + * @param string $model Type name + * @param OODBBean $bean Bean to resolve model for + * + * @return SimpleModel|CustomModel|NULL + */ + protected function resolveModel($prefix, $model, $bean) { + + /* Determine autoloading preference */ + $autoloadFlag = ( defined( 'REDBEAN_CLASS_AUTOLOAD' ) ? REDBEAN_CLASS_AUTOLOAD : TRUE ); + if ( strpos( $model, '_' ) !== FALSE ) { $modelParts = explode( '_', $model ); $modelName = ''; @@ -10990,13 +12031,13 @@ class SimpleFacadeBeanHelper implements BeanHelper $modelName = $prefix . $modelName; if ( !class_exists( $modelName ) ) { $modelName = $prefix . ucfirst( $model ); - if ( !class_exists( $modelName ) ) { + if ( !class_exists( $modelName, $autoloadFlag ) ) { return NULL; } } } else { $modelName = $prefix . ucfirst( $model ); - if ( !class_exists( $modelName ) ) { + if ( !class_exists( $modelName, $autoloadFlag ) ) { return NULL; } } @@ -11015,6 +12056,63 @@ class SimpleFacadeBeanHelper implements BeanHelper } } +namespace RedBeanPHP\BeanHelper { + +use RedBeanPHP\BeanHelper as BeanHelper; +use RedBeanPHP\Facade as Facade; +use RedBeanPHP\OODBBean as OODBBean; +use RedBeanPHP\SimpleModelHelper as SimpleModelHelper; +use RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper as SimpleFacadeBeanHelper; + +/** + * Dynamic Bean Helper. + * + * The dynamic bean helper allows you to use differently namespaced + * classes for models per database connection. + * + * @file RedBeanPHP/BeanHelper/DynamicBeanHelper.php + * @author Gabor de Mooij and the RedBeanPHP Community + * @license BSD/GPLv2 + * + * @copyright + * (c) copyright G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community + * This source file is subject to the BSD/GPLv2 License that is bundled + * with this source code in the file license.txt. + */ +class DynamicBeanHelper extends SimpleFacadeBeanHelper implements BeanHelper +{ + /** + * Model prefix to be used for the current database connection. + * + * @var string + */ + private $modelPrefix; + + /** + * Constructor + * + * Usage: + * + * + * R::addDatabase( ..., new DynamicBeanHelper('Prefix1_') ); + * + * + * @param string $modelPrefix prefix + */ + public function __construct( $modelPrefix ) { + $this->modelPrefix = $modelPrefix; + } + + /** + * @see BeanHelper::getModelForBean + */ + public function getModelForBean( OODBBean $bean ) + { + return $this->resolveModel( $this->modelPrefix, $bean->getMeta( 'type' ), $bean ); + } +} +} + namespace RedBeanPHP { use RedBeanPHP\OODBBean as OODBBean; @@ -11172,7 +12270,7 @@ class SimpleModelHelper implements Observer * invoke the corresponding method on the bean. * * @param string $eventName i.e. 'delete', 'after_delete' - * @param OODBean $bean affected bean + * @param OODBBean $bean affected bean * * @return void */ @@ -11250,7 +12348,7 @@ class TagManager protected $associationManager; /** - * @var OODBBean + * @var OODB */ protected $redbean; @@ -11259,7 +12357,7 @@ class TagManager * it will split the string into words and return an array instead. * In case of an array the argument will be returned 'as is'. * - * @param array|string $tagList list of tags + * @param array|string|false $tagList list of tags * * @return array */ @@ -11275,12 +12373,12 @@ class TagManager } /** - * Finds a tag bean by it's title. + * Finds a tag bean by its title. * Internal method. * * @param string $title title to search for * - * @return OODBBean + * @return OODBBean|NULL */ protected function findTagByTitle( $title ) { @@ -11311,7 +12409,7 @@ class TagManager } /** - * Tests whether a bean has been associated with one ore more + * Tests whether a bean has been associated with one or more * of the listed tags. If the third parameter is TRUE this method * will return TRUE only if all tags that have been specified are indeed * associated with the given bean, otherwise FALSE. @@ -11369,8 +12467,8 @@ class TagManager * In the example above, the $blog bean will no longer * be associated with the tags 'smart' and 'interesting'. * - * @param OODBBean $bean tagged bean - * @param array $tagList list of tags (names) + * @param OODBBean $bean tagged bean + * @param array|string $tagList list of tags (names) * * @return void */ @@ -11446,8 +12544,8 @@ class TagManager * The example adds the tag 'halloween' to the $blog * bean. * - * @param OODBBean $bean bean to tag - * @param array $tagList list of tags to add to bean + * @param OODBBean $bean bean to tag + * @param array|string|false $tagList list of tags to add to bean * * @return void */ @@ -11832,7 +12930,7 @@ class Facade private static $writer; /** - * @var DBAdapter + * @var Adapter */ private static $adapter; @@ -11872,7 +12970,7 @@ class Facade private static $logger; /** - * @var array + * @var callable[] */ private static $plugins = array(); @@ -11882,12 +12980,12 @@ class Facade private static $exportCaseStyle = 'default'; /** - * @var flag allows transactions through facade in fluid mode + * @var bool flag allows transactions through facade in fluid mode */ private static $allowFluidTransactions = FALSE; /** - * @var flag allows to unfreeze if needed with store(all) + * @var bool flag allows to unfreeze if needed with store(all) */ private static $allowHybridMode = FALSE; @@ -11902,7 +13000,7 @@ class Facade public static $currentDB = ''; /** - * @var array + * @var ToolBox[] */ public static $toolboxes = array(); @@ -11914,7 +13012,7 @@ class Facade * @param string $sql the sql you want to execute * @param array $bindings array of values to be bound to query statement * - * @return array + * @return array|int|Cursor|NULL */ private static function query( $method, $sql, $bindings ) { @@ -11949,6 +13047,8 @@ class Facade * flag. * * @param boolean $hybrid + * + * @return bool */ public static function setAllowHybridMode( $hybrid ) { @@ -12026,11 +13126,11 @@ class Facade * a connection is really required, for instance when attempting to load * a bean. * - * @param string $dsn Database connection string - * @param string $username Username for database - * @param string $password Password for database - * @param boolean $frozen TRUE if you want to setup in frozen mode - * @param boolean $partialBeans TRUE to enable partial bean updates + * @param string|\PDO|NULL $dsn Database connection string + * @param string|NULL $username Username for database + * @param string|NULL $password Password for database + * @param boolean|string[] $frozen TRUE if you want to setup in frozen mode + * @param boolean|string[] $partialBeans TRUE to enable partial bean updates * @param array $options Additional (PDO) options to pass * * @return ToolBox @@ -12081,7 +13181,7 @@ class Facade * Toggles fluid transactions. By default fluid transactions * are not active. Starting, committing or rolling back a transaction * through the facade in fluid mode will have no effect. If you wish - * to replace this standard portable behavor with behavior depending + * to replace this standard portable behavior with behavior depending * on how the used database platform handles fluid (DDL) transactions * set this flag to TRUE. * @@ -12161,28 +13261,35 @@ class Facade * This method allows you to dynamically add (and select) new databases * to the facade. Adding a database with the same key will cause an exception. * - * @param string $key ID for the database - * @param string $dsn DSN for the database - * @param string $user user for connection - * @param NULL|string $pass password for connection - * @param bool $frozen whether this database is frozen or not + * @param string $key ID for the database + * @param string|\PDO $dsn DSN for the database + * @param string|NULL $user user for connection + * @param string|NULL $pass password for connection + * @param bool|string[] $frozen whether this database is frozen or not + * @param bool|string[] $partialBeans should we load partial beans? + * @param array $options additional options for the query writer + * @param BeanHelper|NULL $beanHelper Beanhelper to use (use this for DB specific model prefixes) * * @return void */ - public static function addDatabase( $key, $dsn, $user = NULL, $pass = NULL, $frozen = FALSE, $partialBeans = FALSE, $options = array() ) + public static function addDatabase( $key, $dsn, $user = NULL, $pass = NULL, $frozen = FALSE, $partialBeans = FALSE, $options = array(), $beanHelper = NULL ) { if ( isset( self::$toolboxes[$key] ) ) { throw new RedException( 'A database has already been specified for this key.' ); } self::$toolboxes[$key] = self::createToolbox($dsn, $user, $pass, $frozen, $partialBeans, $options); + + if ( !is_null( $beanHelper ) ) { + self::$toolboxes[$key]->getRedBean()->setBeanHelper( $beanHelper ); + } } /** * Creates a toolbox. This method can be called if you want to use redbean non-static. - * It has the same interface as R::setup(). The createToolbx() method can be called - * without any arguments, in this case it will try to create a SQLite database in - * /tmp called red.db (this only works on UNIX-like systems). + * It has the same interface as R::setup(). The createToolbox() method can be called + * without any arguments, in this case it will try to create a SQLite database in + * /tmp called red.db (this only works on UNIX-like systems). * * Usage: * @@ -12204,15 +13311,17 @@ class Facade * Instead, it will prepare the connection and connect 'lazily', i.e. the moment * a connection is really required, for instance when attempting to load a bean. * - * @param string $dsn Database connection string + * @param string|\PDO $dsn Database connection string * @param string $username Username for database * @param string $password Password for database - * @param boolean $frozen TRUE if you want to setup in frozen mode + * @param boolean|string[] $frozen TRUE if you want to setup in frozen mode + * @param boolean|string[] $partialBeans TRUE to enable partial bean updates + * @param array $options * * @return ToolBox */ - public static function createToolbox( $dsn = NULL, $username = NULL, $password = NULL, $frozen = FALSE, $partialBeans = FALSE, $options = array() ) - { + public static function createToolbox( $dsn, $username = NULL, $password = NULL, $frozen = FALSE, $partialBeans = FALSE, $options = array() ) + { if ( is_object($dsn) ) { $db = new RPDO( $dsn ); $dbType = $db->getDatabaseType(); @@ -12237,6 +13346,7 @@ class Facade throw new RedException( 'Unsupported database ('.$wkey.').' ); } $writerClass = '\\RedBeanPHP\\QueryWriter\\'.$writers[$wkey]; + /** @var AQueryWriter $writer */ $writer = new $writerClass( $adapter ); $redbean = new OODB( $writer, $frozen ); @@ -12272,6 +13382,7 @@ class Facade * switched and FALSE otherwise (for instance if you already using the specified database). * * @param string $key Key of the database to select + * @param bool $force * * @return boolean */ @@ -12301,7 +13412,7 @@ class Facade * There are 2 debug styles: * * Classic: separate parameter bindings, explicit and complete but less readable - * Fancy: interpersed bindings, truncates large strings, highlighted schema changes + * Fancy: interspersed bindings, truncates large strings, highlighted schema changes * * Fancy style is more readable but sometimes incomplete. * @@ -12363,9 +13474,9 @@ class Facade * the column types used to store their values. * If no type is passed, this method returns a list of all tables in the database. * - * @param string $type Type of bean (i.e. table) you want to inspect + * @param string|NULL $type Type of bean (i.e. table) you want to inspect, or NULL for a list of all tables * - * @return array + * @return string[] */ public static function inspect( $type = NULL ) { @@ -12429,14 +13540,16 @@ class Facade /** * Toggles fluid or frozen mode. In fluid mode the database - * structure is adjusted to accomodate your objects. In frozen mode + * structure is adjusted to accommodate your objects. In frozen mode * this is not the case. * * You can also pass an array containing a selection of frozen types. * Let's call this chilly mode, it's just like fluid mode except that * certain types (i.e. tables) aren't touched. * - * @param boolean|array $tf mode of operation (TRUE means frozen) + * @param boolean|string[] $tf mode of operation (TRUE means frozen) + * + * @return void */ public static function freeze( $tf = TRUE ) { @@ -12457,10 +13570,10 @@ class Facade * list( $author, $bio ) = R::loadMulti( 'author, bio', $id ); * * - * @param string|array $types the set of types to load at once - * @param mixed $id the common ID + * @param string|string[] $types the set of types to load at once + * @param int $id the common ID * - * @return OODBBean + * @return OODBBean[] */ public static function loadMulti( $types, $id ) { @@ -12502,7 +13615,7 @@ class Facade * * @param string $type type of bean you want to load * @param integer $id ID of the bean you want to load - * @param string $snippet string to use after select (optional) + * @param string|NULL $snippet string to use after select (optional) * * @return OODBBean */ @@ -12556,10 +13669,10 @@ class Facade * * * @param string $type the type of bean you are looking for - * @param string $sql SQL query to find the desired bean, starting right after WHERE clause + * @param string|NULL $sql SQL query to find the desired bean, starting right after WHERE clause * @param array $bindings array of values to be bound to parameters in query * - * @return array + * @return OODBBean[] */ public static function findForUpdate( $type, $sql = NULL, $bindings = array() ) { @@ -12571,16 +13684,16 @@ class Facade * Same as findForUpdate but returns just one bean and adds LIMIT-clause. * * @param string $type the type of bean you are looking for - * @param string $sql SQL query to find the desired bean, starting right after WHERE clause + * @param string|NULL $sql SQL query to find the desired bean, starting right after WHERE clause * @param array $bindings array of values to be bound to parameters in query * - * @return array + * @return OODBBean|NULL */ public static function findOneForUpdate( $type, $sql = NULL, $bindings = array() ) { $sql = self::getWriter()->glueLimitOne( $sql ); $beans = self::findForUpdate($type, $sql, $bindings); - return !empty($beans) ? reset($beans) : NULL; + return empty($beans) ? NULL : reset($beans); } /** @@ -12611,7 +13724,7 @@ class Facade * @param string|OODBBean|SimpleModel $beanOrType bean you want to remove from database * @param integer $id ID if the bean to trash (optional, type-id variant only) * - * @return void + * @return int */ public static function trash( $beanOrType, $id = NULL ) { @@ -12659,11 +13772,11 @@ class Facade * ] ); * * - * @param string|array $typeOrBeanArray type or bean array to import + * @param string|OODBBean[] $typeOrBeanArray type or bean array to import * @param integer $num number of beans to dispense * @param boolean $alwaysReturnArray if TRUE always returns the result as an array * - * @return array|OODBBean + * @return OODBBean|OODBBean[] */ public static function dispense( $typeOrBeanArray, $num = 1, $alwaysReturnArray = FALSE ) { @@ -12704,15 +13817,15 @@ class Facade } /** - * Convience method. Tries to find beans of a certain type, + * Convenience method. Tries to find beans of a certain type, * if no beans are found, it dispenses a bean of that type. * Note that this function always returns an array. * - * @param string $type type of bean you are looking for - * @param string $sql SQL code for finding the bean - * @param array $bindings parameters to bind to SQL + * @param string $type type of bean you are looking for + * @param string|NULL $sql SQL code for finding the bean + * @param array $bindings parameters to bind to SQL * - * @return array + * @return OODBBean[] */ public static function findOrDispense( $type, $sql = NULL, $bindings = array() ) { @@ -12723,9 +13836,9 @@ class Facade /** * Same as findOrDispense but returns just one element. * - * @param string $type type of bean you are looking for - * @param string $sql SQL code for finding the bean - * @param array $bindings parameters to bind to SQL + * @param string $type type of bean you are looking for + * @param string|NULL $sql SQL code for finding the bean + * @param array $bindings parameters to bind to SQL * * @return OODBBean */ @@ -12745,12 +13858,15 @@ class Facade * * Your SQL does not have to start with a WHERE-clause condition. * - * @param string $type the type of bean you are looking for - * @param string $sql SQL query to find the desired bean, starting right after WHERE clause - * @param array $bindings array of values to be bound to parameters in query - * @param string $snippet SQL snippet to include in query (for example: FOR UPDATE) + * @param string $type the type of bean you are looking for + * @param string|NULL $sql SQL query to find the desired bean, starting right after WHERE clause + * @param array $bindings array of values to be bound to parameters in query + * @param string|NULL $snippet SQL snippet to include in query (for example: FOR UPDATE) * - * @return array + * @phpstan-param literal-string|null $sql + * @psalm-param literal-string|null $sql + * + * @return OODBBean[] */ public static function find( $type, $sql = NULL, $bindings = array(), $snippet = NULL ) { @@ -12761,11 +13877,11 @@ class Facade /** * Alias for find(). * - * @param string $type the type of bean you are looking for - * @param string $sql SQL query to find the desired bean, starting right after WHERE clause - * @param array $bindings array of values to be bound to parameters in query + * @param string $type the type of bean you are looking for + * @param string|NULL $sql SQL query to find the desired bean, starting right after WHERE clause + * @param array $bindings array of values to be bound to parameters in query * - * @return array + * @return OODBBean[] */ public static function findAll( $type, $sql = NULL, $bindings = array() ) { @@ -12781,9 +13897,9 @@ class Facade * * @see Finder::find * - * @param string $type type the type of bean you are looking for - * @param string $sql sql SQL query to find the desired bean, starting right after WHERE clause - * @param array $bindings values array of values to be bound to parameters in query + * @param string $type type the type of bean you are looking for + * @param string|NULL $sql sql SQL query to find the desired bean, starting right after WHERE clause + * @param array $bindings values array of values to be bound to parameters in query * * @return array */ @@ -12795,9 +13911,9 @@ class Facade /** * Like R::find() but returns the first bean only. * - * @param string $type the type of bean you are looking for - * @param string $sql SQL query to find the desired bean, starting right after WHERE clause - * @param array $bindings array of values to be bound to parameters in query + * @param string $type the type of bean you are looking for + * @param string|NULL $sql SQL query to find the desired bean, starting right after WHERE clause + * @param array $bindings array of values to be bound to parameters in query * * @return OODBBean|NULL */ @@ -12820,9 +13936,9 @@ class Facade * * @see Finder::find * - * @param string $type the type of bean you are looking for - * @param string $sql SQL query to find the desired bean, starting right after WHERE clause - * @param array $bindings values array of values to be bound to parameters in query + * @param string $type the type of bean you are looking for + * @param string|NULL $sql SQL query to find the desired bean, starting right after WHERE clause + * @param array $bindings values array of values to be bound to parameters in query * * @return OODBBean|NULL */ @@ -12837,9 +13953,9 @@ class Facade * cursors - this is useful for processing large datasets. A bean collection * will not load all beans into memory all at once, just one at a time. * - * @param string $type the type of bean you are looking for - * @param string $sql SQL query to find the desired bean, starting right after WHERE clause - * @param array $bindings values array of values to be bound to parameters in query + * @param string $type the type of bean you are looking for + * @param string|NULL $sql SQL query to find the desired bean, starting right after WHERE clause + * @param array $bindings values array of values to be bound to parameters in query * * @return BeanCollection */ @@ -12909,10 +14025,10 @@ class Facade * * @note instead of an SQL query you can pass a result array as well. * - * @param string|array $types a list of types (either array or comma separated string) - * @param string|array $sql an SQL query or an array of prefetched records - * @param array $bindings optional, bindings for SQL query - * @param array $remappings optional, an array of remapping arrays + * @param string|string[] $types a list of types (either array or comma separated string) + * @param string|array[]|NULL $sql an SQL query or an array of prefetched records + * @param array $bindings optional, bindings for SQL query + * @param array[] $remappings optional, an array of remapping arrays * * @return array */ @@ -12931,9 +14047,9 @@ class Facade * passed IDs as their keys. * * @param string $type type of beans - * @param array $ids ids to load + * @param int[] $ids ids to load * - * @return array + * @return OODBBean[] */ public static function batch( $type, $ids ) { @@ -12947,9 +14063,9 @@ class Facade * loadAll. * * @param string $type type of beans - * @param array $ids ids to load + * @param int[] $ids ids to load * - * @return array + * @return OODBBean[] */ public static function loadAll( $type, $ids ) { @@ -12981,7 +14097,7 @@ class Facade * @param string $sql SQL query to execute * @param array $bindings a list of values to be bound to query parameters * - * @return array + * @return string[][] */ public static function getAll( $sql, $bindings = array() ) { @@ -12998,7 +14114,7 @@ class Facade * @param string $sql SQL query to execute * @param array $bindings a list of values to be bound to query parameters * - * @return string + * @return string|NULL */ public static function getCell( $sql, $bindings = array() ) { @@ -13015,7 +14131,7 @@ class Facade * @param string $sql SQL query to execute * @param array $bindings a list of values to be bound to query parameters * - * @return RedBeanPHP\Cursor\PDOCursor + * @return Cursor */ public static function getCursor( $sql, $bindings = array() ) { @@ -13032,7 +14148,7 @@ class Facade * @param string $sql SQL query to execute * @param array $bindings a list of values to be bound to query parameters * - * @return array + * @return array|NULL */ public static function getRow( $sql, $bindings = array() ) { @@ -13049,7 +14165,7 @@ class Facade * @param string $sql SQL query to execute * @param array $bindings a list of values to be bound to query parameters * - * @return array + * @return string[] */ public static function getCol( $sql, $bindings = array() ) { @@ -13068,7 +14184,7 @@ class Facade * @param string $sql SQL query to execute * @param array $bindings a list of values to be bound to query parameters * - * @return array + * @return string[] */ public static function getAssoc( $sql, $bindings = array() ) { @@ -13076,7 +14192,7 @@ class Facade } /** - *Convenience function to fire an SQL query using the RedBeanPHP + * Convenience function to fire an SQL query using the RedBeanPHP * database adapter. This method allows you to directly query the * database without having to obtain an database adapter instance first. * Executes the specified SQL query together with the specified @@ -13098,7 +14214,7 @@ class Facade * Returns the insert ID for databases that support/require this * functionality. Alias for R::getAdapter()->getInsertID(). * - * @return mixed + * @return int */ public static function getInsertID() { @@ -13124,12 +14240,12 @@ class Facade * This function has a confusing method signature, the R::duplicate() function * only accepts two arguments: bean and filters. * - * @param OODBBean $bean bean to be copied - * @param array $trail for internal usage, pass array() - * @param boolean $pid for internal usage - * @param array $filters white list filter with bean types to duplicate + * @param OODBBean $bean bean to be copied + * @param OODBBean[] $trail for internal usage, pass array() + * @param boolean $pid for internal usage + * @param array $filters white list filter with bean types to duplicate * - * @return array + * @return OODBBean */ public static function dup( $bean, $trail = array(), $pid = FALSE, $filters = array() ) { @@ -13156,10 +14272,10 @@ class Facade * Note: * This is a simplified version of the deprecated R::dup() function. * - * @param OODBBean $bean bean to be copied - * @param array $white white list filter with bean types to duplicate + * @param OODBBean $bean bean to be copied + * @param array $filters white list filter with bean types to duplicate * - * @return array + * @return OODBBean */ public static function duplicate( $bean, $filters = array() ) { @@ -13175,12 +14291,12 @@ class Facade * * all own bean lists (recursively) * * all shared beans (not THEIR own lists) * - * @param array|OODBBean $beans beans to be exported - * @param boolean $parents whether you want parent beans to be exported - * @param array $filters whitelist of types - * @param boolean $meta export meta data as well + * @param OODBBean|OODBBean[] $beans beans to be exported + * @param boolean $parents whether you want parent beans to be exported + * @param array $filters whitelist of types + * @param boolean $meta export meta data as well * - * @return array + * @return array[] */ public static function exportAll( $beans, $parents = FALSE, $filters = array(), $meta = FALSE ) { @@ -13240,14 +14356,14 @@ class Facade * New in 4.3.2: meta mask. The meta mask is a special mask to send * data from raw result rows to the meta store of the bean. This is * useful for bundling additional information with custom queries. - * Values of every column whos name starts with $mask will be + * Values of every column who's name starts with $mask will be * transferred to the meta section of the bean under key 'data.bundle'. * - * @param string $type type of beans to produce - * @param array $rows must contain an array of array - * @param string $metamask meta mask to apply (optional) + * @param string $type type of beans to produce + * @param string[][] $rows must contain an array of array + * @param string|array|NULL $metamask meta mask to apply (optional) * - * @return array + * @return OODBBean[] */ public static function convertToBeans( $type, $rows, $metamask = NULL ) { @@ -13255,11 +14371,11 @@ class Facade } /** - * Just like converToBeans, but for one bean. + * Just like convertToBeans, but for one bean. * - * @param string $type type of bean to produce - * @param array $row one row from the database - * @param string $metamask metamask (see convertToBeans) + * @param string $type type of bean to produce + * @param string[] $row one row from the database + * @param string|array|NULL $metamask metamask (see convertToBeans) * * @return OODBBean|NULL */ @@ -13309,7 +14425,7 @@ class Facade * @param string $type Type of bean to produce * @param string $sql SQL query snippet to use * @param array $bindings bindings for query (optional) - * @param mixed $metamask meta mask (optional, defaults to 'extra_') + * @param string|array $metamask meta mask (optional, defaults to 'extra_') * @param boolean $autoExtract TRUE to return meta mask values as first item of array * * @return array @@ -13353,9 +14469,9 @@ class Facade * or 'horror' this operation will return FALSE because the third parameter * has been set to TRUE. * - * @param OODBBean $bean bean to check for tags - * @param array|string $tags list of tags - * @param boolean $all whether they must all match or just some + * @param OODBBean $bean bean to check for tags + * @param string|string[] $tags list of tags + * @param boolean $all whether they must all match or just some * * @return boolean */ @@ -13380,8 +14496,8 @@ class Facade * In the example above, the $blog bean will no longer * be associated with the tags 'smart' and 'interesting'. * - * @param OODBBean $bean tagged bean - * @param array $tagList list of tags (names) + * @param OODBBean $bean tagged bean + * @param string|string[] $tagList list of tags (names) * * @return void */ @@ -13409,10 +14525,10 @@ class Facade * as 'TexMex' and 'Mexican Cuisine'. The second line will * retrieve all tags attached to the meal object. * - * @param OODBBean $bean bean to tag - * @param mixed $tagList tags to attach to the specified bean + * @param OODBBean $bean bean to tag + * @param string[]|NULL $tagList tags to attach to the specified bean * - * @return string + * @return string[] */ public static function tag( OODBBean $bean, $tagList = NULL ) { @@ -13434,8 +14550,8 @@ class Facade * The example adds the tag 'halloween' to the $blog * bean. * - * @param OODBBean $bean bean to tag - * @param array $tagList list of tags to add to bean + * @param OODBBean $bean bean to tag + * @param string|string[] $tagList list of tags to add to bean * * @return void */ @@ -13466,12 +14582,12 @@ class Facade * tagged as 'horror' or 'gothic', order them by title and limit * the number of movies to be returned to 10. * - * @param string $beanType type of bean you are looking for - * @param array|string $tagList list of tags to match - * @param string $sql additional SQL (use only for pagination) - * @param array $bindings bindings + * @param string $beanType type of bean you are looking for + * @param string|string[] $tagList list of tags to match + * @param string $sql additional SQL (use only for pagination) + * @param array $bindings bindings * - * @return array + * @return OODBBean[] */ public static function tagged( $beanType, $tagList, $sql = '', $bindings = array() ) { @@ -13500,12 +14616,12 @@ class Facade * The example above returns at most 4 movies (due to the LIMIT clause in the SQL * Query Snippet) that have been tagged as BOTH 'short' AND 'gothic'. * - * @param string $beanType type of bean you are looking for - * @param array|string $tagList list of tags to match - * @param string $sql additional sql snippet - * @param array $bindings bindings + * @param string $beanType type of bean you are looking for + * @param string|string[] $tagList list of tags to match + * @param string $sql additional sql snippet + * @param array $bindings bindings * - * @return array + * @return OODBBean[] */ public static function taggedAll( $beanType, $tagList, $sql = '', $bindings = array() ) { @@ -13517,10 +14633,10 @@ class Facade * * @see R::taggedAll * - * @param string $beanType type of bean you are looking for - * @param array|string $tagList list of tags to match - * @param string $sql additional sql snippet - * @param array $bindings bindings + * @param string $beanType type of bean you are looking for + * @param string|string[] $tagList list of tags to match + * @param string $sql additional sql snippet + * @param array $bindings bindings * * @return integer */ @@ -13534,10 +14650,10 @@ class Facade * * @see R::tagged * - * @param string $beanType type of bean you are looking for - * @param array|string $tagList list of tags to match - * @param string $sql additional sql snippet - * @param array $bindings bindings + * @param string $beanType type of bean you are looking for + * @param string|string[] $tagList list of tags to match + * @param string $sql additional sql snippet + * @param array $bindings bindings * * @return integer */ @@ -13597,7 +14713,7 @@ class Facade self::$labelMaker = new LabelMaker( self::$toolbox ); $helper = new SimpleModelHelper(); $helper->attachEventListeners( self::$redbean ); - if (self::$redbean->getBeanHelper() == NULL) { + if (self::$redbean->getBeanHelper() === NULL) { self::$redbean->setBeanHelper( new SimpleFacadeBeanHelper ); } self::$duplicationManager = new DuplicationManager( self::$toolbox ); @@ -13606,7 +14722,7 @@ class Facade } /** - * Facade Convience method for adapter transaction system. + * Facade Convenience method for adapter transaction system. * Begins a transaction. * * Usage: @@ -13647,7 +14763,7 @@ class Facade } /** - * Facade Convience method for adapter transaction system. + * Facade Convenience method for adapter transaction system. * Commits a transaction. * * Usage: @@ -13688,7 +14804,7 @@ class Facade } /** - * Facade Convience method for adapter transaction system. + * Facade Convenience method for adapter transaction system. * Rolls back a transaction. * * Usage: @@ -13736,7 +14852,7 @@ class Facade * * @param string $table name of the table (not type) you want to get columns of * - * @return array + * @return string[] */ public static function getColumns( $table ) { @@ -13768,7 +14884,7 @@ class Facade * ' IN( ?,? ) '. * * @param array $array array to generate question mark slots for - * @param string $template template to use + * @param string|NULL $template template to use * * @return string */ @@ -13802,11 +14918,11 @@ class Facade * ); * * - * @param array $beans a list of OODBBeans - * @param string $type a type string - * @param string $sqlTemplate an SQL template string for the SELECT-query + * @param OODBBean[] $beans a list of OODBBeans + * @param string $type a type string + * @param string $sqlTemplate an SQL template string for the SELECT-query * - * @return array + * @return OODBBean[] */ public static function loadJoined( $beans, $type, $sqlTemplate = 'SELECT %s.* FROM %s WHERE id IN (%s)' ) { @@ -13888,10 +15004,10 @@ class Facade * will automatically temporarily switch to fluid mode to attempt to store the * bean in case of an SQLException. * - * @param array $beans list of beans to be stored - * @param boolean $unfreezeIfNeeded retries in fluid mode in hybrid mode + * @param OODBBean[] $beans list of beans to be stored + * @param boolean $unfreezeIfNeeded retries in fluid mode in hybrid mode * - * @return array + * @return int[] ids */ public static function storeAll( $beans, $unfreezeIfNeeded = FALSE ) { @@ -13907,9 +15023,9 @@ class Facade * For information please consult the R::trash() function. * A loop saver. * - * @param array $beans list of beans to be trashed + * @param OODBBean[] $beans list of beans to be trashed * - * @return void + * @return int */ public static function trashAll( $beans ) { @@ -13925,13 +15041,13 @@ class Facade * only IDs. This function combines trashAll and batch loading * in one call. Note that while this function accepts just * bean IDs, the beans will still be loaded first. This is because - * the function still respects all the FUSE hooks that may have beeb + * the function still respects all the FUSE hooks that may have been * associated with the domain logic associated with these beans. * If you really want to delete just records from the database use * a simple DELETE-FROM SQL query instead. * - * @param string type $type the bean type you wish to trash - * @param string array $ids list of bean IDs + * @param string $type the bean type you wish to trash + * @param int[] $ids list of bean IDs * * @return void */ @@ -13957,9 +15073,9 @@ class Facade * * Returns the number of beans deleted. * - * @param string $type bean type to look for in database - * @param string $sqlSnippet an SQL query snippet - * @param array $bindings SQL parameter bindings + * @param string $type bean type to look for in database + * @param string|NULL $sqlSnippet an SQL query snippet + * @param array $bindings SQL parameter bindings * * @return int */ @@ -14000,10 +15116,10 @@ class Facade * values of the array will be assigned to the name property of each * individual bean. * - * @param string $type type of beans you would like to have - * @param array $labels list of labels, names for each bean + * @param string $type type of beans you would like to have + * @param string[] $labels list of labels, names for each bean * - * @return array + * @return OODBBean[] */ public static function dispenseLabels( $type, $labels ) { @@ -14012,7 +15128,7 @@ class Facade /** * Generates and returns an ENUM value. This is how RedBeanPHP handles ENUMs. - * Either returns a (newly created) bean respresenting the desired ENUM + * Either returns a (newly created) bean representing the desired ENUM * value or returns a list of all enums for the type. * * To obtain (and add if necessary) an ENUM value: @@ -14039,7 +15155,7 @@ class Facade * * @param string $enum either type or type-value * - * @return array|OODBBean + * @return OODBBean|OODBBean[] */ public static function enum( $enum ) { @@ -14052,9 +15168,9 @@ class Facade * and stores the names in a new array. The array then gets sorted using the * default sort function of PHP (sort). * - * @param array $beans list of beans to loop + * @param OODBBean[] $beans list of beans to loop * - * @return array + * @return string[] */ public static function gatherLabels( $beans ) { @@ -14088,7 +15204,7 @@ class Facade * Simple convenience function, returns ISO date formatted representation * of $time. * - * @param mixed $time UNIX timestamp + * @param int|NULL $time UNIX timestamp * * @return string */ @@ -14106,7 +15222,7 @@ class Facade * formatted representation * of $time. * - * @param mixed $time UNIX timestamp + * @param int|NULL $time UNIX timestamp * * @return string */ @@ -14162,7 +15278,7 @@ class Facade * Optional accessor for neat code. * Sets the database adapter you want to use. * - * @return DBAdapter + * @return Adapter */ public static function getDatabaseAdapter() { @@ -14181,7 +15297,7 @@ class Facade * If the PDO object could not be found, for whatever reason, this method * will return NULL instead. * - * @return NULL|PDO + * @return NULL|\PDO */ public static function getPDO() { @@ -14190,6 +15306,8 @@ class Facade $database = $databaseAdapter->getDatabase(); if ( is_null( $database ) ) return NULL; if ( !method_exists( $database, 'getPDO' ) ) return NULL; + + /** @var RPDO $database */ return $database->getPDO(); } @@ -14261,8 +15379,8 @@ class Facade /** * Facade method for AQueryWriter::renameAssociation() * - * @param string|array $from - * @param string $to + * @param string|string[] $from + * @param string $to * * @return void */ @@ -14278,9 +15396,9 @@ class Facade * and shared lists, the beans are exported as-is, only loaded lists * are exported. * - * @param array $beans beans + * @param OODBBean[] $beans beans * - * @return array + * @return array[] */ public static function beansToArray( $beans ) { @@ -14340,9 +15458,9 @@ class Facade * to 35 characters to improve readability. Nested beans will * also be dumped. * - * @param OODBBean|array $data either a bean or an array of beans + * @param OODBBean|OODBBean[] $data either a bean or an array of beans * - * @return array + * @return string|string[] */ public static function dump( $data ) { @@ -14384,7 +15502,7 @@ class Facade * fetchAs but explicitly. For instance if you register * the alias 'cover' for 'page' a property containing a reference to a * page bean called 'cover' will correctly return the page bean and not - * a (non-existant) cover bean. + * a (non-existent) cover bean. * * * R::aliases( array( 'cover' => 'page' ) ); @@ -14408,7 +15526,7 @@ class Facade * From that point on, every bean reference to a cover * will return a 'page' bean. * - * @param array $list list of global aliases to use + * @param string[] $list list of global aliases to use * * @return void */ @@ -14427,13 +15545,13 @@ class Facade * * @param string $type type of bean to search for * @param array $like criteria set describing the bean to search for - * @param boolean $hasBeenCreated set to TRUE if bean has been created + * @param boolean &$hasBeenCreated set to TRUE if bean has been created * * @return OODBBean */ public static function findOrCreate( $type, $like = array(), $sql = '', &$hasBeenCreated = false ) { - return self::$finder->findOrCreate( $type, $like, $sql = '', $hasBeenCreated ); + return self::$finder->findOrCreate( $type, $like, $sql, $hasBeenCreated ); } /** @@ -14448,7 +15566,7 @@ class Facade * @param string $sql optional additional SQL for sorting * @param array $bindings bindings * - * @return array + * @return OODBBean[] */ public static function findLike( $type, $like = array(), $sql = '', $bindings = array() ) { @@ -14491,7 +15609,7 @@ class Facade /** * Stops logging and flushes the logs, - * convient method to stop logging of queries. + * convenient method to stop logging of queries. * Use this method to stop logging SQL queries being * executed by the adapter. Logging queries will not * print them on the screen. Use R::getLogs() to @@ -14566,7 +15684,7 @@ class Facade * Therefore, only stop logging AFTER you have obtained the * query logs using R::getLogs() * - * @return array + * @return string[] */ public static function getLogs() { @@ -14617,7 +15735,7 @@ class Facade * Returns the current logger instance being used by the * database object. * - * @return Logger + * @return Logger|NULL */ public static function getLogger() { @@ -14639,13 +15757,12 @@ class Facade * for some types. * This method will return the previous value. * - * @param boolean|array $yesNoBeans List of type names or 'all' + * @param boolean|string[] $yesNoBeans List of type names or 'all' * - * @return mixed + * @return boolean|string[] */ public static function usePartialBeans( $yesNoBeans ) { - $yesNoBeans = false; return self::$redbean->getCurrentRepository()->usePartialBeans( $yesNoBeans ); } @@ -14714,7 +15831,7 @@ class Facade * @param array $onNotFoundDo task list to be considered on NOT finding the bean * @param OODBBean &$bean reference to obtain the found bean * - * @return mixed + * @return bool|NULL */ public static function matchUp( $type, $sql, $bindings = array(), $onFoundDo = NULL, $onNotFoundDo = NULL, &$bean = NULL ) { $matchUp = new MatchUp( self::$toolbox ); @@ -14759,7 +15876,7 @@ class Facade * 'strtoupper', * "\n" * ); - * + * * * The example above creates an HTML fragment like this: * @@ -14818,11 +15935,10 @@ class Facade * of property names that have to be skipped. By default this array is filled with * two strings: 'created' and 'modified'. * - * @param OODBBean|array $bean reference beans - * @param OODBBean|array $other beans to compare + * @param OODBBean|OODBBean[] $bean reference beans + * @param OODBBean|OODBBean[] $other beans to compare * @param array $filters names of properties of all beans to skip - * @param string $format the format of the key, defaults to '%s.%s.%s' - * @param string $type type/property of bean to use for key generation + * @param string $pattern the format of the key, defaults to '%s.%s.%s' * * @return array */ @@ -14874,7 +15990,6 @@ class Facade * Returns the toolbox associated with the specified key. * * @param string $key key to store toolbox instance under - * @param ToolBox $toolbox toolbox to register * * @return ToolBox|NULL */ @@ -14932,7 +16047,7 @@ class Facade /** * Given a bean and an optional SQL snippet, - * this method will return the bean together with all + * this method will return the bean together with all * child beans in a hierarchically structured * bean table. * @@ -14941,9 +16056,11 @@ class Facade * a warning mechanism in case your database does not support this * functionality. * - * @param OODBBean $bean bean to find children of - * @param string $sql optional SQL snippet - * @param array $bindings SQL snippet parameter bindings + * @param OODBBean $bean bean to find children of + * @param string|NULL $sql optional SQL snippet + * @param array $bindings SQL snippet parameter bindings + * + * @return OODBBean[] */ public static function children( OODBBean $bean, $sql = NULL, $bindings = array() ) { @@ -14974,9 +16091,11 @@ class Facade * it cannot 'predict' what or how you are trying to 'count'. * * @param OODBBean $bean bean to find children of - * @param string $sql optional SQL snippet + * @param string|NULL $sql optional SQL snippet * @param array $bindings SQL snippet parameter bindings * @param string|boolean $select select snippet to use (advanced, optional, see QueryWriter::queryRecursiveCommonTableExpression) + * + * @return int */ public static function countChildren( OODBBean $bean, $sql = NULL, $bindings = array(), $select = QueryWriter::C_CTE_SELECT_COUNT ) { @@ -15007,9 +16126,11 @@ class Facade * it cannot 'predict' what or how you are trying to 'count'. * * @param OODBBean $bean bean to find children of - * @param string $sql optional SQL snippet + * @param string|NULL $sql optional SQL snippet * @param array $bindings SQL snippet parameter bindings * @param string|boolean $select select snippet to use (advanced, optional, see QueryWriter::queryRecursiveCommonTableExpression) + * + * @return int */ public static function countParents( OODBBean $bean, $sql = NULL, $bindings = array(), $select = QueryWriter::C_CTE_SELECT_COUNT ) { @@ -15026,9 +16147,11 @@ class Facade * a warning mechanism in case your database does not support this * functionality. * - * @param OODBBean $bean bean to find parents of - * @param string $sql optional SQL snippet - * @param array $bindings SQL snippet parameter bindings + * @param OODBBean $bean bean to find parents of + * @param string|NULL $sql optional SQL snippet + * @param array $bindings SQL snippet parameter bindings + * + * @return OODBBean[] */ public static function parents( OODBBean $bean, $sql = NULL, $bindings = array() ) { @@ -15236,7 +16359,7 @@ class DuplicationManager protected $filters = array(); /** - * @var array + * @var boolean */ protected $cacheTables = FALSE; @@ -15513,6 +16636,8 @@ class DuplicationManager * every time a duplication action is performed (dup()). * * @param boolean $yesNo TRUE to use caching, FALSE otherwise + * + * @return void */ public function setCacheTables( $yesNo ) { @@ -15690,8 +16815,8 @@ class ArrayTool * The statement in the example will produce the string: * ' IN( ?,? ) '. * - * @param array $array array to generate question mark slots for - * @param string $template template to use + * @param array $array array to generate question mark slots for + * @param string|NULL $template template to use * * @return string */ @@ -15731,6 +16856,7 @@ class ArrayTool namespace RedBeanPHP\Util { use RedBeanPHP\OODB as OODB; +use RedBeanPHP\OODBBean as OODBBean; use RedBeanPHP\RedException as RedException; /** @@ -15850,7 +16976,7 @@ class DispenseHelper * @param integer $num number of beans to dispense * @param boolean $alwaysReturnArray if TRUE always returns the result as an array * - * @return array|OODBBean + * @return OODBBean|OODBBean[] */ public static function dispense( OODB $oodb, $typeOrBeanArray, $num = 1, $alwaysReturnArray = FALSE ) { @@ -16010,6 +17136,7 @@ class Dump namespace RedBeanPHP\Util { use RedBeanPHP\OODB as OODB; +use RedBeanPHP\OODBBean as OODBBean; /** * Multi Bean Loader Helper @@ -16020,7 +17147,7 @@ use RedBeanPHP\OODB as OODB; * * This helper class offers limited support for one-to-one * relations by providing a service to load a set of beans - * with differnt types and a common ID. + * with different types and a common ID. * * @file RedBeanPHP/Util/MultiLoader.php * @author Gabor de Mooij and the RedBeanPHP Community @@ -16051,7 +17178,7 @@ class MultiLoader * @param string|array $types the set of types to load at once * @param mixed $id the common ID * - * @return OODBBean + * @return OODBBean[] */ public static function load( OODB $oodb, $types, $id ) { @@ -16167,7 +17294,7 @@ use RedBeanPHP\ToolBox as ToolBox; * The Quick Export Utility Class provides functionality to easily * expose the result of SQL queries as well-known formats like CSV. * - * @file RedBeanPHP/Util/QuickExporft.php + * @file RedBeanPHP/Util/QuickExport.php * @author Gabor de Mooij and the RedBeanPHP Community * @license BSD/GPLv2 * @@ -16179,12 +17306,12 @@ use RedBeanPHP\ToolBox as ToolBox; class QuickExport { /** - * @var Finder + * @var ToolBox */ protected $toolbox; /** - * @boolean + * @var boolean */ private static $test = FALSE; @@ -16201,7 +17328,13 @@ class QuickExport /** * Makes csv() testable. - */ + * + * @param string $name + * @param mixed $arg1 + * @param boolean $arg2 + * + * @return mixed + */ public static function operation( $name, $arg1, $arg2 = TRUE ) { $out = ''; switch( $name ) { @@ -16250,7 +17383,7 @@ class QuickExport * @param boolean $output TRUE to output CSV directly using readfile * @param array $options delimiter, quote and escape character respectively * - * @return void + * @return string|NULL */ public function csv( $sql = '', $bindings = array(), $columns = NULL, $path = '/tmp/redexport_%s.csv', $output = TRUE, $options = array(',','"','\\') ) { @@ -16360,11 +17493,11 @@ class MatchUp * @param string $type type of bean you're looking for * @param string $sql SQL snippet (starting at the WHERE clause, omit WHERE-keyword) * @param array $bindings array of parameter bindings for SQL snippet - * @param array $onFoundDo task list to be considered on finding the bean - * @param array $onNotFoundDo task list to be considered on NOT finding the bean - * @param OODBBean &$bean reference to obtain the found bean + * @param array|NULL $onFoundDo task list to be considered on finding the bean + * @param array|NULL $onNotFoundDo task list to be considered on NOT finding the bean + * @param OODBBean|NULL &$bean reference to obtain the found bean * - * @return mixed + * @return bool|NULL */ public function matchUp( $type, $sql, $bindings = array(), $onFoundDo = NULL, $onNotFoundDo = NULL, &$bean = NULL ) { @@ -16576,7 +17709,7 @@ class Diff * @param OODBBean|array $others beans to compare * @param array $filters names of properties of all beans to skip * @param string $format the format of the key, defaults to '%s.%s.%s' - * @param string $type type/property of bean to use for key generation + * @param string|NULL $type type/property of bean to use for key generation * * @return array */ @@ -16627,13 +17760,15 @@ class Diff namespace RedBeanPHP\Util { use RedBeanPHP\ToolBox; +use RedBeanPHP\OODB; use RedBeanPHP\OODBBean; +use RedBeanPHP\QueryWriter; /** * Tree * * Given a bean, finds it children or parents - * in a hierchical structure. + * in a hierarchical structure. * * @experimental feature * @@ -16681,8 +17816,8 @@ class Tree { * bean in a tree structure. * * @note this only works for databases that support - * recusrive common table expressions. - * + * recursive common table expressions. + * * Usage: * * @@ -16698,9 +17833,9 @@ class Tree { * parameter binding key ':slot0' is reserved for the ID of the bean * and used in the query. * - * @param OODBBean $bean reference bean to find children of - * @param string $sql optional SQL snippet - * @param array $bindings optional parameter bindings for SQL snippet + * @param OODBBean $bean reference bean to find children of + * @param string|NULL $sql optional SQL snippet + * @param array $bindings optional parameter bindings for SQL snippet * * @return array */ @@ -16719,7 +17854,7 @@ class Tree { * bean in a tree structure. * * @note this only works for databases that support - * recusrive common table expressions. + * recursive common table expressions. * * * $newsPages = R::parents( $newsArticle, ' ORDER BY title ASC ' ); @@ -16734,9 +17869,9 @@ class Tree { * parameter binding key ':slot0' is reserved for the ID of the bean * and used in the query. * - * @param OODBBean $bean reference bean to find parents of - * @param string $sql optional SQL snippet - * @param array $bindings optional parameter bindings for SQL snippet + * @param OODBBean $bean reference bean to find parents of + * @param string|NULL $sql optional SQL snippet + * @param array $bindings optional parameter bindings for SQL snippet * * @return array */ @@ -16755,7 +17890,7 @@ class Tree { * bean in a tree structure. * * @note this only works for databases that support - * recusrive common table expressions. + * recursive common table expressions. * * * $count = R::countChildren( $newsArticle ); @@ -16777,7 +17912,7 @@ class Tree { * it cannot 'predict' what or how you are trying to 'count'. * * @param OODBBean $bean reference bean to find children of - * @param string $sql optional SQL snippet + * @param string|NULL $sql optional SQL snippet * @param array $bindings optional parameter bindings for SQL snippet * @param string|boolean $select select snippet to use (advanced, optional, see QueryWriter::queryRecursiveCommonTableExpression) * @@ -16797,7 +17932,7 @@ class Tree { * bean in a tree structure. * * @note this only works for databases that support - * recusrive common table expressions. + * recursive common table expressions. * * * $count = R::countParents( $newsArticle ); @@ -16818,9 +17953,9 @@ class Tree { * this method assumes you take control of the resulting total yourself since * it cannot 'predict' what or how you are trying to 'count'. * - * @param OODBBean $bean reference bean to find parents of - * @param string $sql optional SQL snippet - * @param array $bindings optional parameter bindings for SQL snippet + * @param OODBBean $bean reference bean to find parents of + * @param string|NULL $sql optional SQL snippet + * @param array $bindings optional parameter bindings for SQL snippet * @param string|boolean $select select snippet to use (advanced, optional, see QueryWriter::queryRecursiveCommonTableExpression) * * @return integer @@ -16944,6 +18079,158 @@ class Feature } } +namespace RedBeanPHP\Util { + +/** + * Either Utility + * + * The Either Utility class provides an easy way to + * substitute the NULL coalesce operator in RedBeanPHP + * (since the lazy loading interface interferes with the ?? + * operator) in a way that can also be used in older PHP-versions. + * + * @file RedBeanPHP/Util/Either.php + * @author Gabor de Mooij and the RedBeanPHP Community + * @license BSD/GPLv2 + * + * @copyright + * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community + * This source file is subject to the BSD/GPLv2 License that is bundled + * with this source code in the file license.txt. + */ +class Either { + + /** + * @var mixed + */ + private $result; + + /** + * Constructs a new Either-instance. + * + * Example usage: + * + * + * $author = $text + * ->either() + * ->page + * ->book + * ->autor + * ->name + * ->or('unknown'); + * + * + * The Either-class lets you access bean properties without having to do + * NULL-checks. The mechanism resembles the use of the ?? somewhat but + * offers backward compatibility with older PHP versions. The mechanism also works + * on arrays. + * + * + * $budget = $company + * ->either() + * ->sharedProject + * ->first() + * ->budget + * ->or(0); + * + */ + public function __construct($result) { + $this->result = $result; + } + + /** + * Extracts a value from the wrapped object and stores + * it in the internal result object. If the desired + * value cannot be found, the internal result object will be set + * to NULL. Chainable. + * + * @param string $something name of the property you wish to extract the value of + * + * @return self + */ + public function __get($something) { + if (is_object($this->result)) { + $this->result = $this->result->{$something}; + } else { + $this->result = NULL; + } + return $this; + } + + /** + * Extracts the first element of the array in the internal result + * object and stores it as the new value of the internal result object. + * Chainable. + * + * @return self + */ + public function first() { + if (is_array($this->result)) { + reset($this->result); + $key = key($this->result); + if (isset($this->result[$key])) { + $this->result = $this->result[$key]; + } else { + $this->result = NULL; + } + } + return $this; + } + + /** + * Extracts the last element of the array in the internal result + * object and stores it as the new value of the internal result object. + * Chainable. + * + * @return self + */ + public function last() { + if (is_array($this->result)) { + end($this->result); + $key = key($this->result); + if (isset($this->result[$key])) { + $this->result = $this->result[$key]; + } else { + $this->result = NULL; + } + } + return $this; + } + + /** + * Extracts the specified element of the array in the internal result + * object and stores it as the new value of the internal result object. + * Chainable. + * + * @return self + */ + public function index( $key = 0 ) { + if (is_array($this->result)) { + if (isset($this->result[$key])) { + $this->result = $this->result[$key]; + } else { + $this->result = NULL; + } + } + return $this; + } + + /** + * Resolves the Either-instance to a final value, either the value + * contained in the internal result object or the value specified + * in the or() function. + * + * @param mixed $value value to resolve to if internal result equals NULL + * + * @return mixed + */ + public function _or( $value ) { + $reference = (is_null($this->result)) ? $value : $this->result; + return $reference; + } +} +} + namespace RedBeanPHP { /** @@ -17094,4 +18381,24 @@ if ( !function_exists( 'pint' ) ) { } } -} \ No newline at end of file +/** + * Function DBPrefix() is a simple function to allow you to + * quickly set a different namespace for FUSE model resolution + * per database connection. It works by creating a new DynamicBeanHelper + * with the specified string as model prefix. + * + * Usage: + * + * + * R::addDatabase( ..., DBPrefix( 'Prefix1_' ) ); + * + */ +if ( !function_exists( 'DBPrefix' ) ) { + + function DBPrefix( $prefix = '\\Model' ) { + return new \RedBeanPHP\BeanHelper\DynamicBeanHelper( $prefix ); + } +} + + +} From 482e5e5219bed4537aa69dd8219c231cf60fbe8e Mon Sep 17 00:00:00 2001 From: Benjamin Aerni Date: Tue, 11 Oct 2022 12:54:23 -0700 Subject: [PATCH 2/2] Apply hack-fix to prevent failing tests --- src/rb.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rb.php b/src/rb.php index 8a6811e51..0e5d2d195 100644 --- a/src/rb.php +++ b/src/rb.php @@ -2820,7 +2820,8 @@ class OODBBean implements \IteratorAggregate,\ArrayAccess,\Countable,Jsonable $this->__info['tainted'] = TRUE; $this->__info['changed'] = TRUE; - array_push( $this->__info['changelist'], $property ); + /* Disabled for FOSSBilling hack-fix */ + //array_push( $this->__info['changelist'], $property ); if ( array_key_exists( $fieldLink, $this->properties ) && !( $value instanceof OODBBean ) ) { if ( is_null( $value ) || $value === FALSE ) {