Browse Source

adds IdentifierInterface

thinkingmedia 9 years ago
parent
commit
b7f91edb76

+ 2 - 2
src/Database/Expression/IdentifierExpression.php

@@ -14,13 +14,13 @@
  */
 namespace Cake\Database\Expression;
 
-use Cake\Database\ExpressionInterface;
+use Cake\Database\IdentifierInterface;
 use Cake\Database\ValueBinder;
 
 /**
  * Represents a single identifier name in the database
  */
-class IdentifierExpression implements ExpressionInterface
+class IdentifierExpression implements IdentifierInterface
 {
 
     /**

+ 36 - 0
src/Database/IdentifierInterface.php

@@ -0,0 +1,36 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.4.1
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Database;
+
+/**
+ * Represents a single identifier name in the database
+ */
+interface IdentifierInterface extends ExpressionInterface
+{
+    /**
+     * Returns the identifier this expression represents
+     *
+     * @return string
+     */
+    public function getIdentifier();
+
+    /**
+     * Sets the identifier this expression represents
+     *
+     * @param string $identifier The identifier
+     * @return void
+     */
+    public function setIdentifier($identifier);
+}

+ 1 - 1
src/Database/Query.php

@@ -1476,7 +1476,7 @@ class Query implements ExpressionInterface, IteratorAggregate
      * ```
      *
      * @param string $identifier The identifier for an expression
-     * @return ExpressionInterface
+     * @return \Cake\Database\IdentifierInterface
      */
     public function identifier($identifier)
     {

+ 28 - 4
tests/TestCase/Database/QueryTest.php

@@ -15,6 +15,8 @@
 namespace Cake\Test\TestCase\Database;
 
 use Cake\Database\Expression\IdentifierExpression;
+use Cake\Database\ExpressionInterface;
+use Cake\Database\IdentifierInterface;
 use Cake\Database\Query;
 use Cake\Database\StatementInterface;
 use Cake\Database\TypeMap;
@@ -3377,10 +3379,32 @@ class QueryTest extends TestCase
     public function testIdentifierExpression()
     {
         $query = new Query($this->connection);
-        /* @var \Cake\Database\Expression\IdentifierExpression $expression */
-        $expression = $query->identifier('foo');
-        $this->assertInstanceOf(IdentifierExpression::class, $expression);
-        $this->assertEquals('foo', $expression->getIdentifier());
+        /* @var \Cake\Database\Expression\IdentifierExpression $identifier */
+        $identifier = $query->identifier('foo');
+
+        $this->assertInstanceOf(IdentifierExpression::class, $identifier);
+        $this->assertEquals('foo', $identifier->getIdentifier());
+    }
+
+    /**
+     * Tests the interface contract of identifier
+     *
+     * @return void
+     */
+    public function testIdentifierInterface()
+    {
+        $query = new Query($this->connection);
+        /* @var \Cake\Database\IdentifierInterface $identifier */
+        $identifier = $query->identifier('description');
+
+        // should support these interfaces
+        $this->assertInstanceOf(IdentifierInterface::class, $identifier);
+        $this->assertInstanceOf(ExpressionInterface::class, $identifier);
+
+        $this->assertEquals('description', $identifier->getIdentifier());
+
+        $identifier->setIdentifier('title');
+        $this->assertEquals('title', $identifier->getIdentifier());
     }
 
     /**