Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PropertyString, cleanHtml helper and escapeHtmlExt helper. #4004

Draft
wants to merge 9 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"composer/package-versions-deprecated": "1.11.99.5",
"composer/semver": "3.4.2",
"endroid/qr-code": "5.0.9",
"ezyang/htmlpurifier": "4.17.0",
"guzzlehttp/guzzle": "7.9.2",
"jaybizzle/crawler-detect": "^1.2",
"laminas/laminas-cache": "3.12.2",
Expand Down
63 changes: 62 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions module/VuFind/src/VuFind/Content/Summaries/Demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

namespace VuFind\Content\Summaries;

use VuFind\String\PropertyString;

/**
* Demo (fake data) summaries content loader.
*
Expand Down Expand Up @@ -56,6 +58,8 @@ public function loadByIsbn($key, \VuFindCode\ISBN $isbnObj)
return [
'Demo summary key: ' . $key,
'Demo summary ISBN: ' . $isbnObj->get13(),
(new PropertyString('Demo non-HTML summary'))
->setHtml('<strong>Demo HTML Summary:</strong><ul><li>Item 1</li><li>Item 2</li></ul>'),
maccabeelevine marked this conversation as resolved.
Show resolved Hide resolved
];
}
}
25 changes: 22 additions & 3 deletions module/VuFind/src/VuFind/RecordDriver/DefaultRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -1271,9 +1271,9 @@ public function getSystemDetails()
public function getSummary()
{
// We need to return an array, so if we have a description, turn it into an
// array (it should be a flat string according to the default schema, but we
// might as well support the array case just to be on the safe side:
return (array)($this->fields['description'] ?? []);
// array (it is a flat string in the default Solr schema, but we also
// support multivalued fields for other backends):
return $this->getFieldAsArray('description');
}

/**
Expand Down Expand Up @@ -1816,4 +1816,23 @@ public function getCoordinateLabels()
{
return (array)($this->fields['long_lat_label'] ?? []);
}

/**
* Get a field as an array
*
* @param string $field Field
*
* @return array
*/
protected function getFieldAsArray(string $field): array
{
// Make sure to return only non-empty values:
$value = $this->fields['description'] ?? '';
if ('' === $value) {
return [];
}
// Avoid casting since description can be a PropertyString too (and casting would return an array of object
// properties):
return is_array($value) ? $value : [$value];
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
}
}
187 changes: 187 additions & 0 deletions module/VuFind/src/VuFind/String/PropertyString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

/**
* Class for a string with additional properties.
*
* PHP version 8
*
* Copyright (C) The National Library of Finland 2024.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package String
* @author Ere Maijala <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/

namespace VuFind\String;

use function array_key_exists;

/**
* Class for a string with additional properties.
*
* @category VuFind
* @package String
* @author Ere Maijala <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
class PropertyString implements PropertyStringInterface
{
/**
* Constructor
*
* @param string $string String value
* @param array $properties Associative array of any additional properties. Use a custom prefix for locally
* defined properties. Double underscore is a reserved prefix, and currently the following keys are defined:
* __ids Identifiers (e.g. subject URIs)
* __html HTML presentation
*/
public function __construct(protected string $string, protected array $properties = [])
{
}

/**
* Set string value
*
* @param string $str String value
*
* @return static
*/
public function setString(string $str): static
{
$this->string = $str;
return $this;
}

/**
* Get string value
*
* @return string
*/
public function getString(): string
{
return $this->string;
}

/**
* Set HTML string
*
* @param string $html HTML
*
* @return static
*/
public function setHtml(string $html): static
{
$this['__html'] = $html;
return $this;
}

/**
* Get HTML string
*
* Note: This could contain anything and must be sanitized for display
*
* @return ?string
*/
public function getHtml(): ?string
{
return $this['__html'];
}

/**
* Set identifiers
*
* @param array $ids Identifiers
*
* @return static
*/
public function setIds(array $ids): static
{
$this['__ids'] = $ids;
return $this;
}

/**
* Get identifiers
*
* @return ?array
*/
public function getIds(): ?array
{
return $this['__ids'];
}

/**
* Check if offset exists
*
* @param mixed $offset Offset
*
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return array_key_exists($offset, $this->properties);
}

/**
* Return value of offset
*
* @param mixed $offset Offset
*
* @return mixed
*/
public function offsetGet(mixed $offset): mixed
{
return $this->properties[$offset] ?? null;
}

/**
* Set value of offset
*
* @param mixed $offset Offset
* @param mixed $value Value
*
* @return void
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->properties[$offset] = $value;
}

/**
* Unset value of offset
*
* @param mixed $offset Offset
*
* @return void
*/
public function offsetUnset(mixed $offset): void
{
unset($this->properties[$offset]);
}

/**
* Return string value
*
* @return string
*/
public function __toString(): string
{
return $this->string;
}
}
Loading
Loading