-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'labels' into post-builder
Merged the 'labels' feature branch into 'post-builder', which implements the `SelfLabels` lexicon.
- Loading branch information
Showing
5 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Atproto\Lexicons\Com\Atproto\Label; | ||
|
||
use Atproto\Contracts\Stringable; | ||
use Atproto\Exceptions\InvalidArgumentException; | ||
use GenericCollection\GenericCollection; | ||
use JsonSerializable; | ||
|
||
class SelfLabels extends GenericCollection implements JsonSerializable, Stringable | ||
{ | ||
private const MAXLENGTH = 10; | ||
private const MAXLENGTH_BY_ITEM = 128; | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function __construct(iterable $collection = []) | ||
{ | ||
$this->collection = $collection; | ||
|
||
try { | ||
parent::__construct($this->validator(), $collection); | ||
} catch (\TypeError|\GenericCollection\Exceptions\InvalidArgumentException $e) { | ||
$this->throw($e); | ||
} | ||
} | ||
|
||
private function validator(): \Closure | ||
{ | ||
return function (string $val): bool { | ||
if ($this->count() + 1 > self::MAXLENGTH) { | ||
throw new InvalidArgumentException("Maximum allowed length is " . self::MAXLENGTH); | ||
} | ||
|
||
if (strlen($val) > self::MAXLENGTH_BY_ITEM) { | ||
throw new InvalidArgumentException("Length exceeded for $val. Max ".self::MAXLENGTH_BY_ITEM." characters allowed."); | ||
} | ||
|
||
return true; | ||
}; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
private function throw($exception): void | ||
{ | ||
throw new InvalidArgumentException( | ||
str_replace( | ||
"::".__NAMESPACE__."\{closure}()", | ||
"", | ||
$exception->getMessage() | ||
), | ||
0, | ||
$exception | ||
); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return json_encode($this); | ||
} | ||
|
||
public function jsonSerialize() | ||
{ | ||
return $this->toArray(); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return array_map(fn (string $val) => ['val' => $val], $this->collection); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Lexicons\Com\Atproto\Label; | ||
|
||
use Atproto\Exceptions\InvalidArgumentException; | ||
use Atproto\Lexicons\Com\Atproto\Label\SelfLabels; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
class SelfLabelsTest extends TestCase | ||
{ | ||
private SelfLabels $selfLabels; | ||
private int $maxlength = 10; | ||
private int $maxlengthByItem = 128; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->selfLabels = new SelfLabels(); | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function test__constructFillsDataCorrectly(): void | ||
{ | ||
$expected = array_map(fn (string $val) => ['val' => $val], ['val 1', 'val 2', 'val 3']); | ||
|
||
$this->selfLabels = new SelfLabels(array_column( | ||
$expected, | ||
'val' | ||
)); | ||
|
||
$this->assertEquals($expected, $this->selfLabels->toArray()); | ||
} | ||
|
||
public function test__constructorThrowsInvalidArgumentExceptionWhenPassedInvalidArgument(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage("must be of the type string"); | ||
|
||
$this->selfLabels = new SelfLabels(['val 1', new stdClass()]); | ||
} | ||
|
||
public function test__constructThrowsInvalidArgumentExceptionWhenLimitExceeded(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage("Maximum allowed length is ".$this->maxlength); | ||
|
||
$trigger = str_split(str_pad('', ++$this->maxlength, 'a')); | ||
$this->selfLabels = new SelfLabels($trigger); | ||
} | ||
|
||
public function test__constructorThrowsExceptionWhenPassedArgumentThatExceedsLimit(): void | ||
{ | ||
$trigger = [str_pad('', ++$this->maxlengthByItem, 'a')]; | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage("Length exceeded for " . current($trigger)); | ||
|
||
$this->selfLabels = new SelfLabels($trigger); | ||
} | ||
|
||
public function testAddThrowsExceptionWhenPassedArgumentExceedsMaxLengthLimit(): void | ||
{ | ||
$trigger = str_split(str_pad('', ++$this->maxlength, 'a')); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage("Maximum allowed length is ".--$this->maxlength); | ||
|
||
foreach($trigger as $item) { | ||
$this->selfLabels[] = $item; | ||
} | ||
} | ||
} |