Skip to content

Commit f786bfc

Browse files
committed
Introduce Utils\AccessAbstract
1 parent 0a43633 commit f786bfc

16 files changed

+191
-175
lines changed

src/Attributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @see http://jsonapi.org/format/#document-resource-object-attributes
1212
*/
13-
class Attributes extends Meta implements AccessInterface
13+
class Attributes extends Meta
1414
{
1515
/**
1616
* @var FactoryManagerInterface

src/Document.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Art4\JsonApiClient;
44

5+
use Art4\JsonApiClient\Utils\AccessAbstract;
56
use Art4\JsonApiClient\Utils\AccessTrait;
67
use Art4\JsonApiClient\Utils\MetaTrait;
78
use Art4\JsonApiClient\Utils\LinksTrait;
@@ -14,7 +15,7 @@
1415
*
1516
* @see http://jsonapi.org/format/#document-top-level
1617
*/
17-
class Document implements AccessInterface
18+
class Document extends AccessAbstract
1819
{
1920
use AccessTrait;
2021

src/Error.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Art4\JsonApiClient;
44

5+
use Art4\JsonApiClient\Utils\AccessAbstract;
56
use Art4\JsonApiClient\Utils\AccessTrait;
67
use Art4\JsonApiClient\Utils\MetaTrait;
78
use Art4\JsonApiClient\Utils\LinksTrait;
@@ -14,7 +15,7 @@
1415
*
1516
* @see http://jsonapi.org/format/#error-objects
1617
*/
17-
class Error implements AccessInterface
18+
class Error extends AccessAbstract
1819
{
1920
use AccessTrait;
2021

src/ErrorCollection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Art4\JsonApiClient;
44

5+
use Art4\JsonApiClient\Utils\AccessAbstract;
56
use Art4\JsonApiClient\Utils\AccessTrait;
67
use Art4\JsonApiClient\Utils\FactoryManagerInterface;
78
use Art4\JsonApiClient\Exception\AccessException;
@@ -12,7 +13,7 @@
1213
*
1314
* @see http://jsonapi.org/format/#error-objects
1415
*/
15-
class ErrorCollection implements AccessInterface
16+
class ErrorCollection extends AccessAbstract
1617
{
1718
use AccessTrait;
1819

src/ErrorSource.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Art4\JsonApiClient;
44

5+
use Art4\JsonApiClient\Utils\AccessAbstract;
56
use Art4\JsonApiClient\Utils\AccessTrait;
67
use Art4\JsonApiClient\Utils\FactoryManagerInterface;
78
use Art4\JsonApiClient\Exception\AccessException;
@@ -12,7 +13,7 @@
1213
*
1314
* @see http://jsonapi.org/format/#error-objects
1415
*/
15-
class ErrorSource implements AccessInterface
16+
class ErrorSource extends AccessAbstract
1617
{
1718
use AccessTrait;
1819

src/Jsonapi.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Art4\JsonApiClient;
44

5+
use Art4\JsonApiClient\Utils\AccessAbstract;
56
use Art4\JsonApiClient\Utils\AccessTrait;
67
use Art4\JsonApiClient\Utils\FactoryManagerInterface;
78
use Art4\JsonApiClient\Utils\MetaTrait;
@@ -13,7 +14,7 @@
1314
*
1415
* @see http://jsonapi.org/format/#document-jsonapi-object
1516
*/
16-
class Jsonapi implements AccessInterface
17+
class Jsonapi extends AccessAbstract
1718
{
1819
use AccessTrait;
1920

src/Link.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Art4\JsonApiClient;
44

5+
use Art4\JsonApiClient\Utils\AccessAbstract;
56
use Art4\JsonApiClient\Utils\AccessTrait;
67
use Art4\JsonApiClient\Utils\FactoryManagerInterface;
78
use Art4\JsonApiClient\Utils\MetaTrait;
@@ -13,7 +14,7 @@
1314
*
1415
* @see http://jsonapi.org/format/#document-links
1516
*/
16-
class Link implements AccessInterface
17+
class Link extends AccessAbstract
1718
{
1819
use AccessTrait;
1920

src/Meta.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Art4\JsonApiClient;
44

5+
use Art4\JsonApiClient\Utils\AccessAbstract;
56
use Art4\JsonApiClient\Utils\AccessTrait;
67
use Art4\JsonApiClient\Utils\FactoryManagerInterface;
78
use Art4\JsonApiClient\Exception\AccessException;
@@ -12,7 +13,7 @@
1213
*
1314
* @see http://jsonapi.org/format/#document-meta
1415
*/
15-
class Meta implements AccessInterface
16+
class Meta extends AccessAbstract
1617
{
1718
use AccessTrait;
1819

src/Relationship.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Art4\JsonApiClient;
44

5+
use Art4\JsonApiClient\Utils\AccessAbstract;
56
use Art4\JsonApiClient\Utils\AccessTrait;
67
use Art4\JsonApiClient\Utils\MetaTrait;
78
use Art4\JsonApiClient\Utils\LinksTrait;
@@ -14,7 +15,7 @@
1415
*
1516
* @see http://jsonapi.org/format/#document-resource-object-relationships
1617
*/
17-
class Relationship implements AccessInterface
18+
class Relationship extends AccessAbstract
1819
{
1920
use AccessTrait;
2021

src/RelationshipCollection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Art4\JsonApiClient;
44

5+
use Art4\JsonApiClient\Utils\AccessAbstract;
56
use Art4\JsonApiClient\Utils\AccessTrait;
67
use Art4\JsonApiClient\Utils\FactoryManagerInterface;
78
use Art4\JsonApiClient\Resource\ResourceInterface;
@@ -13,7 +14,7 @@
1314
*
1415
* @see http://jsonapi.org/format/#document-resource-object-relationships
1516
*/
16-
class RelationshipCollection implements AccessInterface
17+
class RelationshipCollection extends AccessAbstract
1718
{
1819
use AccessTrait;
1920

src/Resource/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Art4\JsonApiClient\Resource;
44

5-
use Art4\JsonApiClient\AccessInterface;
5+
use Art4\JsonApiClient\Utils\AccessAbstract;
66
use Art4\JsonApiClient\Utils\AccessTrait;
77
use Art4\JsonApiClient\Utils\FactoryManagerInterface;
88
use Art4\JsonApiClient\Exception\AccessException;
@@ -13,7 +13,7 @@
1313
*
1414
* @see http://jsonapi.org/format/#document-resource-objects
1515
*/
16-
class Collection implements AccessInterface, ResourceInterface
16+
class Collection extends AccessAbstract implements ResourceInterface
1717
{
1818
use AccessTrait;
1919

src/Resource/Identifier.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Art4\JsonApiClient\Resource;
44

5-
use Art4\JsonApiClient\AccessInterface;
5+
use Art4\JsonApiClient\Utils\AccessAbstract;
66
use Art4\JsonApiClient\Utils\AccessTrait;
77
use Art4\JsonApiClient\Utils\FactoryManagerInterface;
88
use Art4\JsonApiClient\Utils\MetaTrait;
@@ -14,7 +14,7 @@
1414
*
1515
* @see http://jsonapi.org/format/#document-resource-identifier-objects
1616
*/
17-
class Identifier implements AccessInterface, ResourceInterface
17+
class Identifier extends AccessAbstract implements ResourceInterface
1818
{
1919
use AccessTrait;
2020

src/Resource/IdentifierCollection.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
namespace Art4\JsonApiClient\Resource;
44

5-
use Art4\JsonApiClient\AccessInterface;
6-
use Art4\JsonApiClient\Utils\AccessTrait;
7-
use Art4\JsonApiClient\Utils\FactoryManagerInterface;
8-
use Art4\JsonApiClient\Exception\AccessException;
9-
use Art4\JsonApiClient\Exception\ValidationException;
10-
115
/**
126
* A Identifier Collection holds only Identifier
137
*/

src/Resource/Item.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Art4\JsonApiClient\Resource;
44

5-
use Art4\JsonApiClient\AccessInterface;
65
use Art4\JsonApiClient\Utils\LinksTrait;
76
use Art4\JsonApiClient\Utils\FactoryManagerInterface;
87
use Art4\JsonApiClient\Exception\AccessException;

src/Utils/AccessAbstract.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
namespace Art4\JsonApiClient\Utils;
4+
5+
use Art4\JsonApiClient\AccessInterface;
6+
use Art4\JsonApiClient\Exception\AccessException;
7+
8+
/**
9+
* Abstract class for AccessInterface
10+
*/
11+
abstract class AccessAbstract implements AccessInterface
12+
{
13+
abstract public function getKeys();
14+
15+
abstract protected function getValue($key);
16+
17+
abstract protected function hasValue($key);
18+
19+
/**
20+
* Check if a value exists
21+
*
22+
* @param mixed $key The key
23+
* @return boolean
24+
*/
25+
public function has($key)
26+
{
27+
$key = $this->parseKey($key);
28+
29+
$string = $key->shift();
30+
$key->next();
31+
32+
if ( $key->count() === 0 )
33+
{
34+
return $this->hasValue($string);
35+
}
36+
37+
if ( ! $this->hasValue($string) )
38+
{
39+
return false;
40+
}
41+
42+
$value = $this->getValue($string);
43+
44+
// #TODO Handle other objects an arrays
45+
if ( ! $value instanceof AccessInterface )
46+
{
47+
//throw new AccessException('The existance for the key "' . $key->raw . '" could\'nt be checked.');
48+
return false;
49+
}
50+
51+
return $value->has($key);
52+
}
53+
54+
/**
55+
* Get a value by a key
56+
*
57+
* @param mixed $key The key
58+
* @return mixed
59+
*/
60+
public function get($key)
61+
{
62+
$key = $this->parseKey($key);
63+
64+
$string = $key->shift();
65+
$key->next();
66+
67+
$value = $this->getValue($string);
68+
69+
if ( $key->count() === 0 )
70+
{
71+
return $value;
72+
}
73+
74+
if ( ! $value instanceof AccessInterface )
75+
{
76+
throw new AccessException('Could not get the value for the key "' . $key->raw . '".');
77+
}
78+
79+
return $value->get($key);
80+
}
81+
82+
/**
83+
* Convert this object in an array
84+
*
85+
* @param bool $fullArray If true, objects are transformed into arrays recursively
86+
* @return array
87+
*/
88+
public function asArray($fullArray = false)
89+
{
90+
$return = array();
91+
92+
foreach($this->getKeys() as $key)
93+
{
94+
$val = $this->get($key);
95+
96+
if ( $fullArray )
97+
{
98+
$return[$key] = $this->objectTransform($val);
99+
}
100+
else
101+
{
102+
$return[$key] = $val;
103+
}
104+
}
105+
106+
return $return;
107+
}
108+
109+
/**
110+
* Parse a dot.notated.key to an object
111+
*
112+
* @param string|\SplStack $key The key
113+
* @return \SplStack The parsed key
114+
*/
115+
protected function parseKey($key)
116+
{
117+
if ( is_object($key) and $key instanceof \SplStack )
118+
{
119+
return $key;
120+
}
121+
122+
// Handle arrays and objects
123+
if ( is_object($key) or is_array($key) )
124+
{
125+
$key = '';
126+
}
127+
128+
$key_string = strval($key);
129+
130+
$keys = explode('.', $key_string);
131+
132+
$key = new \SplStack;
133+
$key->raw = $key_string;
134+
135+
foreach ( $keys as $value )
136+
{
137+
$key->push($value);
138+
}
139+
140+
$key->rewind();
141+
142+
return $key;
143+
}
144+
145+
/**
146+
* Transforms objects to arrays
147+
*
148+
* @param $val
149+
* @return mixed
150+
*/
151+
protected function objectTransform($val)
152+
{
153+
if ( ! is_object($val) )
154+
{
155+
return $val;
156+
}
157+
elseif ( $val instanceOf AccessInterface )
158+
{
159+
return $val->asArray(true);
160+
}
161+
else
162+
{
163+
// Fallback for stdClass objects
164+
return json_decode(json_encode($val), true);
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)