Skip to content

Commit

Permalink
exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreLebedel committed Oct 23, 2024
1 parent 248dc2e commit 6697fcc
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 21 deletions.
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
"php": "^8.1"
},
"require-dev": {
"laravel/pint": "^1.0",
"pestphp/pest": "^2.20",
"spatie/ray": "^1.28",
"symfony/var-dumper": "^7.1"
"laravel/pint": "^v1.0",
"pestphp/pest": "^v2.0|^v3.0",
"symfony/var-dumper": "^v6.2|^7.0"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 16 additions & 0 deletions src/Exceptions/VCardIOArrayAccessException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Pleb\VCardIO\Exceptions;

class VCardIOArrayAccessException extends VCardIOException
{
public static function invalidIndex(?string $message = null)
{
return new self($message ?? 'Invalid index');
}

public static function invalidValue(?string $message = null)
{
return new self($message ?? 'Invalid value');
}
}
5 changes: 5 additions & 0 deletions src/Exceptions/VCardIOException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Pleb\VCardIO\Exceptions;

abstract class VCardIOException extends \Exception {}
11 changes: 11 additions & 0 deletions src/Exceptions/VCardIOIteratorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Pleb\VCardIO\Exceptions;

class VCardIOIteratorException extends VCardIOException
{
public static function invalidIndex(?string $message = null)
{
return new self($message ?? 'Invalid index');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Pleb\VCardIO\Exceptions;

class VCardIOParseException extends \Exception
class VCardIOParserException extends VCardIOException
{
public static function fileNotFound(string $filePath)
{
Expand Down
128 changes: 112 additions & 16 deletions src/VCardParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,133 @@

namespace Pleb\VCardIO;

use Pleb\VCardIO\Exceptions\VCardIOParseException;
use Pleb\VCardIO\Exceptions\VCardIOArrayAccessException;
use Pleb\VCardIO\Exceptions\VCardIOIteratorException;
use Pleb\VCardIO\Exceptions\VCardIOParserException;
use Pleb\VCardIO\Models\VCard;

class VCardParser
class VCardParser implements \ArrayAccess, \Countable, \Iterator
{
/**
* Parse vCard file and return array of VCard objects.
*
* @return array<VCard>
*/
public static function parseFile(string $filePath): array
protected array $vCards;

protected int $iteratorKey;

public function __construct(string $rawData)
{
$this->rewind();

$this->vCards = [
new VCard,
new VCard,
];
}

public static function parseFile(string $filePath): self
{
if (! file_exists($filePath)) {
throw VCardIOParseException::fileNotFound($filePath);
throw VCardIOParserException::fileNotFound($filePath);
}
if (! is_readable($filePath)) {
throw VCardIOParseException::fileUnreadable($filePath);
throw VCardIOParserException::fileUnreadable($filePath);
}

return self::parseRaw(file_get_contents($filePath));
}

public static function parseRaw(string $rawData): self
{
return new self($rawData);
}

public function getVCards(): array
{
return $this->vCards;
}

public function getVCard(int $index): VCard
{
if (! array_key_exists($index, $this->vCards)) {
throw new \OutOfBoundsException('Invalid index');
}

return $this->vCards[$index];
}

/**
* Countable interface implementation
*/
public function count(): int
{
return count($this->vCards);
}

/**
* Iterator interface implementation
*/
public function rewind(): void
{
$this->iteratorKey = 0;
}

public function current(): mixed
{
if (! $this->valid()) {
throw VCardIOIteratorException::invalidIndex();
}

return $this->vCards[$this->key()] ?? null;
}

public function key(): mixed
{
return $this->iteratorKey;
}

public function next(): void
{
$this->iteratorKey++;
}

public function valid(): bool
{
return array_key_exists($this->key(), $this->vCards);
}

/**
* Parse vCard data and return array of VCard objects.
*
* @return array<VCard>
* ArrayAccess interface implementation
*/
public static function parseRaw(string $rawData): array
public function offsetExists(mixed $offset): bool
{
$vCards = [];
return array_key_exists($offset, $this->vCards);
}

public function offsetGet(mixed $offset): mixed
{
if (! $this->offsetExists($offset)) {
throw VCardIOArrayAccessException::invalidIndex();
}

return $this->getVCard($offset);
}

public function offsetSet(mixed $offset, mixed $value): void
{
if (! is_int($offset)) {
throw VCardIOArrayAccessException::invalidIndex('Invalid interger index');
}
if (! $value instanceof VCard) {
throw VCardIOArrayAccessException::invalidValue('Invalid VCard value');
}

$this->vCards[$offset] = $value;
}

public function offsetUnset(mixed $offset): void
{
if (! $this->offsetExists($offset)) {
throw VCardIOArrayAccessException::invalidIndex();
}

return $vCards;
unset($this->vCards[$offset]);
}
}

0 comments on commit 6697fcc

Please sign in to comment.