Created
March 19, 2018 13:19
-
-
Save rquast/17fff6373886de9b7a23322bb1b1c7f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Cavy\Model\DTO; | |
use Cavy\Lib\Exceptions\ValidationException; | |
use Cavy\Model\DAO\DocumentsModel; | |
use Respect\Validation\Validator as v; | |
class Document extends ModifiedBase implements Indexable { | |
/** | |
* @var string $id Topic identifier | |
*/ | |
protected $id; | |
/** | |
* @var string $user_id OIDC User ID | |
*/ | |
protected $user_id; | |
/** | |
* @var string $content Document content | |
*/ | |
protected $content; | |
/** | |
* Document constructor. | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* @return string | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* @param string $id | |
* @throws \Exception | |
*/ | |
public function setId($id) | |
{ | |
$id = trim($id); | |
Topic::validateId($id, 'invalid_document:id'); | |
$this->id = $id; | |
} | |
/** | |
* @return string | |
*/ | |
public function getUserId() | |
{ | |
return $this->user_id; | |
} | |
/** | |
* @param string $user_id | |
* @throws ValidationException | |
*/ | |
public function setUserId($user_id) | |
{ | |
if ($user_id == null || $user_id == '' || is_null($user_id)) { | |
$this->user_id = null; | |
} else { | |
$user_id = trim($user_id); | |
User::validateId($user_id, 'invalid_document:user_id'); | |
$this->user_id = $user_id; | |
} | |
} | |
/** | |
* Validate content | |
* | |
* @param $content | |
* @param $keyPrefix | |
* @throws ValidationException | |
*/ | |
public static function validateContent($content, $keyPrefix = 'invalid_document:content') { | |
if (!v::length(10, MAX_PROSE_DOCUMENT_SIZE)->validate($content)) { | |
throw new ValidationException([$keyPrefix . ':length' => 'Must be between 10 and ' . MAX_PROSE_DOCUMENT_SIZE .' characters long.']); | |
} | |
$prose = new Prose(); | |
$prose->hydrate($content, $keyPrefix); | |
} | |
/** | |
* @return string | |
*/ | |
public function getContent() | |
{ | |
return $this->content; | |
} | |
/** | |
* @param string $content | |
* @throws ValidationException | |
*/ | |
public function setContent($content) | |
{ | |
$this->validateContent($content); | |
$this->content = $content; | |
} | |
/** | |
* @param mixed $params Mapping parameters | |
* @return void | |
*/ | |
public function toMapping(&$params) { | |
$params['type'] = DocumentsModel::TABLE_NAME; | |
$params['body'] = [ | |
DocumentsModel::TABLE_NAME => [ | |
'_source' => [ | |
'enabled' => true | |
], | |
'properties' => [ | |
'subdomain' => [ | |
'type' => 'string', | |
'analyzer' => 'standard' | |
], | |
'h1' => [ | |
'type' => 'string', | |
'analyzer' => 'standard' | |
], | |
'h2' => [ | |
'type' => 'string', | |
'analyzer' => 'standard' | |
], | |
'h3' => [ | |
'type' => 'string', | |
'analyzer' => 'standard' | |
], | |
'h4' => [ | |
'type' => 'string', | |
'analyzer' => 'standard' | |
], | |
'h5' => [ | |
'type' => 'string', | |
'analyzer' => 'standard' | |
], | |
'h6' => [ | |
'type' => 'string', | |
'analyzer' => 'standard' | |
], | |
'paragraphs' => [ | |
'type' => 'string', | |
'analyzer' => 'standard' | |
], | |
'created' => [ | |
'type' => 'date' | |
], | |
'modified' => [ | |
'type' => 'date' | |
] | |
] | |
] | |
]; | |
} | |
/** | |
* @param string $baseIndex The base index | |
* @param string $subdomain Subdomain | |
* @return array Indexing parameters | |
*/ | |
public function toIndex($baseIndex, $subdomain, $isUpdate = false) { | |
$doc = [ | |
'subdomain' => $subdomain | |
]; | |
$params = [ | |
'index' => $baseIndex, | |
'type' => DocumentsModel::TABLE_NAME, | |
'id' => $this->id, | |
'routing' => $subdomain | |
]; | |
if ($this->content !== null) { | |
$this->setContentParts($doc); | |
} | |
if ($isUpdate) { | |
$params['body'] = []; | |
$params['body']['doc'] = $doc; | |
} else { | |
$params['body'] = $doc; | |
} | |
return $params; | |
} | |
public function setContentParts(&$params) { | |
$prose = new Prose(); | |
$prose->setStripTags(true); | |
try { | |
$prose->hydrate($this->content); | |
foreach ($prose->headings as $key => $value) { | |
$params['h' . $key] = $value; | |
} | |
$params['paragraphs'] = $prose->paragraphs; | |
} catch (\Exception $ex) { | |
echo $ex->getMessage() . "\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment