2019-02-21 23:08:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\ORM\Tests\DataObjectTest;
|
|
|
|
|
2019-02-26 23:14:47 +01:00
|
|
|
use SilverStripe\Dev\TestOnly;
|
2019-02-21 23:08:43 +01:00
|
|
|
use SilverStripe\ORM\FieldType\DBBoolean;
|
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a fake DB field specifically design to test dynamic value assignment. You can set `scalarValueOnly` in
|
|
|
|
* the constructor. You can control whetever the field will try to do a dynamic assignment by specifing
|
|
|
|
* `$dynamicAssignment` in nthe consturctor.
|
|
|
|
*
|
|
|
|
* If the field is set to false, it will try to do a plain assignment. This is so you can save the initial value no
|
|
|
|
* matter what. If the field is set to true, it will try to do a dynamic assignment.
|
|
|
|
*/
|
2019-02-26 23:14:47 +01:00
|
|
|
class MockDynamicAssignmentDBField extends DBBoolean implements TestOnly
|
2019-02-21 23:08:43 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
private $scalarOnly;
|
|
|
|
private $dynamicAssignment;
|
|
|
|
|
|
|
|
/**
|
2019-02-26 23:14:47 +01:00
|
|
|
* @param string $name
|
|
|
|
* @param boolean $scalarOnly Whether our fake field should be scalar only.
|
|
|
|
* @param boolean $dynamicAssignment Whether our fake field will try to do a dynamic assignment.
|
2019-02-21 23:08:43 +01:00
|
|
|
*/
|
|
|
|
public function __construct($name = '', $scalarOnly = false, $dynamicAssignment = false)
|
|
|
|
{
|
|
|
|
$this->scalarOnly = $scalarOnly;
|
|
|
|
$this->dynamicAssignment = $dynamicAssignment;
|
|
|
|
parent::__construct($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-26 23:14:47 +01:00
|
|
|
* If the field value and $dynamicAssignment are true, we'll try to do a dynamic assignment.
|
2019-02-21 23:08:43 +01:00
|
|
|
* @param $value
|
2019-02-26 23:14:47 +01:00
|
|
|
* @return array|int
|
2019-02-21 23:08:43 +01:00
|
|
|
*/
|
|
|
|
public function prepValueForDB($value)
|
|
|
|
{
|
|
|
|
if ($value) {
|
|
|
|
return $this->dynamicAssignment
|
2019-02-26 23:14:47 +01:00
|
|
|
? ['ABS(?)' => [1]]
|
2019-02-21 23:08:43 +01:00
|
|
|
: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scalarValueOnly()
|
|
|
|
{
|
|
|
|
return $this->scalarOnly;
|
|
|
|
}
|
|
|
|
}
|