silverstripe-framework/tests/php/ORM/DataObjectTest/MockDynamicAssignmentDBField.php

56 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\ORM\Tests\DataObjectTest;
2019-02-26 23:14:47 +01:00
use SilverStripe\Dev\TestOnly;
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
{
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.
*/
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.
* @param $value
2019-02-26 23:14:47 +01:00
* @return array|int
*/
public function prepValueForDB($value)
{
if ($value) {
return $this->dynamicAssignment
2019-02-26 23:14:47 +01:00
? ['ABS(?)' => [1]]
: 1;
}
return 0;
}
public function scalarValueOnly()
{
return $this->scalarOnly;
}
}