Skip to content

Commit

Permalink
extension & filters
Browse files Browse the repository at this point in the history
  • Loading branch information
dezbyte committed Jan 24, 2018
1 parent b4d93e1 commit 9570e38
Show file tree
Hide file tree
Showing 17 changed files with 668 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Colibri/Core/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function setVirtual($offset, $value = null)
*/
public function hashCode()
{

return sha1(json_encode($this->toArray()));
}

/**
Expand Down
87 changes: 87 additions & 0 deletions src/Colibri/Extension/AbstractExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Colibri\Extension;

use Colibri\Common\Callback;
use Colibri\Core\Event\EntityLifecycleEvent;
use Colibri\EventDispatcher\EventSubscriber;
use Colibri\Exception\NotFoundException;
use Colibri\Parameters\ParametersCollection;

/**
* Class AbstractExtension
* @package Colibri\Extension
*/
abstract class AbstractExtension implements ExtensionInterface, EventSubscriber
{

/**
* @var ParametersCollection
*/
protected $configuration;

/**
* AbstractExtension constructor.
* @param ParametersCollection $configuration
* @throws NotFoundException
*/
public function __construct(ParametersCollection $configuration)
{
$extensionConfiguration = $configuration->path(sprintf('extensions.%s', $this->getNameNS()));

if (null === $extensionConfiguration) {
throw new NotFoundException(sprintf('Extension could not be loaded because configuration for "%s" does not exist',
$this->getNameNS()));
}

$this->setConfiguration($extensionConfiguration);
}

/**
* @inheritDoc
*/
public function setConfiguration(ParametersCollection $collection)
{
$this->configuration = $collection;

return $this;
}

/**
* @inheritDoc
*/
public function getConfiguration()
{
return $this->configuration;
}

/**
* @param EntityLifecycleEvent $event
* @param callable $callback
*/
protected function resolveEntities(EntityLifecycleEvent $event, callable $callback)
{
$callback = new Callback($callback);

foreach ($this->getConfiguration() as $entityClassName => $extensionConfiguration) {
if ($event->getEntity() instanceof $entityClassName) {
$entity = $event->getEntity();
$callback->call(...[$entity, $extensionConfiguration]);
}
}
}

/**
* @param $variable
* @return bool
*/
protected static function isIterable($variable)
{
if (version_compare(PHP_VERSION, 7.1) === -1) {
return is_array($variable) || ($variable instanceof \Traversable);
} else {
return is_iterable($variable);
}
}

}
23 changes: 23 additions & 0 deletions src/Colibri/Extension/EventSubscriber/AbstractDataFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Colibri\Extension\EventSubscriber;

use Colibri\Core\ORMEvents;
use Colibri\Extension\AbstractExtension;

/**
* Class AbstractDataFilter
* @package Colibri\Extension\EventSubscriber
*/
abstract class AbstractDataFilter extends AbstractExtension
{

/**
* @return array
*/
public function getEvents()
{
return [ORMEvents::beforePersist];
}

}
58 changes: 58 additions & 0 deletions src/Colibri/Extension/EventSubscriber/DataFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Colibri\Extension\EventSubscriber;

use Colibri\Core\Entity\EntityInterface;
use Colibri\Core\Event\EntityLifecycleEvent;
use Colibri\Parameters\ParametersCollection;
use Colibri\Filters\FilterInterface;

/**
* Class DataFilter
* @package Colibri\Extension\EventSubscriber
*/
class DataFilter extends AbstractDataFilter
{

/**
* @inheritDoc
*/
public function getNameNS()
{
return 'dataFilter';
}

/**
* @param EntityLifecycleEvent $event
*/
public function beforePersist(EntityLifecycleEvent $event)
{
$this->resolveEntities($event, function (EntityInterface $entity, ParametersCollection $parameters) {
/** @var ParametersCollection $configuration */
foreach ($parameters as $propertyName => $configuration) {
if ($entity->hasProperty($propertyName) && $configuration->offsetExists('filters')) {
$propertyData = $entity->getByProperty($propertyName);
$propertyData = $this->applyFilters($propertyData, $configuration->offsetGet('filters')->toArray());
$entity->setByProperty($propertyName, $propertyData);
}
}
});
}

/**
* @param $propertyData
* @param array $filterClasses
* @return array|int|string
*/
protected function applyFilters(&$propertyData, array $filterClasses)
{
/** @var FilterInterface $filter */
foreach ($filterClasses as $filterClass => $filterClassArguments) {
$filter = new $filterClass(...$filterClassArguments);
$propertyData = $filter->apply($propertyData);
}

return $propertyData;
}

}
78 changes: 78 additions & 0 deletions src/Colibri/Extension/EventSubscriber/ResourceLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Colibri\Extension\EventSubscriber;

