Skip to content

Commit

Permalink
Adding ConstStrategy::beforeFind for append Enum Field on Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelqueiroz committed Mar 16, 2021
1 parent 14311b4 commit d354c5d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Model/Behavior/EnumBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,20 @@ public function __call(string $method, array $args): bool

return array_key_exists($entity->{$config['field']}, $this->enum($alias));
}

/**
* @param \Cake\Event\EventInterface $event The beforeFind event that was fired.
* @param \Cake\ORM\Query $query Query
* @param \ArrayObject $options The options for the query
* @return void
*/
public function beforeFind(\Cake\Event\EventInterface $event, \Cake\ORM\Query $query, \ArrayObject $options)
{
foreach ($this->getConfig('lists') as $alias => $config) {
$strategy = $this->strategy($alias, $config['strategy']);
if (method_exists($strategy, 'beforeFind')) {
$strategy->beforeFind($event, $query, $options);
}
}
}
}
50 changes: 50 additions & 0 deletions src/Model/Behavior/Strategy/ConstStrategy.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

/**
Expand All @@ -14,6 +15,8 @@
namespace CakeDC\Enum\Model\Behavior\Strategy;

use Cake\ORM\Table;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use ReflectionClass;

class ConstStrategy extends AbstractStrategy
Expand Down Expand Up @@ -92,4 +95,51 @@ protected function _getConstants(): array

return $this->_constants = $constants;
}

/**
* @param \Cake\Event\EventInterface $event The beforeFind event that was fired.
* @param \Cake\ORM\Query $query Query
* @param \ArrayObject $options The options for the query
* @return void
*/
public function beforeFind(\Cake\Event\EventInterface $event, \Cake\ORM\Query $query, \ArrayObject $options)
{
$assocName = Inflector::pluralize(Inflector::classify($this->_alias));
if (!in_array($assocName, array_keys($query->getContain()))) {
return ;
}

$contain = array_filter($query->getContain(), function($value) use ($assocName) {
return $value !== $assocName;
}, ARRAY_FILTER_USE_KEY);

$query->clearContain()->contain($contain);

$query->formatResults(function (\Cake\Collection\CollectionInterface $results) {
return $results->map(function ($row) {
if (is_string($row)) {
return $row;
}

$constant = Hash::get($row, $this->getConfig('field'));

$field = Inflector::singularize(Inflector::underscore($this->_alias));
$value = new \Cake\ORM\Entity([
'label' => Hash::get($this->_getConstants(), $constant, $constant),
'prefix' => $this->getConfig('prefix'),
'value' => $constant
], ['markClean' => true, 'markNew' => false]);

if (is_array($row)) {
$row[$field] = $value->toArray();
return $row;
}

$row->set($field, $value);
$row->setDirty($field, false);

return $row;
});
});
}
}

0 comments on commit d354c5d

Please sign in to comment.