First commit

This commit is contained in:
Pierre Hubert
2016-11-19 12:08:12 +01:00
commit 990540b2b9
4706 changed files with 931207 additions and 0 deletions

View File

@ -0,0 +1,557 @@
<?php
namespace RocketTheme\Toolbox\Blueprints;
use RocketTheme\Toolbox\ArrayTraits\Export;
use RocketTheme\Toolbox\ArrayTraits\ExportInterface;
use RocketTheme\Toolbox\ArrayTraits\NestedArrayAccessWithGetters;
/**
* The Config class contains configuration information.
*
* @author RocketTheme
*/
abstract class BlueprintForm implements \ArrayAccess, ExportInterface
{
use NestedArrayAccessWithGetters, Export;
/**
* @var array
*/
protected $items;
/**
* @var string
*/
protected $filename;
/**
* @var string
*/
protected $context;
/**
* @var array
*/
protected $overrides = [];
/**
* @var array
*/
protected $dynamic = [];
/**
* Load file and return its contents.
*
* @param string $filename
* @return string
*/
abstract protected function loadFile($filename);
/**
* Get list of blueprint form files (file and its parents for overrides).
*
* @param string|array $path
* @param string $context
* @return array
*/
abstract protected function getFiles($path, $context = null);
/**
* Constructor.
*
* @param string|array $filename
* @param array $items
*/
public function __construct($filename = null, array $items = [])
{
$this->nestedSeparator = '/';
$this->filename = $filename;
$this->items = $items;
}
/**
* Set filename for the blueprint. Can also be array of files for parent lookup.
*
* @param string|array $filename
* @return $this
*/
public function setFilename($filename)
{
$this->filename = $filename;
return $this;
}
/**
* Get the filename of the blueprint.
*
* @return array|null|string
*/
public function getFilename()
{
return $this->filename;
}
/**
* Set context for import@ and extend@.
*
* @param $context
* @return $this
*/
public function setContext($context)
{
$this->context = $context;
return $this;
}
/**
* Set custom overrides for import@ and extend@.
*
* @param array $overrides
* @return $this
*/
public function setOverrides($overrides)
{
$this->overrides = $overrides;
return $this;
}
/**
* Load blueprint.
*
* @return $this
*/
public function load()
{
// Only load and extend blueprint if it has not yet been loaded.
if (empty($this->items) && $this->filename) {
// Get list of files.
$files = $this->getFiles($this->filename);
// Load and extend blueprints.
$data = $this->doLoad($files);
$this->items = (array) array_shift($data);
foreach ($data as $content) {
$this->extend($content, true);
}
}
// Import blueprints.
$this->deepInit($this->items);
return $this;
}
/**
* Initialize blueprints with its dynamic fields.
*
* @return $this
*/
public function init()
{
foreach ($this->dynamic as $key => $data) {
// Locate field.
$path = explode('/', $key);
$current = &$this->items;
foreach ($path as $field) {
if (is_object($current)) {
// Handle objects.
if (!isset($current->{$field})) {
$current->{$field} = array();
}
$current = &$current->{$field};
} else {
// Handle arrays and scalars.
if (!is_array($current)) {
$current = array($field => array());
} elseif (!isset($current[$field])) {
$current[$field] = array();
}
$current = &$current[$field];
}
}
// Set dynamic property.
foreach ($data as $property => $call) {
$action = 'dynamic' . ucfirst($call['action']);
if (method_exists($this, $action)) {
$this->{$action}($current, $property, $call);
}
}
}
return $this;
}
/**
* Get form.
*
* @return array
*/
public function form()
{
return (array) $this->get('form');
}
/**
* Get form fields.
*
* @return array
*/
public function fields()
{
return (array) $this->get('form/fields');
}
/**
* Extend blueprint with another blueprint.
*
* @param BlueprintForm|array $extends
* @param bool $append
* @return $this
*/
public function extend($extends, $append = false)
{
if ($extends instanceof BlueprintForm) {
$extends = $extends->toArray();
}
if ($append) {
$a = $this->items;
$b = $extends;
} else {
$a = $extends;
$b = $this->items;
}
$this->items = $this->deepMerge($a, $b);
return $this;
}
/**
* @param string $name
* @param mixed $value
* @param string $separator
* @param bool $append
* @return $this
*/
public function embed($name, $value, $separator = '/', $append = false)
{
$oldValue = $this->get($name, null, $separator);
if (is_array($oldValue) && is_array($value)) {
if ($append) {
$a = $oldValue;
$b = $value;
} else {
$a = $value;
$b = $oldValue;
}
$value = $this->deepMerge($a, $b);
}
$this->set($name, $value, $separator);
return $this;
}
/**
* Get blueprints by using dot notation for nested arrays/objects.
*
* @example $value = $this->resolve('this.is.my.nested.variable');
* returns ['this.is.my', 'nested.variable']
*
* @param array $path
* @param string $separator
* @return array
*/
public function resolve(array $path, $separator = '/')
{
$fields = false;
$parts = [];
$current = $this['form.fields'];
$result = [null, null, null];
while (($field = current($path)) !== null) {
if (!$fields && isset($current['fields'])) {
if (!empty($current['array'])) {
$result = [$current, $parts, $path ? implode($separator, $path) : null];
// Skip item offset.
$parts[] = array_shift($path);
}
$current = $current['fields'];
$fields = true;
} elseif (isset($current[$field])) {
$parts[] = array_shift($path);
$current = $current[$field];
$fields = false;
} elseif (isset($current['.' . $field])) {
$parts[] = array_shift($path);
$current = $current['.' . $field];
$fields = false;
} else {
break;
}
}
return $result;
}
/**
* Deep merge two arrays together.
*
* @param array $a
* @param array $b
* @return array
*/
protected function deepMerge(array $a, array $b)
{
$bref_stack = array(&$a);
$head_stack = array($b);
do {
end($bref_stack);
$bref = &$bref_stack[key($bref_stack)];
$head = array_pop($head_stack);
unset($bref_stack[key($bref_stack)]);
foreach (array_keys($head) as $key) {
if (!empty($key) && ($key[0] === '@' || $key[strlen($key) - 1] === '@')) {
$list = explode('-', trim($key, '@'), 2);
$action = array_shift($list);
if ($action === 'unset' || $action === 'replace') {
$property = array_shift($list);
if (!$property) {
$bref = ['unset@' => true];
} else {
unset($bref[$property]);
}
continue;
}
}
if (isset($key, $bref[$key]) && is_array($bref[$key]) && is_array($head[$key])) {
$bref_stack[] = &$bref[$key];
$head_stack[] = $head[$key];
} else {
$bref = array_merge($bref, [$key => $head[$key]]);
}
}
} while (count($head_stack));
return $a;
}
/**
* @param array $items
* @param array $path
* @return string
*/
protected function deepInit(array &$items, $path = [])
{
$ordering = '';
$order = [];
$field = end($path) === 'fields';
foreach ($items as $key => &$item) {
// Set name for nested field.
if ($field && isset($item['type'])) {
$item['name'] = $key;
}
// Handle special instructions in the form.
if (!empty($key) && ($key[0] === '@' || $key[strlen($key) - 1] === '@')) {
$list = explode('-', trim($key, '@'), 2);
$action = array_shift($list);
$property = array_shift($list);
switch ($action) {
case 'unset':
unset($items[$key]);
if (empty($items)) {
return null;
}
break;
case 'import':
$this->doImport($item, $path);
unset($items[$key]);
break;
case 'ordering':
$ordering = $item;
unset($items[$key]);
break;
default:
$this->dynamic[implode('/', $path)][$property] = ['action' => $action, 'params' => $item];
}
} elseif (is_array($item)) {
// Recursively initialize form.
$newPath = array_merge($path, [$key]);
$location = $this->deepInit($item, $newPath);
if ($location) {
$order[$key] = $location;
} elseif ($location === null) {
unset($items[$key]);
}
}
}
if ($order) {
// Reorder fields if needed.
$items = $this->doReorder($items, $order);
}
return $ordering;
}
/**
* @param array|string $value
* @param array $path
*/
protected function doImport(&$value, array &$path)
{
$type = !is_string($value) ? !isset($value['type']) ? null : $value['type'] : $value;
$files = $this->getFiles($type, isset($value['context']) ? $value['context'] : null);
if (!$files) {
return;
}
/** @var BlueprintForm $blueprint */
$blueprint = new static($files);
$blueprint->setContext($this->context)->setOverrides($this->overrides)->load();
$name = implode('/', $path);
$this->embed($name, $blueprint->form(), '/', false);
}
/**
* Internal function that handles loading extended blueprints.
*
* @param array $files
* @return array
*/
protected function doLoad(array $files)
{
$filename = array_shift($files);
$content = $this->loadFile($filename);
if (isset($content['extends@'])) {
$key = 'extends@';
} elseif (isset($content['@extends'])) {
$key = '@extends';
} elseif (isset($content['@extends@'])) {
$key = '@extends@';
}
$data = isset($key) ? $this->doExtend($filename, $files, (array) $content[$key]) : [];
if (isset($key)) {
unset($content[$key]);
}
$data[] = $content;
return $data;
}
/**
* Internal function to recursively load extended blueprints.
*
* @param string $filename
* @param array $parents
* @param array $extends
* @return array
*/
protected function doExtend($filename, array $parents, array $extends)
{
if (is_string(key($extends))) {
$extends = [$extends];
}
$data = [];
foreach ($extends as $value) {
// Accept array of type and context or a string.
$type = !is_string($value)
? !isset($value['type']) ? null : $value['type'] : $value;
if (!$type) {
continue;
}
if ($type === '@parent' || $type === 'parent@') {
if (!$parents) {
throw new \RuntimeException("Parent blueprint missing for '{$filename}'");
}
$files = $parents;
} else {
$files = $this->getFiles($type, isset($value['context']) ? $value['context'] : null);
// Detect extend loops.
if ($files && array_intersect($files, $parents)) {
// Let's check if user really meant extends@: parent@.
$index = array_search($filename, $files);
if ($index !== false) {
// We want to grab only the parents of the file which is currently being loaded.
$files = array_slice($files, $index + 1);
}
if ($files !== $parents) {
throw new \RuntimeException("Loop detected while extending blueprint file '{$filename}'");
}
if (!$parents) {
throw new \RuntimeException("Parent blueprint missing for '{$filename}'");
}
}
}
if ($files) {
$data = array_merge($data, $this->doLoad($files));
}
}
return $data;
}
/**
* Internal function to reorder items.
*
* @param array $items
* @param array $keys
* @return array
*/
protected function doReorder(array $items, array $keys)
{
$reordered = array_keys($items);
foreach ($keys as $item => $ordering) {
if ((string)(int) $ordering === (string) $ordering) {
$location = array_search($item, $reordered);
$rel = array_splice($reordered, $location, 1);
array_splice($reordered, $ordering, 0, $rel);
} elseif (isset($items[$ordering])) {
$location = array_search($item, $reordered);
$rel = array_splice($reordered, $location, 1);
$location = array_search($ordering, $reordered);
array_splice($reordered, $location + 1, 0, $rel);
}
}
return array_merge(array_flip($reordered), $items);
}
}

