Skip to content

Commit

Permalink
Add support for 'html' fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
daedeloth committed Aug 18, 2022
1 parent d598bef commit 79f4a50
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
"type": "library",
"require": {
"php": ">=7.1",
"catlabinteractive/requirements": "^1.0.6",
"catlabinteractive/requirements": "^1.0.7",
"ext-mbstring": "*"
},
"suggest": {
"ezyang/htmlpurifier": "^4.14"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"catlabinteractive/cursor-pagination" : "^1.0.8"
Expand Down
44 changes: 43 additions & 1 deletion src/CharonConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use CatLab\Charon\Models\Singleton;
use CatLab\Charon\Transformers\ArrayTransformer;

// Optional library
use HTMLPurifier;
use HTMLPurifier_Config;

/**
* Class CharonConfig
* @package CatLab\Charon
Expand All @@ -16,11 +20,49 @@ class CharonConfig extends Singleton
*/
public $defaultArrayTransformer = ArrayTransformer::class;

/**
* @var HTMLPurifier
*/
private $htmlPurifier;

/**
* @var callable
*/
private $htmlPurifierFactory;

/**
* @return string
*/
public function getDefaultArrayTransformer()
{
return $this->defaultArrayTransformer;
}
}

/**
* @param HTMLPurifier $purifier
* @return $this
*/
public function setHtmlPurifierFactory(callable $factory)
{
$this->htmlPurifierFactory = $factory;
return $this;
}

/**
* @return HTMLPurifier
*/
public function getHtmlPurifier()
{
if (!isset($this->htmlPurifier)) {

if (isset($this->htmlPurifierFactory)) {
$this->htmlPurifier = call_user_func($this->htmlPurifierFactory);
} else {
$config = HTMLPurifier_Config::createDefault();
$this->htmlPurifier = new HTMLPurifier($config);
}
}

return $this->htmlPurifier;
}
}
13 changes: 13 additions & 0 deletions src/Models/Properties/Base/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use CatLab\Charon\Models\Properties\ResourceField;
use CatLab\Charon\Models\ResourceDefinition;
use CatLab\Charon\Transformers\DateTransformer;
use CatLab\Charon\Transformers\HtmlTransformer;
use CatLab\Charon\Transformers\ScalarTransformer;
use CatLab\Charon\Validation\HtmlValidator;
use CatLab\Requirements\Enums\PropertyType;
use CatLab\Requirements\Exceptions\PropertyValidationException;
use CatLab\Requirements\Interfaces\Property;
Expand Down Expand Up @@ -489,6 +491,17 @@ public function datetime($transformer = DateTransformer::class)
return $this;
}

/**
* @param $transformer
* @return $this
*/
public function html($transformer = HtmlTransformer::class)
{
$this->type = PropertyType::HTML;
$this->transformer($transformer);
return $this;
}

/**
* @return array
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Singleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected function __construct()
}

/**
* @return mixed
* @return static
*/
final public static function instance()
{
Expand Down
51 changes: 51 additions & 0 deletions src/Transformers/HtmlTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace CatLab\Charon\Transformers;

use CatLab\Charon\CharonConfig;
use CatLab\Charon\Exceptions\NotImplementedException;
use CatLab\Charon\Interfaces\Context;
use CatLab\Charon\Interfaces\Transformer;

/**
*
*/
class HtmlTransformer implements Transformer
{
/**
* @param $value
* @param Context $context
* @return mixed|void
*/
public function toResourceValue($value, Context $context)
{
return $value;
}

/**
* @param $value
* @param Context $context
* @return mixed|void
*/
public function toEntityValue($value, Context $context)
{
// Is this plain text input?
if (strip_tags($value) === $value) {
// Wrap around a paragraph
$value = '<p>' . nl2br($value) . '</p>';
}

$purifier = CharonConfig::instance()->getHtmlPurifier();
return $purifier->purify($value);
}

/**
* @param $value
* @return mixed|void
* @throws \CatLab\Charon\Exceptions\CharonException
*/
public function toParameterValue($value)
{
throw NotImplementedException::makeTranslatable('HTML parameters are not supported');
}
}
1 change: 1 addition & 0 deletions src/Transformers/ScalarTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ protected function castToScalar($value)
return floatval($value);

case PropertyType::STRING:
case PropertyType::HTML:
return strval($value);

default:
Expand Down

0 comments on commit 79f4a50

Please sign in to comment.