ColumnSchema.php 2.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\sphinx;

use yii\base\Object;
use yii\db\Expression;

/**
 * ColumnSchema class describes the metadata of a column in a Sphinx index.
 *
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
class ColumnSchema extends Object
{
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    /**
     * @var string name of this column (without quotes).
     */
    public $name;
    /**
     * @var string abstract type of this column. Possible abstract types include:
     * string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
     * timestamp, time, date, binary, and money.
     */
    public $type;
    /**
     * @var string the PHP type of this column. Possible PHP types include:
     * string, boolean, integer, double.
     */
    public $phpType;
    /**
     * @var string the DB type of this column. Possible DB types vary according to the type of DBMS.
     */
    public $dbType;
    /**
     * @var boolean whether this column is a primary key
     */
    public $isPrimaryKey;
    /**
     * @var boolean whether this column is an attribute
     */
    public $isAttribute;
    /**
     * @var boolean whether this column is a indexed field
     */
    public $isField;
    /**
     * @var boolean whether this column is a multi value attribute (MVA)
     */
    public $isMva;
56

57
    /**
58
     * Converts the input value according to [[phpType]] after retrieval from the database.
59
     * If the value is null or an [[Expression]], it will not be converted.
60
     * @param mixed $value input value
61 62
     * @return mixed converted value
     */
63
    public function phpTypecast($value)
64 65 66 67 68 69 70 71 72
    {
        if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
            return $value;
        }
        if ($value === '' && $this->type !== Schema::TYPE_STRING) {
            return null;
        }
        switch ($this->phpType) {
            case 'string':
73
                return is_resource($value) ? $value : (string) $value;
74 75 76 77
            case 'integer':
                return (integer) $value;
            case 'boolean':
                return (boolean) $value;
78 79
            case 'double':
                return (double) $value;
80 81 82 83
        }

        return $value;
    }
84 85 86 87 88 89 90 91

    /**
     * Converts the input value according to [[type]] and [[dbType]] for use in a db query.
     * If the value is null or an [[Expression]], it will not be converted.
     * @param mixed $value input value
     * @return mixed converted value. This may also be an array containing the value as the first element
     * and the PDO type as the second element.
     */
92
    public function dbTypecast($value)
93 94 95
    {
        // the default implementation does the same as casting for PHP but it should be possible
        // to override this with annotation of explicit PDO type.
96
        return $this->phpTypecast($value);
97
    }
AlexGx committed
98
}