View File

@ -0,0 +1,685 @@
<?php
namespace RocketTheme\Toolbox\Blueprints;
/**
* BlueprintSchema is used to define a data structure.
*
* @package RocketTheme\Toolbox\Blueprints
* @author RocketTheme
* @license MIT
*/
class BlueprintSchema
{
/**
* @var array
*/
protected $items = [];
/**
* @var array
*/
protected $rules = [];
/**
* @var array
*/
protected $nested = [];
/**
* @var array
*/
protected $dynamic = [];
/**
* @var array
*/
protected $filter = ['validation' => true];
/**
* @var array
*/
protected $ignoreFormKeys = ['fields' => 1];
/**
* @var array
*/
protected $types = [];
/**
* Constructor.
*
* @param array $serialized Serialized content if available.
*/
public function __construct($serialized = null)
{
if (is_array($serialized) && !empty($serialized)) {
$this->items = (array) $serialized['items'];
$this->rules = (array) $serialized['rules'];
$this->nested = (array) $serialized['nested'];
$this->dynamic = (array) $serialized['dynamic'];
$this->filter = (array) $serialized['filter'];
}
}
/**
* @param array $types
* @return $this
*/
public function setTypes(array $types)
{
$this->types = $types;
return $this;
}
/**
* Restore Blueprints object.
*
* @param array $serialized
* @return static
*/
public static function restore(array $serialized)
{
return new static($serialized);
}
/**
* Initialize blueprints with its dynamic fields.
*
* @return $this
*/
public function init()
{
foreach ($this->dynamic as $key => $data) {
$field = &$this->items[$key];
foreach ($data as $property => $call) {
$action = 'dynamic' . ucfirst($call['action']);
if (method_exists($this, $action)) {
$this->{$action}($field, $property, $call);
}
}
}
return $this;
}
/**
* Set filter for inherited properties.
*
* @param array $filter List of field names to be inherited.
*/
public function setFilter(array $filter)
{
$this->filter = array_flip($filter);
}
/**
* Get value by using dot notation for nested arrays/objects.
*
* @example $value = $data->get('this.is.my.nested.variable');
*
* @param string $name Dot separated path to the requested value.
* @param mixed $default Default value (or null).
* @param string $separator Separator, defaults to '.'
*
* @return mixed Value.
*/
public function get($name, $default = null, $separator = '.')
{
$name = $separator != '.' ? strtr($name, $separator, '.') : $name;
return isset($this->items[$name]) ? $this->items[$name] : $default;
}
/**
* Set value by using dot notation for nested arrays/objects.
*
* @example $value = $data->set('this.is.my.nested.variable', $newField);
*
* @param string $name Dot separated path to the requested value.
* @param mixed $value New value.
* @param string $separator Separator, defaults to '.'
*/
public function set($name, $value, $separator = '.')
{
$name = $separator != '.' ? strtr($name, $separator, '.') : $name;
$this->items[$name] = $value;
$this->addProperty($name);
}
/**
* Define value by using dot notation for nested arrays/objects.
*
* @example $value = $data->set('this.is.my.nested.variable', true);
*
* @param string $name Dot separated path to the requested value.
* @param mixed $value New value.
* @param string $separator Separator, defaults to '.'
*/
public function def($name, $value, $separator = '.')
{
$this->set($name, $this->get($name, $value, $separator), $separator);
}
/**
* @return array
* @deprecated
*/
public function toArray()
{
return $this->getState();
}
/**
* Convert object into an array.
*
* @return array
*/
public function getState()
{
return [
'items' => $this->items,
'rules' => $this->rules,
'nested' => $this->nested,
'dynamic' => $this->dynamic,
'filter' => $this->filter
];
}
/**
* Get nested structure containing default values defined in the blueprints.
*
* Fields without default value are ignored in the list.
*
* @return array
*/
public function getDefaults()
{
return $this->buildDefaults($this->nested);
}
/**
* Embed an array to the blueprint.
*
* @param $name
* @param array $value
* @param string $separator
* @param bool $merge Merge fields instead replacing them.
* @return $this
*/
public function embed($name, array $value, $separator = '.', $merge = false)
{
if (isset($value['rules'])) {
$this->rules = array_merge($this->rules, $value['rules']);
}
if (!isset($value['form']['fields']) || !is_array($value['form']['fields'])) {
$value['form']['fields'] = [];
}
$name = $separator != '.' ? strtr($name, $separator, '.') : $name;
$form = array_diff_key($value['form'], ['fields' => 1]);
$items = isset($this->items[$name]) ? $this->items[$name] : ['type' => '_root', 'form_field' => false];
$this->items[$name] = [
'form' => $form
] + $items;
$this->addProperty($name);
$prefix = $name ? $name . '.' : '';
$params = array_intersect_key($form, $this->filter);
$location = [$name];
$this->parseFormFields($value['form']['fields'], $params, $prefix, '', $merge, $location);
return $this;
}
/**
* Merge two arrays by using blueprints.
*
* @param array $data1
* @param array $data2
* @param string $name Optional
* @param string $separator Optional
* @return array
*/
public function mergeData(array $data1, array $data2, $name = null, $separator = '.')
{
$nested = $this->getNested($name, $separator);
if (!is_array($nested)) {
$nested = [];
}
return $this->mergeArrays($data1, $data2, $nested);
}
/**
* Get the property with given path.
*
* @param string $path
* @param string $separator
* @return mixed
*/
public function getProperty($path = null, $separator = '.')
{
$name = $this->getPropertyName($path, $separator);
$property = $this->get($name);
$nested = $this->getNested($name);
return $this->getPropertyRecursion($property, $nested);
}
/**
* Returns name of the property with given path.
*
* @param string $path
* @param string $separator
* @return string
*/
public function getPropertyName($path = null, $separator = '.')
{
$parts = explode($separator, $path);
$nested = $this->nested;
$result = [];
while (($part = array_shift($parts)) !== null) {
if (!isset($nested[$part])) {
if (isset($nested['*'])) {
$part = '*';
} else {
return implode($separator, array_merge($result, [$part], $parts));
}
}
$result[] = $part;
$nested = $nested[$part];
}
return implode('.', $result);
}
/**
* Return data fields that do not exist in blueprints.
*
* @param array $data
* @param string $prefix
* @return array
*/
public function extra(array $data, $prefix = '')
{
$rules = $this->nested;
// Drill down to prefix level
if (!empty($prefix)) {
$parts = explode('.', trim($prefix, '.'));
foreach ($parts as $part) {
$rules = isset($rules[$part]) ? $rules[$part] : [];
}
}
return $this->extraArray($data, $rules, $prefix);
}
/**
* Get the property with given path.
*
* @param $property
* @param $nested
* @return mixed
*/
protected function getPropertyRecursion($property, $nested)
{
if (!isset($property['type']) || empty($nested) || !is_array($nested)) {
return $property;
}
if ($property['type'] === '_root') {
foreach ($nested as $key => $value) {
if ($key === '') {
continue;
}
$name = is_array($value) ? $key : $value;
$property['fields'][$key] = $this->getPropertyRecursion($this->get($name), $value);
}
} elseif ($property['type'] === '_parent' || !empty($property['array'])) {
foreach ($nested as $key => $value) {
$name = is_array($value) ? "{$property['name']}.{$key}" : $value;
$property['fields'][$key] = $this->getPropertyRecursion($this->get($name), $value);
}
}
return $property;
}
/**
* Get property from the definition.
*
* @param string $path Comma separated path to the property.
* @param string $separator
* @return array|string|null
* @internal
*/
protected function getNested($path = null, $separator = '.')
{
if (!$path) {
return $this->nested;
}
$parts = explode($separator, $path);
$item = array_pop($parts);
$nested = $this->nested;
foreach ($parts as $part) {
if (!isset($nested[$part])) {
$part = '*';
if (!isset($nested[$part])) {
return [];
}
}
$nested = $nested[$part];
}
return isset($nested[$item]) ? $nested[$item] : (isset($nested['*']) ? $nested['*'] : null);
}
/**
* @param array $nested
* @return array
*/
protected function buildDefaults(array $nested)
{
$defaults = [];
foreach ($nested as $key => $value) {
if ($key === '*') {
// TODO: Add support for adding defaults to collections.
continue;
}
if (is_array($value)) {
// Recursively fetch the items.
$list = $this->buildDefaults($value);
// Only return defaults if there are any.
if (!empty($list)) {
$defaults[$key] = $list;
}
} else {
// We hit a field; get default from it if it exists.
$item = $this->get($value);
// Only return default value if it exists.
if (isset($item['default'])) {
$defaults[$key] = $item['default'];
}
}
}
return $defaults;
}
/**
* @param array $data1
* @param array $data2
* @param array $rules
* @return array
* @internal
*/
protected function mergeArrays(array $data1, array $data2, array $rules)
{
foreach ($data2 as $key => $field) {
$val = isset($rules[$key]) ? $rules[$key] : null;
$rule = is_string($val) ? $this->items[$val] : null;
if (!empty($rule['type']) && $rule['type'][0] === '_'
|| (array_key_exists($key, $data1) && is_array($data1[$key]) && is_array($field) && is_array($val) && !isset($val['*']))
) {
// Array has been defined in blueprints and is not a collection of items.
$data1[$key] = $this->mergeArrays($data1[$key], $field, $val);
} else {
// Otherwise just take value from the data2.
$data1[$key] = $field;
}
}
return $data1;
}
/**
* Gets all field definitions from the blueprints.
*
* @param array $fields Fields to parse.
* @param array $params Property parameters.
* @param string $prefix Property prefix.
* @param string $parent Parent property.
* @param bool $merge Merge fields instead replacing them.
* @param array $formPath
*/
protected function parseFormFields(array $fields, array $params, $prefix = '', $parent = '', $merge = false, array $formPath = [])
{
// Go though all the fields in current level.
foreach ($fields as $key => $field) {
$key = $this->getFieldKey($key, $prefix, $parent);
$newPath = array_merge($formPath, [$key]);
$properties = array_diff_key($field, $this->ignoreFormKeys) + $params;
$properties['name'] = $key;
// Set default properties for the field type.
$type = isset($properties['type']) ? $properties['type'] : '';
if (isset($this->types[$type])) {
$properties += $this->types[$type];
}
// Merge properties with existing ones.
if ($merge && isset($this->items[$key])) {
$properties += $this->items[$key];
}
$isInputField = !isset($properties['input@']) || $properties['input@'];
if (!$isInputField) {
// Remove property if it exists.
if (isset($this->items[$key])) {
$this->removeProperty($key);
}
} elseif (!isset($this->items[$key])) {
// Add missing property.
$this->addProperty($key);
}
if (isset($field['fields'])) {
// Recursively get all the nested fields.
$isArray = !empty($properties['array']);
$newParams = array_intersect_key($properties, $this->filter);
$this->parseFormFields($field['fields'], $newParams, $prefix, $key . ($isArray ? '.*': ''), $merge, $newPath);
} else {
if (!isset($this->items[$key])) {
// Add parent rules.
$path = explode('.', $key);
array_pop($path);
$parent = '';
foreach ($path as $part) {
$parent .= ($parent ? '.' : '') . $part;
if (!isset($this->items[$parent])) {
$this->items[$parent] = ['type' => '_parent', 'name' => $parent, 'form_field' => false];
}
}
}
if ($isInputField) {
$this->parseProperties($key, $properties);
}
}
if ($isInputField) {
$this->items[$key] = $properties;
}
}
}
protected function getFieldKey($key, $prefix, $parent)
{
// Set name from the array key.
if ($key && $key[0] == '.') {
return ($parent ?: rtrim($prefix, '.')) . $key;
}
return $prefix . $key;
}
protected function parseProperties($key, array &$properties)
{
$key = ltrim($key, '.');
if (!empty($properties['data'])) {
$this->dynamic[$key] = $properties['data'];
}
foreach ($properties as $name => $value) {
if (!empty($name) && ($name[0] === '@' || $name[strlen($name) - 1] === '@')) {
$list = explode('-', trim($name, '@'), 2);
$action = array_shift($list);
$property = array_shift($list);
$this->dynamic[$key][$property] = ['action' => $action, 'params' => $value];
}
}
// Initialize predefined validation rule.
if (isset($properties['validate']['rule'])) {
$properties['validate'] += $this->getRule($properties['validate']['rule']);
}
}
/**
* Add property to the definition.
*
* @param string $path Comma separated path to the property.
* @internal
*/
protected function addProperty($path)
{
$parts = explode('.', $path);
$item = array_pop($parts);
$nested = &$this->nested;
foreach ($parts as $part) {
if (!isset($nested[$part]) || !is_array($nested[$part])) {
$nested[$part] = [];
}
$nested = &$nested[$part];
}
if (!isset($nested[$item])) {
$nested[$item] = $path;
}
}
/**
* Remove property to the definition.
*
* @param string $path Comma separated path to the property.
* @internal
*/
protected function removeProperty($path)
{
$parts = explode('.', $path);
$item = array_pop($parts);
$nested = &$this->nested;
foreach ($parts as $part) {
if (!isset($nested[$part]) || !is_array($nested[$part])) {
return;
}
$nested = &$nested[$part];
}
if (isset($nested[$item])) {
unset($nested[$item]);
}
}
/**
* @param $rule
* @return array
* @internal
*/
protected function getRule($rule)
{
if (isset($this->rules[$rule]) && is_array($this->rules[$rule])) {
return $this->rules[$rule];
}
return [];
}
/**
* @param array $data
* @param array $rules
* @param string $prefix
* @return array
* @internal
*/
protected function extraArray(array $data, array $rules, $prefix)
{
$array = [];
foreach ($data as $key => $field) {
$val = isset($rules[$key]) ? $rules[$key] : (isset($rules['*']) ? $rules['*'] : null);
$rule = is_string($val) ? $this->items[$val] : null;
if ($rule || isset($val['*'])) {
// Item has been defined in blueprints.
} elseif (is_array($field) && is_array($val)) {
// Array has been defined in blueprints.
$array += $this->ExtraArray($field, $val, $prefix . $key . '.');
} else {
// Undefined/extra item.
$array[$prefix.$key] = $field;
}
}
return $array;
}
/**
* @param array $field
* @param string $property
* @param array $call
*/
protected function dynamicData(array &$field, $property, array $call)
{
$params = $call['params'];
if (is_array($params)) {
$function = array_shift($params);
} else {
$function = $params;
$params = [];
}
$list = preg_split('/::/', $function, 2);
$f = array_pop($list);
$o = array_pop($list);
if (!$o) {
if (function_exists($f)) {
$data = call_user_func_array($f, $params);
}
} else {
if (method_exists($o, $f)) {
$data = call_user_func_array(array($o, $f), $params);
}
}
// If function returns a value,
if (isset($data)) {
if (isset($field[$property]) && is_array($field[$property]) && is_array($data)) {
// Combine field and @data-field together.
$field[$property] += $data;
} else {
// Or create/replace field with @data-field.
$field[$property] = $data;
}
}
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace RocketTheme\Toolbox\Blueprints;
/**
* Deprecated class, use BlueprintSchema instead.
*
* @package RocketTheme\Toolbox\Blueprints
* @author RocketTheme
* @license MIT
* @deprecated
*/
class Blueprints extends BlueprintSchema {}

View File

@ -0,0 +1,96 @@
<?php
use RocketTheme\Toolbox\Blueprints\BlueprintForm;
use RocketTheme\Toolbox\File\YamlFile;
require_once 'helper.php';
class BlueprintsBlueprintFormTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider dataProvider
*/
public function testLoad($test)
{
$blueprint = new Blueprint($test);
$blueprint->setOverrides(
['extends' => ['extends', 'basic']]
);
$blueprint->load();
// Save test results if they do not exist (data needs to be verified by human!)
/*
$resultFile = YamlFile::instance(__DIR__ . '/data/form/items/' . $test . '.yaml');
if (!$resultFile->exists()) {
$resultFile->content($blueprint->toArray());
$resultFile->save();
}
*/
// Test 1: Loaded form.
$this->assertEquals($this->loadYaml($test, 'form/items'), $blueprint->toArray());
}
public function dataProvider()
{
return [
['empty'],
['basic'],
['import'],
['extends']
];
}
protected function loadYaml($test, $type = 'blueprint')
{
$file = YamlFile::instance(__DIR__ . "/data/{$type}/{$test}.yaml");
$content = $file->content();
$file->free();
return $content;
}
}
class Blueprint extends BlueprintForm
{
/**
* @param string $filename
* @return string
*/
protected function loadFile($filename)
{
$file = YamlFile::instance(__DIR__ . "/data/blueprint/{$filename}.yaml");
$content = $file->content();
$file->free();
return $content;
}
/**
* @param string|array $path
* @param string $context
* @return array
*/
protected function getFiles($path, $context = null)
{
if (is_string($path)) {
// Resolve filename.
if (isset($this->overrides[$path])) {
$path = $this->overrides[$path];
} else {
if ($context === null) {
$context = $this->context;
}
if ($context && $context[strlen($context)-1] !== '/') {
$context .= '/';
}
$path = $context . $path;
}
}
$files = (array) $path;
return $files;
}
}

View File

@ -0,0 +1,90 @@
<?php
use RocketTheme\Toolbox\Blueprints\BlueprintSchema;
use RocketTheme\Toolbox\File\YamlFile;
require_once 'helper.php';
class BlueprintsBlueprintSchemaTest extends PHPUnit_Framework_TestCase
{
public function testCreation()
{
$blueprints = new BlueprintSchema;
$this->assertEquals(
[
'items' => [],
'rules' => [],
'nested' => [],
'dynamic' => [],
'filter' => ['validation' => true]
],
$blueprints->getState());
$this->assertEquals([], $blueprints->getDefaults());
}
/**
* @dataProvider dataProvider
*/
public function testLoad($test)
{
$input = $this->loadYaml($test);
$blueprint = new BlueprintSchema;
$blueprint->embed('', $input);
// Save test results if they do not exist (data needs to be verified by human!)
/*
$resultFile = YamlFile::instance(__DIR__ . '/data/schema/state/' . $test . '.yaml');
if (!$resultFile->exists()) {
$resultFile->content($blueprint->getState());
$resultFile->save();
}
*/
// Test 1: Internal state.
$this->assertEquals($this->loadYaml($test, 'schema/state'), $blueprint->getState());
// Save test results if they do not exist (data needs to be verified by human!)
$resultFile = YamlFile::instance(__DIR__ . '/data/schema/init/' . $test . '.yaml');
if (!$resultFile->exists()) {
$resultFile->content($blueprint->init()->getState());
$resultFile->save();
}
// Test 2: Initialize blueprint.
$this->assertEquals($this->loadYaml($test, 'schema/init'), $blueprint->init()->getState());
// Test 3: Default values.
$this->assertEquals($this->loadYaml($test, 'schema/defaults'), $blueprint->getDefaults());
// Test 4: Extra values.
$this->assertEquals($this->loadYaml($test, 'schema/extra'), $blueprint->extra($this->loadYaml($test, 'input')));
// Test 5: Merge data.
$this->assertEquals(
$this->loadYaml($test, 'schema/merge'),
$blueprint->mergeData($blueprint->getDefaults(), $this->loadYaml($test, 'input'))
);
}
public function dataProvider()
{
return [
['empty'],
['basic'],
];
}
protected function loadYaml($test, $type = 'blueprint')
{
$file = YamlFile::instance(__DIR__ . "/data/{$type}/{$test}.yaml");
$content = $file->content();
$file->free();
return $content;
}
}

View File

@ -0,0 +1,41 @@
rules:
name:
pattern: "[A-Z][a-z]+"
min: 3
max: 15
form:
validation: loose
fields:
text:
type: text
label: Text
placeholder: 'Enter your text'
enabled:
type: select
label: Enabled
default: true
data-options@: blueprint_data_option_test
user.name:
type: name
label: Name
default: John
validate:
type: name
user.country:
type: select
label: Enabled
default: fi
data-options@:
- blueprint_data_option_test
- { us: 'United States', fi: 'Finland', po: 'Poland' }
- true
undefined:
type: select
label: Undefined
data-options@: undefined_function

View File

@ -0,0 +1,17 @@
extends@: parent@
form:
fields:
enabled:
default: false
text:
default: My text
ordering@: enabled
user.name:
default: Joe
unset-validate@: true
undefined:
unset@: true

View File

@ -0,0 +1,4 @@
form:
fields:
address:
import@: partials/address

View File

@ -0,0 +1,17 @@
form:
fields:
user.address:
type: text
label: Address
user.zip:
type: text
label: ZIP code
user.city:
type: text
label: City
user.country:
type: text
label: Country

View File

@ -0,0 +1,40 @@
rules:
name:
pattern: '[A-Z][a-z]+'
min: 3
max: 15
form:
validation: loose
fields:
text:
type: text
label: Text
placeholder: 'Enter your text'
name: text
enabled:
type: select
label: Enabled
default: true
data-options@: blueprint_data_option_test
name: enabled
user.name:
type: name
label: Name
default: John
validate:
type: name
name: user.name
user.country:
type: select
label: Enabled
default: fi
data-options@:
- blueprint_data_option_test
- { us: 'United States', fi: Finland, po: Poland }
- true
name: user.country
undefined:
type: select
label: Undefined
data-options@: undefined_function
name: undefined

View File

@ -0,0 +1,34 @@
rules:
name:
pattern: '[A-Z][a-z]+'
min: 3
max: 15
form:
validation: loose
fields:
enabled:
type: select
label: Enabled
default: false
data-options@: blueprint_data_option_test
name: enabled
text:
type: text
label: Text
placeholder: 'Enter your text'
default: 'My text'
name: text
user.name:
type: name
label: Name
default: Joe
name: user.name
user.country:
type: select
label: Enabled
default: fi
data-options@:
- blueprint_data_option_test
- { us: 'United States', fi: Finland, po: Poland }
- true
name: user.country

View File

@ -0,0 +1,8 @@
form:
fields:
address:
fields:
user.address: { type: text, label: Address, name: user.address }
user.zip: { type: text, label: 'ZIP code', name: user.zip }
user.city: { type: text, label: City, name: user.city }
user.country: { type: text, label: Country, name: user.country }

View File

@ -0,0 +1,12 @@
text: Testing...
user:
name: Igor
extra: data...
some:
random: false
variables:
- true
- false
just: for
fun:

View File

@ -0,0 +1,7 @@
some:
random: false
variables:
- true
- false
just: for
fun:

View File

@ -0,0 +1,4 @@
enabled: true
user:
name: John
country: fi

View File

@ -0,0 +1,8 @@
some:
random: false
variables:
- true
- false
just: for
fun:
user.extra: data...

View File

@ -0,0 +1,7 @@
some:
random: false
variables:
- true
- false
just: for
fun:

View File

@ -0,0 +1,88 @@
items:
'':
form:
validation: loose
type: _root
form_field: false
text:
type: text
label: Text
placeholder: 'Enter your text'
validation: loose
name: text
enabled:
type: select
label: Enabled
default: true
data-options@: blueprint_data_option_test
validation: loose
name: enabled
options:
'yes': 'Yes'
'no': 'No'
user:
type: _parent
name: user
form_field: false
user.name:
type: name
label: Name
default: John
validate:
type: name
validation: loose
name: user.name
user.country:
type: select
label: Enabled
default: fi
data-options@:
- blueprint_data_option_test
-
us: 'United States'
fi: Finland
po: Poland
- true
validation: loose
name: user.country
options:
fi: Finland
po: Poland
us: 'United States'
undefined:
type: select
label: Undefined
data-options@: undefined_function
validation: loose
name: undefined
rules:
name:
pattern: '[A-Z][a-z]+'
min: 3
max: 15
nested:
'': ''
text: text
enabled: enabled
user:
name: user.name
country: user.country
undefined: undefined
dynamic:
enabled:
options:
action: data
params: blueprint_data_option_test
user.country:
options:
action: data
params:
- blueprint_data_option_test
- { us: 'United States', fi: Finland, po: Poland }
- true
undefined:
options:
action: data
params: undefined_function
filter:
validation: true

View File

@ -0,0 +1,11 @@
items:
'':
form: { }
type: _root
form_field: false
rules: { }
nested:
'': ''
dynamic: { }
filter:
validation: true

View File

@ -0,0 +1,14 @@
text: Testing...
user:
name: Igor
country: fi
extra: data...
some:
random: false
variables:
- true
- false
just: for
fun:
enabled: true

View File

@ -0,0 +1,7 @@
some:
random: false
variables:
- true
- false
just: for
fun:

View File

@ -0,0 +1,81 @@
items:
'':
form:
validation: loose
type: _root
form_field: false
text:
type: text
label: Text
placeholder: 'Enter your text'
validation: loose
name: text
enabled:
type: select
label: Enabled
default: true
data-options@: blueprint_data_option_test
validation: loose
name: enabled
user:
type: _parent
name: user
form_field: false
user.name:
type: name
label: Name
default: John
validate:
type: name
validation: loose
name: user.name
user.country:
type: select
label: Enabled
default: fi
data-options@:
- blueprint_data_option_test
-
us: 'United States'
fi: Finland
po: Poland
- true
validation: loose
name: user.country
undefined:
type: select
label: Undefined
data-options@: undefined_function
validation: loose
name: undefined
rules:
name:
pattern: '[A-Z][a-z]+'
min: 3
max: 15
nested:
'': ''
text: text
enabled: enabled
user:
name: user.name
country: user.country
undefined: undefined
dynamic:
enabled:
options:
action: data
params: blueprint_data_option_test
user.country:
options:
action: data
params:
- blueprint_data_option_test
- { us: 'United States', fi: Finland, po: Poland }
- true
undefined:
options:
action: data
params: undefined_function
filter:
validation: true

View File

@ -0,0 +1,11 @@
items:
'':
form: { }
type: _root
form_field: false
rules: { }
nested:
'': ''
dynamic: { }
filter:
validation: true

View File

@ -0,0 +1,9 @@
<?php
function blueprint_data_option_test(array $param = null, $sort = false)
{
if ($sort) {
asort($param);
}
return $param ?: ['yes' => 'Yes', 'no' => 'No'];
}