Skip to content

Commit

Permalink
Merge branch 'fix/empy-labels'
Browse files Browse the repository at this point in the history
  • Loading branch information
lukerenfrew committed Aug 12, 2021
2 parents c7d807b + 4701049 commit e662fb8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 14 deletions.
79 changes: 79 additions & 0 deletions src/GatherContent/ConfigValueObject/LegacyNotBlank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace GatherContent\ConfigValueObject;

trait LegacyNotBlank
{
/**
* Assert that value is not blank
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function notBlank($value, $message = null, $propertyPath = null)
{
if (false === $value || (empty($value) && '0' != $value)) {
$message = sprintf(
$message ?: 'Value "%s" is blank, but was expected to contain a value.',
self::stringify($value)
);

throw static::createException($value, $message, Assertion::INVALID_NOT_BLANK, $propertyPath);
}
}

/**
* Helper method that handles building the assertion failure exceptions.
* They are returned from this method so that the stack trace still shows
* the assertions method.
*/
protected static function createException($value, $message, $code, $propertyPath, array $constraints = array())
{
return new ConfigValueException($message, $code, $propertyPath, $value, $constraints);
}

/**
* Make a string version of a value.
*
* @param mixed $value
* @return string
*/
private static function stringify($value)
{
if (is_bool($value)) {
return $value ? '<TRUE>' : '<FALSE>';
}

if (is_scalar($value)) {
$val = (string)$value;

if (strlen($val) > 100) {
$val = substr($val, 0, 97) . '...';
}

return $val;
}

if (is_array($value)) {
return '<ARRAY>';
}

if (is_object($value)) {
return get_class($value);
}

if (is_resource($value)) {
return '<RESOURCE>';
}

if ($value === NULL) {
return '<NULL>';
}

return 'unknown';
}

}
15 changes: 1 addition & 14 deletions src/GatherContent/ConfigValueObject/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,7 @@

final class Validator
{
public static function notBlank($value, $message = null, string $propertyPath = null): bool
{
if (false === $value || (empty($value) && '0' != $value) ) {

$message = sprintf(
$message ?: 'Value "%s" is blank, but was expected to contain a value.',
(string) $value
);

throw new \Assert\InvalidArgumentException($value, $message, Assertion::INVALID_NOT_BLANK, $propertyPath);
}

return true;
}
use LegacyNotBlank;

public function validate($config)
{
Expand Down

0 comments on commit e662fb8

Please sign in to comment.