• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mixerapi / mixerapi-dev / 7434143287

06 Jan 2024 09:54PM UTC coverage: 94.68% (-1.1%) from 95.793%
7434143287

Pull #141

github

web-flow
Merge 4d1f37519 into 39576a822
Pull Request #141: CakePHP 5 support

961 of 1015 relevant lines covered (94.68%)

4.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.44
/plugins/json-ld-view/src/JsonLdEntityContext.php
1
<?php
2
declare(strict_types=1);
3

4
namespace MixerApi\JsonLdView;
5

6
use MixerApi\Core\Model\Model;
7

8
/**
9
 * Provides context for a JSON-LD entity. This class will document schema.org values from the entities properties when
10
 * possible.
11
 *
12
 * @uses SchemaMapper
13
 */
14
class JsonLdEntityContext
15
{
16
    private Model $model;
17

18
    private array $data = [];
19

20
    /**
21
     * @param \MixerApi\Core\Model\Model $model an instance of Model
22
     */
23
    public function __construct(Model $model)
24
    {
25
        $this->model = $model;
3✔
26
    }
27

28
    /**
29
     * Returns $this->data which is a key-value array containing a property, schema.org url, and an optional
30
     * description.
31
     *
32
     * @return array
33
     */
34
    public function build(): array
35
    {
36
        $this->assignFromInterface();
3✔
37
        $this->assignFromValidations();
3✔
38

39
        return $this->data;
3✔
40
    }
41

42
    /**
43
     * @return void
44
     */
45
    private function assignFromInterface(): void
46
    {
47
        $entity = $this->model->getEntity();
3✔
48
        if (!$entity instanceof JsonLdDataInterface) {
3✔
49
            return;
×
50
        }
51

52
        $schemas = $entity->getJsonLdSchemas();
3✔
53
        foreach ($schemas as $schema) {
3✔
54
            $name = $schema->getProperty();
3✔
55
            $this->data[$name] = $schema->getSchemaUrl();
3✔
56
        }
57
    }
58

59
    /**
60
     * @return void
61
     */
62
    private function assignFromValidations(): void
63
    {
64
        foreach ($this->model->getProperties() as $property) {
3✔
65
            $name = $property->getName();
3✔
66
            if (isset($this->data[$name])) {
3✔
67
                continue;
3✔
68
            }
69

70
            $url = SchemaMapper::findSchemaFromModelProperty($property);
3✔
71

72
            if (is_string($url)) {
3✔
73
                $this->data[$name] = $url;
3✔
74
            }
75
        }
76
    }
77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc