|
@@ -0,0 +1,46 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+namespace internship\controller\process;
|
|
|
|
|
+
|
|
|
|
|
+use n2n\util\type\attrs\DataMap;
|
|
|
|
|
+use n2n\web\http\controller\impl\ControllingUtils;
|
|
|
|
|
+use n2n\web\http\controller\impl\HttpData;
|
|
|
|
|
+use n2n\web\http\controller\impl\ExecResult;
|
|
|
|
|
+use n2n\bind\build\impl\Bind;
|
|
|
|
|
+use n2n\bind\mapper\impl\Mappers;
|
|
|
|
|
+use n2n\validation\validator\impl\Validators;
|
|
|
|
|
+use internship\bo\Article;
|
|
|
|
|
+use internship\bo\CategoryName;
|
|
|
|
|
+
|
|
|
|
|
+class ArticleInProcess {
|
|
|
|
|
+ private DataMap $dataMap;
|
|
|
|
|
+ public function __construct(private ControllingUtils $cu) {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @param HttpData $httpData
|
|
|
|
|
+ * @return ExecResult
|
|
|
|
|
+ */
|
|
|
|
|
+ function in(HttpData $httpData, ?Article $article = null): ExecResult {
|
|
|
|
|
+ $this->dataMap = new DataMap();
|
|
|
|
|
+
|
|
|
|
|
+ $mustExist = $article === null;
|
|
|
|
|
+ return $this->cu->exec(Bind::attrs($httpData)->toAttrs($this->dataMap)
|
|
|
|
|
+ ->dynProp('categoryName', $mustExist, Mappers::cleanString(true), Validators::enum(
|
|
|
|
|
+ ['international', 'national', 'sport']))
|
|
|
|
|
+ ->dynProp('text', $mustExist, Mappers::cleanString(true, maxlength: 9999) )
|
|
|
|
|
+ ->dynProp('title', $mustExist, Mappers::cleanString(true, minlength: 3, maxlength: 255)));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function out(Article $article): Article {
|
|
|
|
|
+ $this->cu->exec(Bind::attrs($this->dataMap)->toObj($article)
|
|
|
|
|
+ ->optProp('categoryName',
|
|
|
|
|
+ Validators::enum(['international', 'national', 'sport']),
|
|
|
|
|
+ Mappers::valueClosure(function ($categoryNameStr) {
|
|
|
|
|
+ return CategoryName::tryFrom($categoryNameStr);
|
|
|
|
|
+ }))
|
|
|
|
|
+ ->optProp('text', Mappers::cleanString(true, maxlength: 9999) )
|
|
|
|
|
+ ->optProp('title', Mappers::cleanString(true, minlength: 3, maxlength: 255)));
|
|
|
|
|
+ return $article;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|