This repository was archived by the owner on Jan 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathAbstractResource.php
More file actions
85 lines (71 loc) · 1.77 KB
/
AbstractResource.php
File metadata and controls
85 lines (71 loc) · 1.77 KB
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
76
77
78
79
80
81
82
83
84
85
<?php
/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tobscure\JsonApi;
use LogicException;
abstract class AbstractResource implements ResourceInterface
{
use LinksTrait;
use MetaTrait;
/**
* The resource type.
*
* @var string
*/
protected $type;
/**
* {@inheritdoc}
*/
public function getType()
{
return $this->type;
}
/**
* {@inheritdoc}
*/
public function getAttributes(array $fields = null)
{
return [];
}
/**
* {@inheritdoc}
*
* @throws \LogicException
*/
public function getRelationship($name)
{
$method = $this->getRelationshipMethodName($name);
if (method_exists($this, $method)) {
$relationship = $this->$method();
if ($relationship !== null && ! ($relationship instanceof Relationship)) {
throw new LogicException('Relationship method must return null or an instance of Tobscure\JsonApi\Relationship');
}
return $relationship;
}
}
/**
* Get the method name for the given relationship.
*
* snake_case and kebab-case are converted into camelCase.
*
* @param string $name
*
* @return string
*/
private function getRelationshipMethodName($name)
{
if (stripos($name, '-')) {
$name = lcfirst(implode('', array_map('ucfirst', explode('-', $name))));
}
if (stripos($name, '_')) {
$name = lcfirst(implode('', array_map('ucfirst', explode('_', $name))));
}
return $name;
}
}