| @@ 11-100 (lines=90) @@ | ||
| 8 | use phootwork\lang\Text; |
|
| 9 | use phootwork\collection\CollectionUtils; |
|
| 10 | ||
| 11 | class Paths implements Arrayable { |
|
| 12 | ||
| 13 | use ExtensionPart; |
|
| 14 | ||
| 15 | /** @var Map */ |
|
| 16 | private $paths; |
|
| 17 | ||
| 18 | public function __construct($contents = null) { |
|
| 19 | $this->parse($contents === null ? new Map() : $contents); |
|
| 20 | } |
|
| 21 | ||
| 22 | private function parse($contents) { |
|
| 23 | $data = CollectionUtils::toMap($contents); |
|
| 24 | ||
| 25 | // paths |
|
| 26 | $this->paths = new Map(); |
|
| 27 | foreach ($data as $p => $path) { |
|
| 28 | if (!Text::create($p)->startsWith('x-')) { |
|
| 29 | $this->paths->set($p, new Path($p, $path)); |
|
| 30 | } |
|
| 31 | } |
|
| 32 | ||
| 33 | // extensions |
|
| 34 | $this->parseExtensions($data); |
|
| 35 | } |
|
| 36 | ||
| 37 | public function toArray() { |
|
| 38 | $paths = clone $this->paths; |
|
| 39 | $paths->setAll($this->getExtensions()); |
|
| 40 | return $paths->toArray(); |
|
| 41 | } |
|
| 42 | ||
| 43 | public function size() { |
|
| 44 | return $this->paths->size(); |
|
| 45 | } |
|
| 46 | ||
| 47 | /** |
|
| 48 | * Returns whether a path with the given name exists |
|
| 49 | * |
|
| 50 | * @param string $path |
|
| 51 | * @return boolean |
|
| 52 | */ |
|
| 53 | public function has($path) { |
|
| 54 | return $this->paths->has($path); |
|
| 55 | } |
|
| 56 | ||
| 57 | /** |
|
| 58 | * Returns whether the given path exists |
|
| 59 | * |
|
| 60 | * @param Path $path |
|
| 61 | * @return boolean |
|
| 62 | */ |
|
| 63 | public function contains(Path $path) { |
|
| 64 | return $this->paths->contains($path); |
|
| 65 | } |
|
| 66 | ||
| 67 | /** |
|
| 68 | * Returns the path info for the given path |
|
| 69 | * |
|
| 70 | * @param string $path |
|
| 71 | * @return Path |
|
| 72 | */ |
|
| 73 | public function get($path) { |
|
| 74 | if (!$this->paths->has($path)) { |
|
| 75 | $this->paths->set($path, new Path($path)); |
|
| 76 | } |
|
| 77 | return $this->paths->get($path); |
|
| 78 | } |
|
| 79 | ||
| 80 | /** |
|
| 81 | * Sets the path |
|
| 82 | * |
|
| 83 | * @param Path $path |
|
| 84 | * @return $this |
|
| 85 | */ |
|
| 86 | public function add(Path $path) { |
|
| 87 | $this->paths->set($path->getPath(), $path); |
|
| 88 | return $this; |
|
| 89 | } |
|
| 90 | ||
| 91 | /** |
|
| 92 | * Removes the given path |
|
| 93 | * |
|
| 94 | * @param string $path |
|
| 95 | */ |
|
| 96 | public function remove($path) { |
|
| 97 | $this->paths->remove($path); |
|
| 98 | return $this; |
|
| 99 | } |
|
| 100 | } |
|
| 101 | ||
| @@ 11-98 (lines=88) @@ | ||
| 8 | use phootwork\lang\Arrayable; |
|
| 9 | use phootwork\lang\Text; |
|
| 10 | ||
| 11 | class Responses implements Arrayable { |
|
| 12 | ||
| 13 | use ExtensionPart; |
|
| 14 | ||
| 15 | /** @var Map */ |
|
| 16 | private $responses; |
|
| 17 | ||
| 18 | public function __construct($contents = null) { |
|
| 19 | $this->parse($contents === null ? new Map() : $contents); |
|
| 20 | } |
|
| 21 | ||
| 22 | private function parse($contents) { |
|
| 23 | $data = CollectionUtils::toMap($contents); |
|
| 24 | ||
| 25 | // responses |
|
| 26 | $this->responses = new Map(); |
|
| 27 | foreach ($data as $r => $response) { |
|
| 28 | if (!Text::create($r)->startsWith('x-')) { |
|
| 29 | $this->responses->set($r, new Response($r, $response)); |
|
| 30 | } |
|
| 31 | } |
|
| 32 | ||
| 33 | // extensions |
|
| 34 | $this->parseExtensions($data); |
|
| 35 | } |
|
| 36 | ||
| 37 | public function toArray() { |
|
| 38 | $responses = clone $this->responses; |
|
| 39 | $responses->setAll($this->getExtensions()); |
|
| 40 | return $responses->toArray(); |
|
| 41 | } |
|
| 42 | ||
| 43 | public function size() { |
|
| 44 | return $this->responses->size(); |
|
| 45 | } |
|
| 46 | ||
| 47 | /** |
|
| 48 | * Returns whether the given response exists |
|
| 49 | * |
|
| 50 | * @param string $code |
|
| 51 | * @return boolean |
|
| 52 | */ |
|
| 53 | public function has($code) { |
|
| 54 | return $this->responses->has($code); |
|
| 55 | } |
|
| 56 | ||
| 57 | /** |
|
| 58 | * Returns whether the given response exists |
|
| 59 | * |
|
| 60 | * @param Response $response |
|
| 61 | * @return boolean |
|
| 62 | */ |
|
| 63 | public function contains(Response $response) { |
|
| 64 | return $this->responses->contains($response); |
|
| 65 | } |
|
| 66 | ||
| 67 | /** |
|
| 68 | * Returns the reponse info for the given code |
|
| 69 | * |
|
| 70 | * @param string $code |
|
| 71 | * @return Response |
|
| 72 | */ |
|
| 73 | public function get($code) { |
|
| 74 | if (!$this->responses->has($code)) { |
|
| 75 | $this->responses->set($code, new Response($code)); |
|
| 76 | } |
|
| 77 | ||
| 78 | return $this->responses->get($code); |
|
| 79 | } |
|
| 80 | ||
| 81 | /** |
|
| 82 | * Sets the response |
|
| 83 | * |
|
| 84 | * @param Response $code |
|
| 85 | */ |
|
| 86 | public function add(Response $response) { |
|
| 87 | $this->responses->set($response->getCode(), $response); |
|
| 88 | } |
|
| 89 | ||
| 90 | /** |
|
| 91 | * Removes the given repsonse |
|
| 92 | * |
|
| 93 | * @param string $code |
|
| 94 | */ |
|
| 95 | public function remove($code) { |
|
| 96 | $this->responses->remove($code); |
|
| 97 | } |
|
| 98 | } |
|
| 99 | ||