Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #28 from kkamkou/collection-name-issue

unicode issue in the string property
  • Loading branch information...
commit 6a730ef4016b3eb6a3417eddf6ced7065fddf076 2 parents a5b33fe + 25a4a2e
Jonathan Moss authored
View
7 src/morph/property/Regex.php
@@ -6,6 +6,7 @@
* @copyright Jonathan Moss 2009
*/
namespace morph\property;
+
/**
* Class to represent an integer property
*
@@ -14,10 +15,10 @@
*/
class Regex extends Generic
{
-
-
/**
* @param string $name
+ *
+ * @return void
*/
public function __construct($name)
{
@@ -37,7 +38,7 @@ public function setValue($value)
/**
* Returns this attributes value
*
- * @return integer
+ * @return string
*/
public function getValue()
{
View
33 src/morph/property/String.php
@@ -6,6 +6,7 @@
* @copyright Jonathan Moss 2009
*/
namespace morph\property;
+
/**
* Class to represent a string property
*
@@ -14,7 +15,6 @@
*/
class String extends Generic
{
-
/**
* The maximum string length permitted
* @var integer
@@ -28,7 +28,7 @@ class String extends Generic
*/
public function __construct($name, $default = null, $maximumLength = null){
parent::__construct($name, $default);
- $this->maximumLength = (\is_null($maximumLength)) ? null : (int)$maximumLength;
+ $this->maximumLength = (is_null($maximumLength)) ? null : (int)$maximumLength;
}
/**
@@ -37,10 +37,16 @@ public function __construct($name, $default = null, $maximumLength = null){
* @param integer $Value
*/
public function setValue($value){
+ if (null === $value) {
+ return parent::setValue($value);
+ }
+
$cleanValue = (string)$value;
- if(!empty($this->maximumLength) && (strlen($cleanValue) > $this->maximumLength) ){
- //throw validation exception
- $cleanValue = substr($cleanValue, 0, $this->maximumLength);
+ if(!empty($this->maximumLength)
+ && ($this->_getUnicodeValue('strlen', $cleanValue) > $this->maximumLength) ){
+ $cleanValue = $this->_getUnicodeValue(
+ 'substr', array($cleanValue, 0, $this->maximumLength)
+ );
}
parent::setValue($cleanValue);
}
@@ -51,7 +57,22 @@ public function setValue($value){
* @return string
*/
public function getValue(){
- return (string)parent::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)
+ {
+ $prefix = 'iconv';
+ if (extension_loaded('mbstring')) {
+ $prefix = 'mb';
+ }
+ return call_user_func_array($prefix . '_' . $fncName, (array)$value);
+ }
}
View
45 unit-tests/property/TestProperties.php
@@ -40,13 +40,26 @@ public function testValue()
public function testStringProperty()
{
- $validString = 'ABCDEFGHIJKLMNOP';
- $invalidString = 'ABCDEFGHIJKLMNOPQR';
- $property = new String('Name', null, 16);
- $property->setValue($validString);
- $this->assertEquals($validString, $property->getValue());
- $property->setValue($invalidString);
- $this->assertEquals(16, strlen($property->getValue()));
+ $string = 'ABCDEFGHIJKLMNOPмамамылаРАМУЫДВЭР';
+ $stringLength = iconv_strlen($string);
+
+ $property = new String('Name', null, $stringLength);
+
+ $property->setValue($string);
+ $this->assertEquals($string, $property->getValue());
+
+ $property->setValue($string);
+ $this->assertEquals($stringLength, iconv_strlen($property->getValue()));
+
+ $property->setValue($string . $string);
+ $this->assertEquals($stringLength, iconv_strlen($property->getValue()));
+
+ $property->setValue(null);
+ $this->assertNull($property->getValue());
+
+ $default = 'ABCАБВ';
+ $propertyWithDefault = new String('NameSecond', $default);
+ $this->assertEquals($default, $propertyWithDefault->getValue());
}
public function testIntegerProperty()
@@ -120,7 +133,7 @@ public function testDateProperty()
//check the MorphDate objects content is correct
$this->assertEquals($time, $property->__getRawValue()->sec);
}
-
+
public function testBinaryData()
{
$property = new BinaryData("AProperty");
@@ -133,12 +146,12 @@ public function testBinaryData()
//check the MorphDate objects content is correct
$this->assertEquals($data, $property->__getRawValue()->bin);
-
+
$property2 = new BinaryData("AProperty");
$property2->__setRawValue($property->__getRawValue());
$this->assertEquals($data, $property2->getValue());
}
-
+
public function testInteger32Property()
{
$min = 1;
@@ -156,10 +169,10 @@ public function testInteger32Property()
$property->setValue($toSmall);
$this->assertEquals($min, $property->getValue());
-
+
$this->assertInstanceOf('\MongoInt32', $property->__getRawValue());
}
-
+
public function testInteger64Property()
{
$min = 1;
@@ -177,18 +190,18 @@ public function testInteger64Property()
$property->setValue($toSmall);
$this->assertEquals($min, $property->getValue());
-
+
$this->assertInstanceOf('\MongoInt64', $property->__getRawValue());
}
-
+
public function testRegex()
{
$property = new Regex("AProperty");
- $data = '/bob/';
+ $data = '/bobМамаМоёт/';
$property->setValue($data);
$this->assertEquals($data, $property->getValue());
$this->assertInstanceOf('\MongoRegex', $property->__getRawValue());
$this->assertEquals($data, '/' . $property->__getRawValue()->regex . '/');
}
-
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.