Commit 56aa14e8 authored by Tino Goratsch's avatar Tino Goratsch

save current work in progress at implementing the data processor

parent 95dde1f7
......@@ -26,18 +26,25 @@ abstract class AbstractModel
* @var AbstractRepository
*/
protected $repository;
/**
* @var DataProcessor
*/
private $dataProcessor;
/**
* AbstractModel constructor.
* @param EventDispatcherInterface $eventDispatcher
* @param DataProcessor $dataProcessor
* @param AbstractRepository $repository
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
DataProcessor $dataProcessor,
AbstractRepository $repository
) {
$this->eventDispatcher = $eventDispatcher;
$this->repository = $repository;
$this->dataProcessor = $dataProcessor;
}
/**
......@@ -100,15 +107,7 @@ abstract class AbstractModel
*/
protected function prepareData(array $data)
{
$allowedColumns = $this->getAllowedColumns();
foreach ($data as $key => $value) {
if (!in_array($key, $allowedColumns)) {
unset($data[$key]);
}
}
return $data;
return $this->dataProcessor->processColumnData($data, $this->getAllowedColumns());
}
/**
......
......@@ -31,19 +31,21 @@ abstract class AbstractNestedSetModel extends AbstractModel
/**
* AbstractNestedSetModel constructor.
* @param EventDispatcherInterface $eventDispatcher
* @param DataProcessor $dataProcessor
* @param AbstractRepository $repository
* @param Insert $insertOperation
* @param Edit $editOperation
* @param $deleteOperation
* @param Delete $deleteOperation
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
DataProcessor $dataProcessor,
AbstractRepository $repository,
Insert $insertOperation,
Edit $editOperation,
Delete $deleteOperation
) {
parent::__construct($eventDispatcher, $repository);
parent::__construct($eventDispatcher, $dataProcessor, $repository);
$this->insertOperation = $insertOperation;
$this->editOperation = $editOperation;
......
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Model;
use ACP3\Core\Model\DataProcessor\ColumnTypeStrategyFactory;
class DataProcessor
{
/**
* @var ColumnTypeStrategyFactory
*/
protected $factory;
/**
* DataProcessor constructor.
* @param ColumnTypeStrategyFactory $factory
*/
public function __construct(ColumnTypeStrategyFactory $factory)
{
$this->factory = $factory;
}
/**
* @param array $formData
* @param array $allowedColumns
* @return array
*/
public function processColumnData(array $formData, array $allowedColumns)
{
$data = [];
foreach ($formData as $column => $value) {
if (array_key_exists($column, $allowedColumns)) {
$data[$column] = $this->factory->getStrategy($allowedColumns[$column])->doEscape($value);
} elseif (in_array($column, $allowedColumns)) {
$data[$column] = $value;
}
}
return $data;
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Model\DataProcessor\ColumnType;
interface ColumnTypeStrategyInterface
{
/**
* @param mixed $value
* @return mixed
*/
public function doEscape($value);
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Model\DataProcessor\ColumnType;
use ACP3\Core\Date;
class DateTimeColumnType implements ColumnTypeStrategyInterface
{
/**
* @var Date
*/
protected $date;
/**
* DateTimeColumnType constructor.
* @param Date $date
*/
public function __construct(Date $date)
{
$this->date = $date;
}
/**
* @param string $value
* @return string
*/
public function doEscape($value)
{
return $this->date->toSQL($value);
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Model\DataProcessor\ColumnType;
class DoubleColumnType implements ColumnTypeStrategyInterface
{
/**
* @param mixed $value
* @return float
*/
public function doEscape($value)
{
return doubleval($value);
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Model\DataProcessor\ColumnType;
class IntegerColumnType implements ColumnTypeStrategyInterface
{
/**
* @param mixed $value
* @return int
*/
public function doEscape($value)
{
return intval($value);
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Model\DataProcessor\ColumnType;
class RawColumnType implements ColumnTypeStrategyInterface
{
/**
* @param mixed $value
* @return mixed
*/
public function doEscape($value)
{
return $value;
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Model\DataProcessor\ColumnType;
use ACP3\Core\Helpers\Secure;
class TextColumnType implements ColumnTypeStrategyInterface
{
/**
* @var Secure
*/
protected $secure;
/**
* TextColumnType constructor.
* @param Secure $secure
*/
public function __construct(Secure $secure)
{
$this->secure = $secure;
}
/**
* @param mixed $value
* @return string
*/
public function doEscape($value)
{
return $this->secure->strEncode($value);
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Model\DataProcessor\ColumnType;
class TextWysiwygColumnType extends TextColumnType
{
/**
* @inheritdoc
*/
public function doEscape($value)
{
return $this->secure->strEncode($value, true);
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Model\DataProcessor;
use ACP3\Core\Model\DataProcessor\ColumnType\ColumnTypeStrategyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ColumnTypeStrategyFactory
{
/**
* @var ContainerInterface
*/
private $container;
/**
* ColumnTypeStrategyFactory constructor.
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* @param string $columnType
* @return ColumnTypeStrategyInterface
*/
public function getStrategy($columnType)
{
$serviceId = 'core.model.column_type.' . $columnType . '_column_type_strategy';
/** @var ColumnTypeStrategyInterface $service */
$service = $this->container->get($serviceId);
return $service;
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Model\DataProcessor;
interface ColumnTypes
{
const COLUMN_TYPE_DATETIME = 'datetime';
const COLUMN_TYPE_INT = 'integer';
const COLUMN_TYPE_DOUBLE = 'double';
const COLUMN_TYPE_TEXT = 'text';
const COLUMN_TYPE_TEXT_WYSIWYG = 'text_wysiwyg';
const COLUMN_TYPE_RAW = 'raw';
}
services:
core.model.column_type.date_time_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\DateTimeColumnType
arguments:
- '@core.date'
core.model.column_type.double_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\DoubleColumnType
core.model.column_type.integer_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\IntegerColumnType
core.model.column_type.raw_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\RawColumnType
core.model.column_type.text_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\TextColumnType
arguments:
- '@core.helpers.secure'
core.model.column_type.text_wysiwyg_column_type_strategy:
class: ACP3\Core\Model\DataProcessor\ColumnType\TextWysiwygColumnType
arguments:
- '@core.helpers.secure'
core.model.data_processor:
class: ACP3\Core\Model\DataProcessor
arguments:
- '@core.model.column_type_strategy_factory'
core.model.column_type_strategy_factory:
class: ACP3\Core\Model\DataProcessor\ColumnTypeStrategyFactory
arguments:
- '@service_container'
......@@ -11,6 +11,7 @@ imports:
- { resource: components/helpers.yml }
- { resource: components/http.yml }
- { resource: components/language.yml }
- { resource: components/model.yml }
- { resource: components/modules.yml }
- { resource: components/validation.yml }
- { resource: components/view.yml }
......
......@@ -7,9 +7,8 @@
namespace ACP3\Modules\ACP3\Articles\Model;
use ACP3\Core\Date;
use ACP3\Core\Helpers\Secure;
use ACP3\Core\Model\AbstractModel;
use ACP3\Core\Model\DataProcessor;
use ACP3\Modules\ACP3\Articles\Installer\Schema;
use ACP3\Modules\ACP3\Articles\Model\Repository\ArticleRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
......@@ -18,49 +17,29 @@ class ArticlesModel extends AbstractModel
{
const EVENT_PREFIX = Schema::MODULE_NAME;
/**
* @var Date
*/
protected $date;
/**
* @var Secure
*/
protected $secure;
/**
* ArticlesModel constructor.
* @param EventDispatcherInterface $eventDispatcher
* @param Date $date
* @param Secure $secure
* @param DataProcessor $dataProcessor
* @param ArticleRepository $articleRepository
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
Date $date,
Secure $secure,
DataProcessor $dataProcessor,
ArticleRepository $articleRepository
) {
parent::__construct($eventDispatcher, $articleRepository);
$this->date = $date;
$this->secure = $secure;
parent::__construct($eventDispatcher, $dataProcessor, $articleRepository);
}
/**
* @param array $formData
* @param array $data
* @param int $userId
* @param int|null $entryId
* @return bool|int
*/
public function saveArticle(array $formData, $userId, $entryId = null)
public function saveArticle(array $data, $userId, $entryId = null)
{
$data = [
'start' => $this->date->toSQL($formData['start']),
'end' => $this->date->toSQL($formData['end']),
'title' => $this->secure->strEncode($formData['title']),
'text' => $this->secure->strEncode($formData['text'], true),
'user_id' => $userId,
];
$data['user_id'] = $userId;
return $this->save($data, $entryId);
}
......@@ -71,11 +50,11 @@ class ArticlesModel extends AbstractModel
protected function getAllowedColumns()
{
return [
'start',
'end',
'title',
'text',
'user_id'
'start' => DataProcessor\ColumnTypes::COLUMN_TYPE_DATETIME,
'end' => DataProcessor\ColumnTypes::COLUMN_TYPE_DATETIME,
'title' => DataProcessor\ColumnTypes::COLUMN_TYPE_TEXT,
'text' => DataProcessor\ColumnTypes::COLUMN_TYPE_TEXT_WYSIWYG,
'user_id' => DataProcessor\ColumnTypes::COLUMN_TYPE_INT
];
}
}
......@@ -13,6 +13,5 @@ services:
class: ACP3\Modules\ACP3\Articles\Model\ArticlesModel
arguments:
- '@core.event_dispatcher'
- '@core.date'
- '@core.helpers.secure'
- '@core.model.data_processor'
- '@articles.model.articlerepository'
......@@ -9,6 +9,7 @@ namespace ACP3\Modules\ACP3\Categories\Model;
use ACP3\Core\Helpers\Secure;
use ACP3\Core\Model\AbstractModel;
use ACP3\Core\Model\DataProcessor;
use ACP3\Modules\ACP3\Categories\Installer\Schema;
use ACP3\Modules\ACP3\Categories\Model\Repository\CategoryRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
......@@ -25,15 +26,17 @@ class CategoriesModel extends AbstractModel
/**
* CategoriesModel constructor.
* @param EventDispatcherInterface $eventDispatcher
* @param DataProcessor $dataProcessor
* @param Secure $secure
* @param CategoryRepository $categoryRepository
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
DataProcessor $dataProcessor,
Secure $secure,
CategoryRepository $categoryRepository
) {
parent::__construct($eventDispatcher, $categoryRepository);
parent::__construct($eventDispatcher, $dataProcessor, $categoryRepository);
$this->secure = $secure;
}
......
......@@ -13,5 +13,6 @@ services:
class: ACP3\Modules\ACP3\Categories\Model\CategoriesModel
arguments:
- '@core.event_dispatcher'
- '@core.model.data_processor'
- '@core.helpers.secure'
- '@categories.model.categories_repository'
......@@ -8,4 +8,5 @@ services:
class: ACP3\Modules\ACP3\Comments\Model\CommentsModel
arguments:
- '@core.event_dispatcher'
- '@core.model.data_processor'
- '@comments.model.commentrepository'
......@@ -9,6 +9,7 @@ namespace ACP3\Modules\ACP3\Emoticons\Model;
use ACP3\Core\Helpers\Secure;
use ACP3\Core\Model\AbstractModel;
use ACP3\Core\Model\DataProcessor;
use ACP3\Modules\ACP3\Emoticons\Installer\Schema;
use ACP3\Modules\ACP3\Emoticons\Model\Repository\EmoticonRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
......@@ -25,15 +26,17 @@ class EmoticonsModel extends AbstractModel
/**
* EmoticonsModel constructor.
* @param EventDispatcherInterface $eventDispatcher
* @param DataProcessor $dataProcessor
* @param Secure $secure
* @param EmoticonRepository $emoticonRepository
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
DataProcessor $dataProcessor,
Secure $secure,
EmoticonRepository $emoticonRepository
) {
parent::__construct($eventDispatcher, $emoticonRepository);
parent::__construct($eventDispatcher, $dataProcessor, $emoticonRepository);
$this->secure = $secure;
}
......
......@@ -13,5 +13,6 @@ services:
class: ACP3\Modules\ACP3\Emoticons\Model\EmoticonsModel
arguments:
- '@core.event_dispatcher'
- '@core.model.data_processor'
- '@core.helpers.secure'
- '@emoticons.model.emoticonrepository'
......@@ -10,6 +10,7 @@ namespace ACP3\Modules\ACP3\Files\Model;
use ACP3\Core\Date;
use ACP3\Core\Helpers\Secure;
use ACP3\Core\Model\AbstractModel;
use ACP3\Core\Model\DataProcessor;
use ACP3\Modules\ACP3\Files\Installer\Schema;
use ACP3\Modules\ACP3\Files\Model\Repository\FilesRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
......@@ -34,17 +35,19 @@ class FilesModel extends AbstractModel
/**
* FilesModel constructor.
* @param EventDispatcherInterface $eventDispatcher
* @param DataProcessor $dataProcessor
* @param Date $date
* @param Secure $secure
* @param FilesRepository $filesRepository
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
DataProcessor $dataProcessor,
Date $date,
Secure $secure,
FilesRepository $filesRepository
) {
parent::__construct($eventDispatcher, $filesRepository);
parent::__construct($eventDispatcher, $dataProcessor, $filesRepository);
$this->secure = $secure;
$this->filesRepository = $filesRepository;
......
......@@ -13,6 +13,7 @@ services:
class: ACP3\Modules\ACP3\Files\Model\FilesModel
arguments:
- '@core.event_dispatcher'
- '@core.model.data_processor'
- '@core.date'
- '@core.helpers.secure'
- '@files.model.filesrepository'
......@@ -10,6 +10,7 @@ namespace ACP3\Modules\ACP3\Gallery\Model;
use ACP3\Core\Date;
use ACP3\Core\Helpers\Secure;
use ACP3\Core\Model\AbstractModel;
use ACP3\Core\Model\DataProcessor;
use ACP3\Modules\ACP3\Gallery\Installer\Schema;
use ACP3\Modules\ACP3\Gallery\Model\Repository\GalleryRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
......@@ -30,17 +31,19 @@ class GalleryModel extends AbstractModel
/**
* GalleryModel constructor.
* @param EventDispatcherInterface $eventDispatcher
* @param DataProcessor $dataProcessor
* @param Date $date
* @param Secure $secure
* @param GalleryRepository $galleryRepository
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
DataProcessor $dataProcessor,
Date $date,
Secure $secure,
GalleryRepository $galleryRepository)
{
parent::__construct($eventDispatcher, $galleryRepository);
GalleryRepository $galleryRepository
) {
parent::__construct($eventDispatcher, $dataProcessor, $galleryRepository);
$this->date = $date;
$this->secure = $secure;
......
......@@ -9,6 +9,7 @@ namespace ACP3\Modules\ACP3\Gallery\Model;
use ACP3\Core\Helpers\Secure;
use ACP3\Core\Model\AbstractModel;
use ACP3\Core\Model\DataProcessor;
use ACP3\Core\Settings\SettingsInterface;
use ACP3\Modules\ACP3\Gallery\Installer\Schema;
use ACP3\Modules\ACP3\Gallery\Model\Repository\PictureRepository;
......@@ -34,17 +35,19 @@ class PictureModel extends AbstractModel
/**
* PictureModel constructor.
* @param EventDispatcherInterface $eventDispatcher
* @param DataProcessor $dataProcessor
* @param SettingsInterface $config
* @param Secure $secure
* @param PictureRepository $pictureRepository
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
DataProcessor $dataProcessor,
SettingsInterface $config,
Secure $secure,
PictureRepository $pictureRepository
) {
parent::__construct($eventDispatcher, $pictureRepository);
parent::__construct($eventDispatcher, $dataProcessor, $pictureRepository);
$this->secure = $secure;
$this->config = $config;
......
......@@ -18,6 +18,7 @@ services:
class: ACP3\Modules\ACP3\Gallery\Model\GalleryModel
arguments:
- '@core.event_dispatcher'
- '@core.model.data_processor'
- '@core.date'
- '@core.helpers.secure'
- '@gallery.model.galleryrepository'
......@@ -26,6 +27,7 @@ services:
class: ACP3\Modules\ACP3\Gallery\Model\PictureModel
arguments:
- '@core.event_dispatcher'
- '@core.model.data_processor'
- '@core.config'
- '@core.helpers.secure'
- '@gallery.model.picturerepository'
......@@ -13,4 +13,5 @@ services:
class: ACP3\Modules\ACP3\Guestbook\Model\GuestbookModel
arguments:
- '@core.event_dispatcher'
- '@core.model.data_processor'
- '@guestbook.model.guestbookrepository'
......@@ -9,6 +9,7 @@ namespace ACP3\Modules\ACP3\Menus\Model;
use ACP3\Core\Helpers\Secure;
use ACP3\Core\Model\AbstractNestedSetModel;
use ACP3\Core\Model\DataProcessor;
use ACP3\Core\NestedSet\Operation\Delete;
use ACP3\Core\NestedSet\Operation\Edit;
use ACP3\Core\NestedSet\Operation\Insert;
......@@ -28,6 +29,7 @@ class MenuItemsModel extends AbstractNestedSetModel
/**
* MenuItemsModel constructor.
* @param EventDispatcherInterface $eventDispatcher
* @param DataProcessor $dataProcessor
* @param MenuItemRepository $repository
* @param Insert $insertOperation
* @param Edit $editOperation
......@@ -36,13 +38,14 @@ class MenuItemsModel extends AbstractNestedSetModel
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
DataProcessor $dataProcessor,
MenuItemRepository $repository,
Insert $insertOperation,
Edit $editOperation,
Delete $deleteOperation,
Secure $secure
) {
parent::__construct($eventDispatcher, $repository, $insertOperation, $editOperation, $deleteOperation);