Template with placeholders > verification of placeholders without values > make stronger and point out which are missing

This commit is contained in:
Meritoo
2019-04-10 08:57:33 +02:00
parent 39b0172a85
commit e66cbd54e2
8 changed files with 190 additions and 152 deletions

View File

@@ -0,0 +1,36 @@
<?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Exception\ValueObject\Template;
use Exception;
/**
* An exception used while there are missing values required to fill all placeholders in template
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
class MissingPlaceholdersInValuesException extends Exception
{
/**
* Creates an exception
*
* @param string $content Content of template
* @param array $missingPlaceholders Missing placeholders in provided values, iow. placeholders without values
* @return MissingPlaceholdersInValuesException
*/
public static function create(string $content, array $missingPlaceholders): MissingPlaceholdersInValuesException
{
$template = 'Cannot fill template \'%s\', because of missing values for placeholder(s): %s. Did you provide all'
. ' required values?';
$message = sprintf($template, $content, implode(', ', $missingPlaceholders));
return new static($message);
}
}

View File

@@ -1,39 +0,0 @@
<?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Meritoo\Common\Exception\ValueObject\Template;
use Exception;
/**
* An exception used while there is not enough values to fill all placeholders in template
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
class NotEnoughValuesException extends Exception
{
/**
* Creates an exception
*
* @param string $content Invalid content of template
* @param int $valuesCount Count of values
* @param int $placeholdersCount Count of placeholders
* @return NotEnoughValuesException
*/
public static function create(string $content, int $valuesCount, int $placeholdersCount): NotEnoughValuesException
{
$template = 'Not enough values (%d) to fill all placeholders (%d) in template \'%s\'. Did you provide all'
. ' required values?';
$message = sprintf($template, $valuesCount, $placeholdersCount, $content);
return new static($message);
}
}

View File

@@ -11,7 +11,7 @@ declare(strict_types=1);
namespace Meritoo\Common\ValueObject;
use Meritoo\Common\Exception\ValueObject\Template\InvalidContentException;
use Meritoo\Common\Exception\ValueObject\Template\NotEnoughValuesException;
use Meritoo\Common\Exception\ValueObject\Template\MissingPlaceholdersInValuesException;
/**
* Template with placeholders that may be filled by real data
@@ -54,18 +54,18 @@ class Template
* Returns content of the template filled with given values (by replacing placeholders with their proper values)
*
* @param array $values Pairs of key-value where: key - name of placeholder, value - value of the placeholder
* @throws NotEnoughValuesException
* @throws MissingPlaceholdersInValuesException
* @return string
*/
public function fill(array $values): string
{
$placeholders = static::getPlaceholders($this->content);
$valuesCount = count($values);
$placeholdersCount = count($placeholders[0]);
$providedPlaceholders = array_keys($values);
$missingPlaceholders = array_diff($placeholders[1], $providedPlaceholders);
// Oops, not enough values (iow. more placeholders than values)
if ($placeholdersCount > $valuesCount) {
throw NotEnoughValuesException::create($this->content, $valuesCount, $placeholdersCount);
// Oops, there are placeholders without values (iow. provided values are different than placeholders)
if (!empty($missingPlaceholders)) {
throw MissingPlaceholdersInValuesException::create($this->content, $missingPlaceholders);
}
$result = $this->content;