Skip to content
This repository
tree: abd1e052ac
Fetching contributors…

Cannot retrieve contributors at this time

file 90 lines (81 sloc) 2.166 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
<?php
/**
* @package Morph
* @subpackage Property
* @author Jonathan Moss <xirisr@gmail.com>
* @copyright Jonathan Moss 2009
*/
namespace morph\property;

/**
* Class to represent a string property
*
* @package Morph
* @subpackage Property
*/
class String extends Generic
{
    /**
* The maximum string length permitted
* @var integer
*/
    protected $maximumLength;

    /**
* @param string $name
* @param string $default
* @param integer $maximumLength
*/
    public function __construct($name, $default = null, $maximumLength = null)
    {
        parent::__construct($name, $default);
        $this->maximumLength = (is_null($maximumLength)) ? null : (int)$maximumLength;
    }

    /**
* Sets the value of this attribute
*
* @param mixed $value
* @return void
*/
    public function setValue($value)
    {
        if (null === $value) {
            return parent::setValue(null);
        }

        $cleanValue = (string)$value;
        if(!empty($this->maximumLength)
            && ($this->_getUnicodeValue('strlen', $cleanValue) > $this->maximumLength) ){
            $cleanValue = $this->_getUnicodeValue(
                'substr', array($cleanValue, 0, $this->maximumLength)
            );
        }

        parent::setValue($cleanValue);
    }

    /**
* Returns this attributes value
*
* @return string
*/
    public function getValue()
    {
        return (null === parent::getValue()) ? null : (string)parent::getValue();
    }

    /**
* Gets unicode value according function name
*
* @param string $fncName
* @param mixed $value
* @return string
*/
    protected function _getUnicodeValue($fncName, $value)
    {
        // PHP 5.4 has native support of unicode chars
        if (version_compare(PHP_VERSION, '5.4', '>=')) {
            return call_user_func_array($fncName, (array)$value);
        }

        // iconv or mbstring
        $prefix = 'iconv';
        if (extension_loaded('mbstring')) {
            $prefix = 'mb';
        }

        return call_user_func_array($prefix . '_' . $fncName, (array)$value);
    }
}
Something went wrong with that request. Please try again.