Download the PHP package dvasilenko/datamapper_tools without Composer
On this page you can find all versions of the php package dvasilenko/datamapper_tools. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dvasilenko/datamapper_tools
More information about dvasilenko/datamapper_tools
Files in dvasilenko/datamapper_tools
Package datamapper_tools
Short Description Useful utilities DataMapper
License MIT
Informations about the package datamapper_tools
DataMapper
DataMapper помогает вам преобразовать один формат данные в другой, вы можете получить как массив данных, так и XML.
Содержание
- Установка
- Использование
- Создать свой DataMapper
- Настройка своего DataMapper
- Создание своего обработчика типа данных
- Использование DataMapper
- Обработчики данных по умолчанию
- Дата и время
- Константа
- Справочник
- Простой
Установка
Установка с помощью Composer
Использование
Необходимо создать класс описывающий правила преобразования данных, отнаследовавшись от базавого класса BaseDataMapper.
Базово поддерживается 4 типа данных:
- Дата и время
- Константа
- Справочник
- Простой тип
При необходимости можно расширить своими типами данных.
Создать свой DataMapper
Настройка своего DataMapper
Обращение к элементам вложенного массива осуществляется через точку "."
Например, правила ниже позволяют обратиться к значениям элемента "client" и "order"
Создание своего обработчика типа данных
Класс обработчика должен реализовывать интерфейс ResolveInterface
Использование DataMapper
Результат:
Array
(
[Client] => Array
(
[Type] => Array
(
[attributes] => Array
(
[id] => 1
)
)
[Id] => 1
[Name] => Имя
[FIO] => Фамилия Имя
[DateBirth] => 01.01.2000
[Gender] => M
[Order] => Array
(
[Id] => 1
)
)
)
Если нужен XML
Результат:
Обработчики данных по умолчанию
Базово поддерживается 4 типа данных:
-
Дата и время
Необходимо указать формат времени (поддерживаются форматы нативного класса DateTime). Если значение передается в числовом виде, то считается, что это TimeStamp, так же можно отметить, что значение времени передается в милисекундах:
'type' => 'DateTime', 'key' => '', 'field' => '', 'params' => ['format' => 'd.m.Y', 'isMillisecond' => true]
-
Константа
Можно указать, чтоб значение передавалось как часть параметра в ввиде аттрибута:
'type' => 'Define', 'key' => '', 'field' => '', params => ['attribute' => 'id']
-
Справочник
Ниже перечислены разные варианты, допускается возможность их комбинирования.
Если нужно указать массив соответствий с условием числовых диапазонов:
'type' => 'Directory', 'key' => '', 'field' => '', params => [
'conformityCondition' => [
['from' => 1, 'to' => 30, 'value' => 1],
['from' => 31, 'to' => 60, 'value' => 2],
['from' => 61, 'to' => 90, 'value' => 3],
['from' => 91, 'to' => 180, 'value' => 4],
['from' => 181, 'to' => 365, 'value' => 5],
['from' => 366, 'to' => 1095, 'value' => 6],
['from' => 1096, 'value' => 7],
['value' => 8],
]
]
Простой массив соответсвтий:
'type' => 'Directory', 'key' => '', 'field' => '', 'params' => [
'conformity' => [
'man' => 'M',
'woman' => 'F',
],
'default' => 'M',
],
Если нужно предварительно привести к числовому типу данных (int):
type='Directory', 'key' => '', 'field' => '', 'params' => ['type' => 'int']
Если нужно отсеять по доп. полям:
type='Directory', 'key' => '', 'field' => '', 'params' => [
'propertyCode' => '',// код поля, содержащим массив доп. полей
'propertyId' => '',// наименование нужного поля
'propertyRowId' => '',// атрибут хранящий значение
],
Можно указать, чтоб значение передавалось как часть параметра в ввиде аттрибута:
'type' => 'Directory', 'key' => '', 'field' => '', 'params' => ['attribute' => 'id']
-
Простой тип
Можно указать ключ "funcs", в котором перечислить функции, которыми необходимо обработать полученное значение:
'type' => 'Simple', 'key' => '', 'field' => '', 'funcs' => 'trim|mb_strtoupper'
Можно указать, чтоб значение передавалось как часть параметра в ввиде аттрибута:
type='Simple', key='' field='' params=['attribute' => 'id']