]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/criteria/dataset/class_DataSetCriteria.php
Continued:
[core.git] / framework / main / classes / criteria / dataset / class_DataSetCriteria.php
index f443623e870435c120263aa93c5feafc9e5d8e85..88e49dda27759a5b441dbe6592140d743bebd966 100644 (file)
@@ -4,15 +4,20 @@ namespace Org\Mxchange\CoreFramework\Criteria\DataSet;
 
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Criteria\BaseCriteria;
+use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
-use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
+
+// Import SPL stuff
+use \BadMethodCallException;
+use \InvalidArgumentException;
 
 /**
  * A set of data storeable in databases
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -48,14 +53,19 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
        /**
         * Primary keys
         */
-       private $primaryKeys = array();
+       private $primaryKeys = [];
+
+       /**
+        * Search criteria instance
+        */
+       private $searchInstance = NULL;
 
        /**
         * Protected constructor
         *
         * @return      void
         */
-       protected function __construct () {
+       private function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
        }
@@ -65,8 +75,16 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
         *
         * @param       $tableName                      Name of the table
         * @return      $criteriaInstance       An instance of this criteria
+        * @throws      InvalidArgumentException        If a parameter is not valid
         */
-       public static final function createDataSetCriteria ($tableName) {
+       public static final function createDataSetCriteria (string $tableName) {
+               // Validate parameter
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: tableName=%s - CALLED!', $tableName));
+               if (empty($tableName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "tableName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               }
+
                // Get a new instance
                $criteriaInstance = new DataSetCriteria();
 
@@ -74,6 +92,7 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
                $criteriaInstance->setTableName($tableName);
 
                // Return the instance
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: criteriaInstance=%s - EXIT!', $criteriaInstance->__toString()));
                return $criteriaInstance;
        }
 
@@ -83,8 +102,8 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
         * @param       $tableName      Name of the table to set
         * @return      void
         */
-       public final function setTableName ($tableName) {
-               $this->tableName = (string) $tableName;
+       public final function setTableName (string $tableName) {
+               $this->tableName = $tableName;
        }
 
        /**
@@ -102,8 +121,8 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
         * @param       $uniqueKey      Column to use as unique key
         * @return      void
         */
-       public final function setUniqueKey ($uniqueKey) {
-               $this->uniqueKey = (string) $uniqueKey;
+       public final function setUniqueKey (string $uniqueKey) {
+               $this->uniqueKey = $uniqueKey;
        }
 
        /**
@@ -115,23 +134,72 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
                return $this->uniqueKey;
        }
 
+       /**
+        * Setter for primary key
+        *
+        * @param       $primaryKey             Primary key to set
+        * @return      void
+        */
+       public final function setPrimaryKey (string $primaryKey) {
+               $this->primaryKey = $primaryKey;
+       }
+
+       /**
+        * Setter for primary key array
+        *
+        * @param       $primaryKeys    Primary key array to set
+        * @return      void
+        */
+       public function setPrimaryKeyCombined (array $primaryKeys) {
+               $this->primaryKeys = $primaryKeys;
+       }
+
+       /**
+        * Getter for primary keys
+        *
+        * @return      $primaryKeys    Primary key array
+        */
+       public final function getPrimaryKeys () {
+               // Return it
+               return $this->primaryKeys;
+       }
+
+       /**
+        * Setter for search instance
+        *
+        * @param       $searchInstance         Searchable criteria instance
+        * @return      void
+        */
+       public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
+               $this->searchInstance = $searchInstance;
+       }
+
+       /**
+        * Getter for search instance
+        *
+        * @return      $searchInstance         Searchable criteria instance
+        */
+       public final function getSearchInstance () {
+               return $this->searchInstance;
+       }
+
        /**
         * Getter for unique key value
         *
         * @return      $uniqueValue    Value of the unique key
+        * @throws      BadMethodCallException  If no primary and no unique key was found
         */
        public final function getUniqueValue () {
                // Get primary key(s) first
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DATA-SET-CRITERIA: CALLED!');
                $primaryKey  = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
                $primaryKeys = $this->getPrimaryKeys();
 
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
-
                /*
                 * If this is not set, this could mean a badly written frontend as
                 * tables should always have a primary key.
                 */
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: this->tableName=%s,primaryKey=%s,primaryKeys()=%d', $this->getTableName(), $primaryKey, count($primaryKeys)));
                if (count($primaryKeys) > 0) {
                        /*
                         * Init return value, this can be put all together without any
@@ -141,37 +209,38 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
                        $return = '';
 
                        // Combination set, so get all
-                       foreach ($primaryKeys as $primaryKey) {
+                       foreach ($primaryKeys as $primaryKeyPart) {
                                // Add it
-                               $return .= trim($this->getCriteriaElemnent($primaryKey));
-                       } // END - foreach
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: primaryKeyPart=%s', $primaryKeyPart));
+                               $return .= trim($this->getCriteriaElemnent($primaryKeyPart));
+                       }
 
                        // Debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
 
                        // Return it
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: return=%s - EXIT!', $return));
                        return $return;
                } elseif (!empty($primaryKey)) {
                        // Return primary key
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: primaryKey=%s - EXIT!', $primaryKey));
                        return $primaryKey;
                } else {
-                       // @TODO Issue a warning
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
+                       // Issue a warning
+                       self::createDebugInstance(__CLASS__, __LINE__)->warningMessage('DATA-SET-CRITERIA: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
 
                        // Get unique key
                        $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
 
-                       // Debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
-
                        // Is it empty, too?
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: uniqueKey=%s', $uniqueKey));
                        if (empty($uniqueKey)) {
                                // Bad news, nothing is "unique" by design for this table
-                               ApplicationEntryPoint::exitApplication('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
-                       } else {
-                               // Return unique key
-                               return $uniqueKey;
+                               throw new BadMethodCallException(sprintf('Table %s has both no primary and unique key, but %s was called. Please fix your table.', $this->getTableName(), __METHOD__), FrameworkInterface::EXCEPTION_BAD_METHOD_CALL);
                        }
+
+                       // Return unique key
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: uniqueKey=%s - EXIT!', $uniqueKey));
+                       return $uniqueKey;
                }
        }
 
@@ -182,45 +251,19 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
         */
        public final function getPrimaryKey () {
                // Get primary key by default
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DATA-SET-CRITERIA: CALLED!');
                $primaryKey = $this->primaryKey;
 
+               // Is it still empty?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DATA-SET-CRITERIA: primaryKey=%s', $primaryKey));
                if (empty($primaryKey)) {
                        // Get uniqueKey
                        $primaryKey = $this->getUniqueKey();
-               } // END - if
+               }
 
                // Return it
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATA-SET-CRITERIA: primaryKey=%s - EXIT!', $primaryKey));
                return $primaryKey;
        }
 
-       /**
-        * Setter for primary key
-        *
-        * @param       $primaryKey             Primary key to set
-        * @return      void
-        */
-       public final function setPrimaryKey ($primaryKey) {
-               $this->primaryKey = (string) $primaryKey;
-       }
-
-       /**
-        * Setter for primary key array
-        *
-        * @param       $primaryKeys    Primary key array to set
-        * @return      void
-        */
-       public function setPrimaryKeyCombined (array $primaryKeys) {
-               $this->primaryKeys = $primaryKeys;
-       }
-
-       /**
-        * Getter for primary keys
-        *
-        * @return      $primaryKeys    Primary key array
-        */
-       public final function getPrimaryKeys () {
-               // Return it
-               return $this->primaryKeys;
-       }
-
 }