|
| 1 | +<?php namespace Neomerx\Tests\JsonApi\Data; |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2015-2018 [email protected] |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +use ArrayAccess; |
| 20 | +use ArrayIterator; |
| 21 | +use IteratorAggregate; |
| 22 | + |
| 23 | +/** |
| 24 | + * This model emulates a resource which looks like an array (Traversable) with its properties. |
| 25 | + * The library should successfully distinguish arrays from resources that may act as arrays. |
| 26 | + * |
| 27 | + * A real word example of such a resource is Yii CModel https://www.yiiframework.com/doc/api/1.1/CModel |
| 28 | + * |
| 29 | + * @package Neomerx\Tests\JsonApi |
| 30 | + */ |
| 31 | +class AuthorCModel implements ArrayAccess, IteratorAggregate |
| 32 | +{ |
| 33 | + const ATTRIBUTE_ID = 'author_id'; |
| 34 | + const ATTRIBUTE_FIRST_NAME = 'first_name'; |
| 35 | + const ATTRIBUTE_LAST_NAME = 'last_name'; |
| 36 | + const LINK_COMMENTS = 'comments'; |
| 37 | + |
| 38 | + /** |
| 39 | + * Resource properties. |
| 40 | + * |
| 41 | + * @var array |
| 42 | + */ |
| 43 | + private $properties = []; |
| 44 | + |
| 45 | + /** |
| 46 | + * @param string $identity |
| 47 | + * @param string $firstName |
| 48 | + * @param string $lastName |
| 49 | + * @param array|null $comments |
| 50 | + */ |
| 51 | + public function __construct(string $identity, string $firstName, string $lastName, array $comments = null) |
| 52 | + { |
| 53 | + $this[self::ATTRIBUTE_ID] = $identity; |
| 54 | + $this[self::ATTRIBUTE_FIRST_NAME] = $firstName; |
| 55 | + $this[self::ATTRIBUTE_LAST_NAME] = $lastName; |
| 56 | + $this[self::LINK_COMMENTS] = $comments; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @inheritdoc |
| 61 | + */ |
| 62 | + public function getIterator() |
| 63 | + { |
| 64 | + return new ArrayIterator($this->properties); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @inheritdoc |
| 69 | + */ |
| 70 | + public function offsetExists($offset) |
| 71 | + { |
| 72 | + return array_key_exists($offset, $this->properties); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @inheritdoc |
| 77 | + */ |
| 78 | + public function offsetGet($offset) |
| 79 | + { |
| 80 | + return $this->properties[$offset]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @inheritdoc |
| 85 | + */ |
| 86 | + public function offsetSet($offset, $value) |
| 87 | + { |
| 88 | + $this->properties[$offset] = $value; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @inheritdoc |
| 93 | + */ |
| 94 | + public function offsetUnset($offset) |
| 95 | + { |
| 96 | + unset($this->properties[$offset]); |
| 97 | + } |
| 98 | +} |
0 commit comments