Skip to content

Commit 4cfe6a7

Browse files
committed
Merge pull request #150 from AlexeyDsov/primitiveIdentifierUp
PrimitiveIdentifier now can export/import objects by custom methods
2 parents d9238d9 + ddda3b1 commit 4cfe6a7

File tree

4 files changed

+123
-12
lines changed

4 files changed

+123
-12
lines changed

core/Form/Primitives/IdentifiablePrimitive.class.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ abstract class IdentifiablePrimitive
1616
extends PrimitiveInteger // parent class doesn't really matter here
1717
{
1818
protected $className = null;
19+
private $extractMethod = 'getId';
1920

2021
/**
2122
* due to historical reasons, by default we're dealing only with
@@ -25,6 +26,29 @@ abstract class IdentifiablePrimitive
2526

2627
abstract public function of($className);
2728

29+
/**
30+
* @param mixed $extractMethod
31+
* @return IdentifiablePrimitive
32+
*/
33+
public function setExtractMethod($extractMethod)
34+
{
35+
if (is_callable($extractMethod)) {
36+
/* all ok, call what you want */
37+
} elseif (strpos($extractMethod, '::') === false) {
38+
Assert::isTrue(
39+
method_exists($this->className, $extractMethod),
40+
"knows nothing about '".$this->className
41+
."::{$extractMethod}' method"
42+
);
43+
} else {
44+
ClassUtils::checkStaticMethod($extractMethod);
45+
}
46+
47+
$this->extractMethod = $extractMethod;
48+
49+
return $this;
50+
}
51+
2852
/**
2953
* @return string
3054
**/
@@ -77,7 +101,7 @@ public function exportValue()
77101
if (!$this->value)
78102
return null;
79103

80-
return $this->value->getId();
104+
return $this->actualExportValue($this->value);
81105
}
82106

83107
/* void */ protected function checkNumber($number)
@@ -95,5 +119,20 @@ protected function castNumber($number)
95119

96120
return $number;
97121
}
122+
123+
protected function actualExportValue($value)
124+
{
125+
if (!ClassUtils::isInstanceOf($value, $this->className)) {
126+
return null;
127+
}
128+
129+
if (is_callable($this->extractMethod)) {
130+
return call_user_func($this->extractMethod, $value);
131+
} elseif (strpos($this->extractMethod, '::') === false) {
132+
return $value->{$this->extractMethod}($value);
133+
} else {
134+
ClassUtils::callStaticMethod($this->extractMethod, $value);
135+
}
136+
}
98137
}
99138
?>

core/Form/Primitives/PrimitiveIdentifier.class.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,19 @@ public function dao()
5555
**/
5656
public function setMethodName($methodName)
5757
{
58-
if (strpos($methodName, '::') === false) {
58+
if (is_callable($methodName)) {
59+
/* all ok, call what you want */
60+
} elseif (strpos($methodName, '::') === false) {
5961
$dao = $this->dao();
6062

6163
Assert::isTrue(
6264
method_exists($dao, $methodName),
6365
"knows nothing about '".get_class($dao)
6466
."::{$methodName}' method"
6567
);
66-
} else
68+
} else {
6769
ClassUtils::checkStaticMethod($methodName);
70+
}
6871

6972
$this->methodName = $methodName;
7073

@@ -79,7 +82,7 @@ public function importValue($value)
7982

8083
return
8184
$this->import(
82-
array($this->getName() => $value->getId())
85+
array($this->getName() => $this->actualExportValue($value))
8386
);
8487

8588
} catch (WrongArgumentException $e) {
@@ -105,7 +108,7 @@ public function import($scope)
105108
) {
106109
$value = $scope[$this->name];
107110

108-
$this->raw = $value->getId();
111+
$this->raw = $this->actualExportValue($value);
109112
$this->setValue($value);
110113

111114
return $this->imported = true;
@@ -139,12 +142,13 @@ public function import($scope)
139142

140143
protected function actualImportValue($value)
141144
{
142-
return
143-
(strpos($this->methodName, '::') === false)
144-
? $this->dao()->{$this->methodName}($value)
145-
: ClassUtils::callStaticMethod(
146-
$this->methodName, $value
147-
);
145+
if (is_callable($this->methodName)) {
146+
return call_user_func($this->methodName, $value);
147+
} elseif (strpos($this->methodName, '::') === false) {
148+
return $this->dao()->{$this->methodName}($value);
149+
} else {
150+
return ClassUtils::callStaticMethod($this->methodName, $value);
151+
}
148152
}
149153
}
150154
?>

