Skip to content

Commit

Permalink
Added the table as an argument for the domain checker callables
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 30, 2014
1 parent 6280b8c commit 4122b8f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/ORM/DomainChecker.php
Expand Up @@ -28,6 +28,12 @@ class DomainChecker {

protected $_updateRules = [];

protected $_options = [];

public function __construct(array $options) {
$this->_options = $options;
}

public function add(callable $rule) {
$this->_rules[] = $rule;
return $this;
Expand All @@ -46,15 +52,15 @@ public function addUpdate(callable $rule) {
public function checkCreate(EntityInterface $entity) {
$success = true;
foreach (array_merge($this->_rules, $this->_createRules) as $rule) {
$success = $rule($entity) && $success;
$success = $rule($entity, $this->_options) && $success;
}
return $success;
}

public function checkUpdate(EntityInterface $entity) {
$success = true;
foreach (array_merge($this->_rules, $this->_updateRules) as $rule) {
$success = $rule($entity) && $success;
$success = $rule($entity, $this->_options) && $success;
}
return $success;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Table.php
Expand Up @@ -1856,7 +1856,7 @@ public function domainRules() {
if ($this->_domainChecker !== null) {
return $this->_domainChecker;
}
return $this->_domainChecker = $this->buildDomainRules(new DomainChecker);
return $this->_domainChecker = $this->buildDomainRules(new DomainChecker(['scope' => $this]));
}

public function buildDomainRules(DomainChecker $rules) {
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/ORM/DomainRulesIntegrationTest.php
Expand Up @@ -60,7 +60,8 @@ public function testsSaveBelongsToWithValidationError() {
$table->association('authors')
->target()
->domainRules()
->add(function (Entity $author) {
->add(function (Entity $author, array $options) use ($table) {
$this->assertSame($options['scope'], $table->association('authors')->target());
$author->errors('name', ['This is an error']);
return false;
});
Expand Down

0 comments on commit 4122b8f

Please sign in to comment.