use Colibri\Collection\Collection;
use Colibri\Core\Event\EntityLifecycleEvent;
use Colibri\Core\ORMEvents;
use Colibri\Extension\AbstractExtension;
use Colibri\Parameters\ParametersCollection;

/**
* Class ResourceLogger
* @package Colibri\Extension\EventSubscriber
*/
class ResourceLogger extends AbstractExtension
{

protected $entitiesStates;

/**
* @inheritDoc
*/
public function __construct(ParametersCollection $configuration)
{
parent::__construct($configuration);

$this->entitiesStates = new Collection();
}

/**
* @inheritdoc
*/
public function getEvents()
{
return [ORMEvents::beforePersist, ORMEvents::afterPersist];
}

/**
* @inheritdoc
*/
public function getNameNS()
{
return 'resourceLogger';
}

/**
* @param EntityLifecycleEvent $event
*/
public function beforePersist(EntityLifecycleEvent $event)
{
$entity = $event->getEntity();
$states = $this->getEntitiesStates();

$states->set($entity->hashCode(), $entity);
}

/**
* @param EntityLifecycleEvent $event
*/
public function afterPersist(EntityLifecycleEvent $event)
{
$entity = $event->getEntity();
$states = $this->getEntitiesStates();

if (!$states->has($entity->hashCode())) {

}
}

/**
* @return Collection
*/
public function getEntitiesStates()
{
return $this->entitiesStates;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Colibri\Extension\EventSubscriber\ResourceLogger;

/**
* Interface ResourceModelInterface
* @package Colibri\Extension\EventSubscriber\ResourceLogger
*/
interface ResourceModelInterface
{

}
70 changes: 70 additions & 0 deletions src/Colibri/Extension/EventSubscriber/Sluggable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Colibri\Extension\EventSubscriber;

use Behat\Transliterator\Transliterator;
use Colibri\Core\Entity\EntityInterface;
use Colibri\Core\Event\EntityLifecycleEvent;
use Colibri\Core\ORMEvents;
use Colibri\Extension\AbstractExtension;
use Colibri\Parameters\ParametersCollection;

/**
* Class Sluggable
* @package Colibri\Extension\EventSubscriber
*/
class Sluggable extends AbstractExtension
{

/**
* @return array
*/
public function getEvents()
{
return [ORMEvents::beforePersist];
}

/**
* @inheritDoc
*/
public function getNameNS()
{
return 'sluggable';
}

/**
* @param EntityInterface $entity
* @param ParametersCollection $configuration
* @return array
*/
protected function getSlugForEntity(EntityInterface $entity, ParametersCollection $configuration)
{
$parts = [];

foreach ($configuration->offsetGet('properties') as $property) {
if ($entity->hasProperty($property)) {
$parts[] = Transliterator::urlize(Transliterator::transliterate($entity->getByProperty($property)));
}
}

$string = implode($configuration->offsetGet('separator'), $parts);

return sprintf('%s%s%s', $configuration->get('prefix'), $string, $configuration->get('suffix'));
}

/**
* @param EntityLifecycleEvent $event
*/
public function beforePersist(EntityLifecycleEvent $event)
{
foreach ($this->getConfiguration() as $entityClassName => $propertyConfiguration) {
if ($event->getEntity() instanceof $entityClassName) {
$entity = $event->getEntity();
foreach ($propertyConfiguration as $propertyName => $configuration) {
$entity->setByProperty($propertyName, $this->getSlugForEntity($entity, $configuration));
}
}
}
}

}
Loading

0 comments on commit 9570e38

Please sign in to comment.