aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/JsonApiIntegration/Parser.php
blob: 6e460d9a9917117f629f77751a84728d4f22fc8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

namespace JsonApi\JsonApiIntegration;

use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
use Neomerx\JsonApi\Contracts\Parser\EditableContextInterface;
use Neomerx\JsonApi\Exceptions\InvalidArgumentException;
use Neomerx\JsonApi\Parser\Parser as NeomerxParser;
use SimpleORMap;
use function Neomerx\JsonApi\I18n\format as _;

/**
 * Eine Instanz von Neomerx\JsonApi\Encoder\Parser\Parser wird
 * benötigt, um Werte, die an den JSON-API-Encoder gehen, zu
 * analysieren und entsprechned weiter zu verarbeiten. Unter anderem
 * wird darin auch die Unterscheidung getroffen, ob Werte, die an den
 * JSON-API-Encoder gehen, Collections sind oder nicht.
 *
 * Bei dieser Analyse werden sinnvollerweise alle Werte, die das
 * PHP-Interface \IteratorAggregate implementieren, als Collections
 * behandelt. Da aber die Stud.IP-Klasse \SimpleORMap
 * ungewöhnlicherweise ebenfalls dieses Interface implementiert, muss
 * hier eine Sonderbehandlung stattfinden.
 *
 * Dazu wird die Methode
 * Neomerx\JsonApi\Encoder\Parser\Parser::analyzeCurrentData so
 * überschrieben, dass Instanzen von \SimpleORMap nicht als
 * Collections gelten.
 *
 * @see Neomerx\JsonApi\Parser\Parser
 * @see \SimpleORMap
 */
class Parser extends NeomerxParser
{
    /**
     * @var SchemaContainerInterface
     */
    private $schemaContainer;

    /**
     * As `$schemaContainer` is private in \Neomerx\JsonApi\Parser\Parser it has
     * to be stored again in this subclass.
     *
     * @param FactoryInterface         $factory
     * @param SchemaContainerInterface $container
     * @param EditableContextInterface $context
     */
    public function __construct(
        FactoryInterface $factory,
        SchemaContainerInterface $container,
        EditableContextInterface $context
    ) {
        $this->schemaContainer = $container;

        parent::__construct($factory, $container, $context);
    }

    /**
     * Show better error messages using instances of subclasses of \SimpleORMap
     * without a Schema.
     *
     * @inheritdoc
     */
    public function parse($data, array $paths = []): iterable
    {
        \assert(\is_array($data) === true || \is_object($data) === true || $data === null);

        if ($data instanceof SimpleORMap && $this->schemaContainer->hasSchema($data) !== true) {
            throw new InvalidArgumentException(_(static::MSG_NO_SCHEMA_FOUND, \get_class($data)));
        }

        return parent::parse($data, $paths);
    }
}