doc/ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
2012-11-25 Nikita V. Konstantinov
2+
3+
* core/Form/Primitives/PrimitiveIdentifier.class.php
4+
core/Form/Primitives/PrimitiveIdentifier.class.php:
5+
PrimitiveIdentifier now can export/import objects by custom methods
6+
17
2012-11-18 Nikita V. Konstantinov
8+
29
* meta/types/ObjectType.class.php:
310
update for Business objects - now you can setSomeProperty(null)
411
for Objects properties if it's not required

test/core/PrimitiveIdentifierTest.class.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/* $Id$ */
33

4-
final class PrimitiveIdentifierTest extends TestCase
4+
final class PrimitiveIdentifierTest extends TestCaseDAO
55
{
66
public function testEmpty()
77
{
@@ -20,5 +20,66 @@ public function testEmpty()
2020
$this->assertFalse($prm->importValue($value));
2121
}
2222
}
23+
24+
/**
25+
* @group pi
26+
*/
27+
public function testCustomImportExport()
28+
{
29+
$dbs = DBTestPool::me()->getPool();
30+
if (empty($dbs)) {
31+
$this->fail('For test required at least one DB in config');
32+
}
33+
DBPool::me()->setDefault(reset($dbs));
34+
35+
$moscow = TestCity::create()->setCapital(true)->setName('Moscow');
36+
$moscow->dao()->add($moscow);
37+
38+
$stalingrad = TestCity::create()->setCapital(false)->setName('Stalingrad');
39+
$stalingrad->dao()->add($stalingrad);
40+
41+
$prms = array();
42+
$prms[] = Primitive::identifier('city')->
43+
setScalar(true)->
44+
of('TestCity')->
45+
setMethodName('PrimitiveIdentifierTest::getCityByName')->
46+
setExtractMethod('PrimitiveIdentifierTest::getCityName');
47+
48+
$prms[] = Primitive::identifier('city')->
49+
setScalar(true)->
50+
of('TestCity')->
51+
setMethodName(array(get_class($this), 'getCityByName'))->
52+
setExtractMethod(function(TestCity $city) {return $city->getName();});
53+
54+
foreach ($prms as $prm) {
55+
$prm->import(array('city' => 'Moscow'));
56+
$this->assertEquals($moscow, $prm->getValue());
57+
$this->assertEquals('Moscow', $prm->exportValue());
58+
59+
$prm->importValue($stalingrad);
60+
$this->assertequals($stalingrad, $prm->getValue());
61+
$this->assertequals('Stalingrad', $prm->exportValue());
62+
63+
$prm->import(array('city' => $moscow));
64+
$this->assertEquals($moscow, $prm->getValue());
65+
$this->assertEquals('Moscow', $prm->exportValue());
66+
}
67+
}
68+
69+
/**
70+
* @param string $name
71+
* @return TestCity
72+
*/
73+
public static function getCityByName($name)
74+
{
75+
return Criteria::create(TestCity::dao())->
76+
add(Expression::eq('name', DBValue::create($name)))->
77+
get();
78+
}
79+
80+
public static function getCityName(TestCity $city)
81+
{
82+
return $city->getName();
83+
}
2384
}
2485
?>

0 commit comments

Comments
 (0)