Skip to content

Commit

Permalink
Merge branch 'release/2.x' of github.com:dmstr/yii2-pages-module into…
Browse files Browse the repository at this point in the history
… release/2.x
  • Loading branch information
schmunk42 committed Jan 30, 2019
2 parents c4a0630 + e9c38ac commit f98c060
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"dmstr/yii2-backend-module": "^1.0.0-beta12",
"bedezign/yii2-audit": "^1.1",
"mikehaertl/php-shellcommand": "^1.2.4",
"pheme/yii2-settings": "^0.5.0"
"pheme/yii2-settings": "^0.5.0",
"justinrainbow/json-schema": "^5.2.0"
},
"require-dev": {
"codeception/codeception": "^2.2"
Expand Down
31 changes: 31 additions & 0 deletions models/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use yii\helpers\Inflector;
use yii\helpers\Json;
use yii\helpers\Url;
use JsonSchema\Validator;

/**
* Class Tree
Expand Down Expand Up @@ -54,6 +55,36 @@
*/
class Tree extends BaseTree
{

/**
* @inheritdoc
*/
public function rules()
{
return ArrayHelper::merge(
parent::rules(),
[
[
self::ATTR_REQUEST_PARAMS,
function ($attribute, $params) {

$validator = new Validator();

$obj = Json::decode($this->requestParamsSchema, false);
$data = Json::decode($this->{$attribute}, false);
$validator->check($data, $obj);
if ($validator->getErrors()) {
foreach ($validator->getErrors() as $error) {
$this->addError($error['property'], "{$error['property']}: {$error['message']}");
}
}

},
],
]
);
}

/**
* @inheritdoc
*/
Expand Down
50 changes: 29 additions & 21 deletions traits/RequestParamActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
*
* By default it will generate a text field per action parameter.
*
* For customization you can create a public method for each individual action parameter by adding a method which name have to follow this schema:
* For customization you can create a public method for each individual action parameter by adding a method which name
* have to follow this schema:
*
* `camelizedActionId` + ActionParam + `ParameterName`
*
Expand All @@ -35,34 +36,38 @@
*
* Example: detailActionParamProductId
*
* This method must return a array (key-value pairs), where the keys should refer to the actual value and the value will be the label
* This method must return a array (key-value pairs), where the keys should refer to the actual value and the value will
* be the label
*
* Example:
*
* return ArrayHelper::map(Product::find()->all(),'id','name');
*
*
* Hint:
* Hints:
*
* If the method as described above returns false, then this property will be ignored.
* - If the method as described above returns false, then this property will be ignored.
*
* If the method as described above returns true, then this property will be displayed. This functionality can be used to maniulate e.g. title or description (see class propertie `$allowedProperties`)
* - If the method as described above returns true, then this property will be displayed. This functionality can be used
* to manipulate e.g. title or description (see class property `$allowedProperties`)
*
* You can use php doc block to add option to properties:
* - You can use php doc block to add options to properties:
*
* Example:
*
* /**
* * @editor title My Title
* *\/
* public function detailActionParamProductName() {
* return true;
* }
*
* This will generate a input with defined title for an *existing* parameter
* Example:
*
* /**
* * @editor title My Title
* *\/
* public function detailActionParamProductName() {
* return true;
* }
*
* This will generate a input with defined title for an *existing* parameter
*
* - If property is NOT optional, it will be set as required in json schema.
* However, since this only implies that the property must be set in the data, but not that a value must also be set,
* a validation rule should be defined using notations (see above). For properties of type 'string' a minLength: 1
* option is set as fallback.
*
* @package dmstr\modules\pages\traits
* @author Elias Luhr <[email protected]>
Expand Down Expand Up @@ -111,13 +116,10 @@ private function generateJson($parameters, $actionId)

$properties = [];
$requiredFields = [];
$debug = [];
foreach ($parameters as $parameter) {
// get name
$parameterName = $parameter->name;

$debug[] = $parameterName;

// nameActionParamId
$methodName = $actionId . 'ActionParam' . ucfirst($parameterName);
// use data from method if it exist.
Expand All @@ -137,9 +139,9 @@ private function generateJson($parameters, $actionId)
$additionalData = [];
if ($docs !== false) {
// matches e.g.
// @descrition My custom description
// @editor description My custom description
// in php doc blocks
preg_match_all('/@editor (\$[a-z]+) (.*)\n/', $docs, $matches);
preg_match_all('/@editor[\s]+([a-zA-Z-_]+)[\s]+(.*)\n/', $docs, $matches);
if (isset($matches[1], $matches[2]) && \count($matches[1]) === \count($matches[2])) {
$matchIndex = 0;
foreach ($matches[1] as $propertyName) {
Expand All @@ -164,6 +166,11 @@ private function generateJson($parameters, $actionId)
// add to required if not is optional
if (!$parameter->isOptional()) {
$requiredFields[] = $parameterName;
// TODO: how to check other types?
if (($additionalData['type'] === 'string') && (!isset($additionalData['minLength']))) {
$extraProperties[] = '"minLength": 1';
}

}

foreach ($additionalData as $name => $value) {
Expand Down Expand Up @@ -204,6 +211,7 @@ private function generateJson($parameters, $actionId)

if (!$parameter->isOptional()) {
$requiredFields[] = $parameterName;
$extraProperties[] = '"minLength": 1';
}

// generate default if nothing else is defined
Expand Down

0 comments on commit f98c060

Please sign in to comment.