Skip to content

Commit

Permalink
Merge pull request #6 from MARCspec/analysis-XV3xOq
Browse files Browse the repository at this point in the history
Applied fixes from StyleCI
  • Loading branch information
cKlee committed Dec 4, 2015
2 parents bd16e74 + 40e13b3 commit 4194fe5
Show file tree
Hide file tree
Showing 22 changed files with 1,404 additions and 1,590 deletions.
72 changes: 35 additions & 37 deletions ComparisonString.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* from within a MARC record.
*
* @author Carsten Klee <mailme.klee@yahoo.de>
* @package CK\MARCspec
* @copyright For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand All @@ -13,98 +12,97 @@
use CK\MARCspec\Exception\InvalidMARCspecException;

/**
* A MARCspec comparison string class
*/
* A MARCspec comparison string class.
*/
class ComparisonString implements ComparisonStringInterface, \JsonSerializable, \ArrayAccess
{

/**
* @var string The escaped comparison string
*/
private $raw;

/**
*
* {@inheritdoc}
*
* @throws \InvalidArgumentException if argument is not a string or
* comparison string is not properly escaped
*/
* {@inheritdoc}
*
* @throws \InvalidArgumentException if argument is not a string or
* comparison string is not properly escaped
*/
public function __construct($raw)
{

if (!is_string($raw)) {
throw new \InvalidArgumentException('Argument must be of type string. Got '
.gettype($raw).'.');
}

if (false !== strpos($raw, ' ')) {
throw new InvalidMARCspecException(
InvalidMARCspecException::CS.
InvalidMARCspecException::SPACE,
$raw
);
}
/** char of list ${}!=~?|\s must be escaped if not at index 0*/

/* char of list ${}!=~?|\s must be escaped if not at index 0*/
if (!preg_match('/^(.(?:[^${}!=~?| ]|(?<=\\\\)[${}!=~?|])*)$/', $raw)) {
throw new InvalidMARCspecException(
InvalidMARCspecException::CS.
InvalidMARCspecException::ESCAPE,
$raw
);
}

$this->raw = $raw;
}

/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function getComparable()
{
$comparable = str_replace('\s', ' ', $this->raw);

return stripcslashes($comparable);
}

/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function getRaw()
{
return $this->raw;
}

/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public static function escape($arg)
{
$specialChars = ['{','}','!','=','~','?'];
$specialChars = ['{', '}', '!', '=', '~', '?'];
for ($i = 0; $i < count($specialChars); $i++) {
$arg = str_replace($specialChars[$i], '\\'.$specialChars[$i], $arg);
}

return $arg = str_replace(' ', '\s', $arg);
}

/**
* {@inheritdoc}
*/
public function __toString()
{
return "\\".$this->raw;
return '\\'.$this->raw;
}

/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return ['comparisonString'=>$this->raw];
return ['comparisonString' => $this->raw];
}

/**
* Access object like an associative array
* Access object like an associative array.
*
* @api
*
Expand All @@ -121,9 +119,9 @@ public function offsetExists($offset)
return false;
}
}

/**
* Access object like an associative array
* Access object like an associative array.
*
* @api
*
Expand All @@ -142,9 +140,9 @@ public function offsetGet($offset)
throw new \UnexpectedValueException("Offset $offset does not exist.");
}
}

/**
* Access object like an associative array
* Access object like an associative array.
*
* @api
*
Expand All @@ -154,9 +152,9 @@ public function offsetSet($offset, $value)
{
throw new \UnexpectedValueException("Offset $offset cannot be set.");
}

/**
* Access object like an associative array
* Access object like an associative array.
*
* @param string $offset
*/
Expand Down
81 changes: 39 additions & 42 deletions ComparisonStringInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,65 @@
* from within a MARC record.
*
* @author Carsten Klee <mailme.klee@yahoo.de>
* @package CK\MARCspec
* @copyright For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CK\MARCspec;

/**
* MARCspec comparison string interface
* MARCspec comparison string interface.
*/
interface ComparisonStringInterface
{

/**
* Constructor for ComparisonString
*
* @api
*
* @param string $raw The escaped comparison string
*/
* Constructor for ComparisonString.
*
* @api
*
* @param string $raw The escaped comparison string
*/
public function __construct($raw);

/**
* Get unescaped comparable string
*
* @api
*
* @return string The comparable string
*/
* Get unescaped comparable string.
*
* @api
*
* @return string The comparable string
*/
public function getComparable();

/**
* Get raw escaped string
*
* @api
*
* @return string The escaped string
*/
* Get raw escaped string.
*
* @api
*
* @return string The escaped string
*/
public function getRaw();

/**
* Escape a comparison string
*
* @api
*
* @param string $arg The unescaped string
*
* @return string The escaped string
*/
* Escape a comparison string.
*
* @api
*
* @param string $arg The unescaped string
*
* @return string The escaped string
*/
public static function escape($arg);

/**
* encodes ComparisonString as string
*
* @api
*
* @return string
*/
* encodes ComparisonString as string.
*
* @api
*
* @return string
*/
public function __toString();

/**
* Serialize ComparisonString as JSON
* Serialize ComparisonString as JSON.
*
* @api
*
Expand Down
Loading

0 comments on commit 4194fe5

Please sign in to comment.