Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
14 changes: 13 additions & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
checks:
php:
type_analyzer_migration_checks: false
argument_type_checks: false
code_rating: true
duplication: true
fix_doc_comments: false
no_exit: false
unused_parameters: false
unused_properties: false
unused_variables: false
use_statement_alias_conflict: false
verify_property_names: false

build:
image: default-jammy
environment:
php: 8.4.11
php:
version: 8.4.11
ini:
memory_limit: "512M"
nodes:
coverage:
services:
Expand Down
3 changes: 2 additions & 1 deletion src/Controllers/Media/Endpoints/Thumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function handle(...$arguments): ResponseInterface
return $response;
}

if (preg_match('/^(\d+)x(\d+)(x([0-9A-F]{6})?)?$/i', $this->handler->peekPath(), $matches)) {
$size = $this->handler->peekPath();
if (is_string($size) && preg_match('/^(\d+)x(\d+)(x([0-9A-F]{6})?)?$/i', $size, $matches)) {
$this->handler->shiftPath();
$maxWidth = $matches[1];
$maxHeight = $matches[2];
Expand Down
1 change: 1 addition & 0 deletions src/Controllers/MediaRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Divergence\Controllers;

use Exception;
use Divergence\Controllers\Media\Endpoints\Browse;
use Divergence\Controllers\Media\Endpoints\Caption;
use Divergence\Controllers\Media\Endpoints\Create;
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Records/Endpoints/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function handle(...$arguments): ResponseInterface
if (!$Record) {
$className = $this->handler::$recordClass;
$defaultClass = $className::getDefaultClassName();
$Record = new $defaultClass();
$Record = $defaultClass::create();
}

$this->handler->onRecordCreatedHook($Record, $_REQUEST);
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Records/Endpoints/MultiSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function getDatumRecord($datum)

if (empty($datum[$PrimaryKey])) {
$defaultClass = $className::getDefaultClassName();
$record = new $defaultClass();
$record = $defaultClass::create();
$this->handler->onRecordCreatedHook($record, $datum);

return $record;
Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use Divergence\App;
use Divergence\Responders\Response;
use BadMethodCallException;
use Error;
use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -87,7 +87,7 @@ public function __call(string $name, array $arguments)
$endpointName = strtolower($name);

if (!isset($this->endpointClasses[$endpointName])) {
throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', static::class, $name));
throw new Error(sprintf('Call to undefined method %s::%s()', static::class, $name));
}

if (!isset($this->endpoints[$endpointName])) {
Expand Down
212 changes: 212 additions & 0 deletions src/Data/Collections/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php
/**
* This file is part of the Divergence package.
*
* (c) Henry Paradiz <henry.paradiz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Divergence\Data\Collections;

use Iterator;
use Countable;
use ArrayAccess;

/**
* @template TRecord of object|array
* @implements Iterator<array-key, TRecord>
* @implements ArrayAccess<array-key, TRecord>
*/
class Collection implements Iterator, Countable, ArrayAccess, Indexing
{
use Getters;

public static $addHandler;
public static $addManyHandler;
public static $removeHandler;
public static $removeManyHandler;
public static $createIndexByFieldHandler;
public static $hasIndexHandler;
public static $updateIndexForModelHandler;
public static $setIndexesHandler;
public static $clearIndexesHandler;

/** @var array<array-key, TRecord> */
public array $Index = [];

/** @var array<string, IndexedField> */
public array $Indexes = [];

/** @var array<int, TRecord> */
public array $HashKeyIndex = [];

public int $position = 0;

/**
* @param array<array-key, TRecord> $records
* @param array<int, string> $indexes
*/
public function __construct(array $records = [], array $indexes = [])
{
foreach ($indexes as $field) {
$this->createIndexByField($field);
}

$this->addMany($records);
}

public function validate($record)
{
return true;
}

public function add($record)
{
$handler = static::$addHandler;
$handler::handle($this, $record);
}

public function addMany(array $records)
{
$handler = static::$addManyHandler;
$handler::handle($this, $records);
}

public function remove($record)
{
$handler = static::$removeHandler;
$handler::handle($this, $record);
}

public function removeMany($records)
{
$handler = static::$removeManyHandler;
$handler::handle($this, $records);
}

public function toArray()
{
return $this->Index;
}

/* ### Implements IndexedFields Internally in the Collection ### */

public function createIndexByField($field)
{
$handler = static::$createIndexByFieldHandler;
$handler::handle($this, $field);
}

public function hasIndex($field): bool
{
$handler = static::$hasIndexHandler;
return $handler::handle($this, $field);
}

public function updateIndexForModel($index, &$record)
{
$handler = static::$updateIndexForModelHandler;
$handler::handle($this, $index, $record);
}

public function setIndexes(&$record)
{
$handler = static::$setIndexesHandler;
$handler::handle($this, $record);
}

public function clearIndexes(&$record)
{
$handler = static::$clearIndexesHandler;
$handler::handle($this, $record);
}

/* ### START implements Countable { ### */

public function count(): int
{
return count($this->Index);
}

/* ### } END implements Countable ### */

/* ### START implements Iterator { ### */

public function current(): mixed
{
return $this->Index[$this->position] ?? null;
}

public function key(): int
{
return $this->position;
}

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

public function rewind(): void
{
$this->position = 0;
}

public function valid(): bool
{
return isset($this->Index[$this->position]);
}

/* ### } END implements Iterator ### */

/* ### START implements ArrayAccess { ### */

public function offsetSet(mixed $offset, mixed $value): void
{
if (is_null($offset)) {
$this->add($value);
} elseif ($this->validate($value)) {
if (isset($this->Index[$offset])) {
$position = $this->position;
$this->offsetUnset($offset);
array_splice($this->Index, $offset, 0, [$value]);
$this->position = $position;
} else {
$this->Index[$offset] = $value;
}

$this->setIndexes($value);
}
}

public function offsetExists(mixed $offset): bool
{
return isset($this->Index[$offset]);
}

public function offsetUnset(mixed $offset): void
{
if (isset($this->Index[$offset])) {
$record = $this->Index[$offset];
$recordKey = RecordKey::get($record);
unset($this->HashKeyIndex[$recordKey]);
$this->clearIndexes($record);
array_splice($this->Index, $offset, 1);

if ($this->position > $offset) {
--$this->position;
}
}
}

public function offsetGet(mixed $offset): mixed
{
if ($offset < 0) {
$offset += $this->count();
}
return $this->Index[$offset] ?? null;
}

/* ### } END implements ArrayAccess ### */
}
17 changes: 17 additions & 0 deletions src/Data/Collections/Events/AbstractHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is part of the Divergence package.
*
* (c) Henry Paradiz <henry.paradiz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Divergence\Data\Collections\Events;

use Divergence\Data\Collections\Collection;

abstract class AbstractHandler
{
abstract public static function handle(Collection $collection);
}
34 changes: 34 additions & 0 deletions src/Data/Collections/Events/Add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* This file is part of the Divergence package.
*
* (c) Henry Paradiz <henry.paradiz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Divergence\Data\Collections\Events;

use Divergence\Data\Collections\Collection;
use Divergence\Data\Collections\Indexing;
use Divergence\Data\Collections\RecordKey;

class Add extends AbstractHandler
{
public static function handle(Collection $collection, $record = null): void
{
if ($collection->validate($record)) {
$recordKey = RecordKey::get($record);

if (isset($collection->HashKeyIndex[$recordKey])) {
return;
}

array_push($collection->Index, $record);

if ($collection instanceof Indexing) {
$collection->setIndexes($record);
}
}
}
}
22 changes: 22 additions & 0 deletions src/Data/Collections/Events/AddMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* This file is part of the Divergence package.
*
* (c) Henry Paradiz <henry.paradiz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Divergence\Data\Collections\Events;

use Divergence\Data\Collections\Collection;

class AddMany extends AbstractHandler
{
public static function handle(Collection $collection, array $records = []): void
{
foreach ($records as $record) {
$collection->add($record);
}
}
}
Loading