Skip to content

Commit

Permalink
Add array schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
romanzipp committed Oct 12, 2020
1 parent 7525433 commit 05fb189
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/Conductors/ArrayStructures/AbstractArraySchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace romanzipp\Seo\Conductors\ArrayStructures;

use Closure;

abstract class AbstractArraySchema
{
/**
* @var string
*/
protected $class;

/**
* @var \Closure
*/
protected $callback;

public function __construct(?string $class = null)
{
$this->class = $class;
}

/**
* @param string|null $class
* @return static
*/
public static function make(?string $class = null)
{
return new static($class);
}

/**
* @param \Closure $callback
* @return static
*/
public function callback(Closure $callback)
{
$this->callback = $callback;

return $this;
}

/**
* Get the callback.
*
* @return \Closure
*/
public function getCallback(): Closure
{
return $this->callback;
}

protected function call(array $parameters)
{
call_user_func(
$this->getCallback(),
...$parameters
);
}

abstract public function acceptsSingleValue(): bool;

abstract public function apply($data);
}
35 changes: 35 additions & 0 deletions src/Conductors/ArrayStructures/AttributeArraySchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace romanzipp\Seo\Conductors\ArrayStructures;

use InvalidArgumentException;

class AttributeArraySchema extends AbstractArraySchema
{
public function apply($data)
{
if ( ! is_array($data)) {
throw new InvalidArgumentException('Invalid argument supplied for attribute array schema');
}

foreach ($data as $attributes) {
$this->call([
new $this->class,
$attributes,
]);
}
}

protected function getCallbackParameters(): array
{
return [
new $this->class,

];
}

public function acceptsSingleValue(): bool
{
return false;
}
}
24 changes: 24 additions & 0 deletions src/Conductors/ArrayStructures/NestedArraySchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace romanzipp\Seo\Conductors\ArrayStructures;

use InvalidArgumentException;

class NestedArraySchema extends AbstractArraySchema
{
public function apply($data)
{
if ( ! is_array($data)) {
throw new InvalidArgumentException('Invalid argument supplied for nested array schema');
}

foreach ($data as $key => $value) {
$this->call([$key, $value]);
}
}

public function acceptsSingleValue(): bool
{
return false;
}
}
24 changes: 24 additions & 0 deletions src/Conductors/ArrayStructures/SingleArraySchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace romanzipp\Seo\Conductors\ArrayStructures;

use InvalidArgumentException;

class SingleArraySchema extends AbstractArraySchema
{
public function apply($value)
{
if ( ! is_string($value)) {
throw new InvalidArgumentException('Invalid argument supplied for single array schema');
}

$this->call([
$value,
]);
}

public function acceptsSingleValue(): bool
{
return true;
}
}

0 comments on commit 05fb189

Please sign in to comment.