Skip to content

Commit b6a2e4c

Browse files
author
Anton Shevchuk
committed
Initial release
Moved package to the individual repository
1 parent 9e68622 commit b6a2e4c

12 files changed

Lines changed: 664 additions & 1 deletion

LICENSE renamed to LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 Bluz Framework
3+
Copyright (c) Bluz Framework
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Container Component
2+
## Achievements
3+
4+
[![PHP >= 8.0+](https://img.shields.io/packagist/php-v/bluzphp/container.svg?style=flat)](https://php.net/)
5+
6+
[![Latest Stable Version](https://img.shields.io/packagist/v/bluzphp/container.svg?label=version&style=flat)](https://packagist.org/packages/bluzphp/container)
7+
8+
[![Build Status](https://img.shields.io/scrutinizer/build/g/bluzphp/container)](https://scrutinizer-ci.com/g/bluzphp/container/build-status/master)
9+
10+
[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/bluzphp/container.svg?style=flat)](https://scrutinizer-ci.com/g/bluzphp/container/)
11+
12+
[![Total Downloads](https://img.shields.io/packagist/dt/bluzphp/container.svg?style=flat)](https://packagist.org/packages/bluzphp/container)
13+
14+
[![License](https://img.shields.io/packagist/l/bluzphp/container.svg?style=flat)](https://packagist.org/packages/bluzphp/container)
15+
16+
## Usage
17+
18+
Example of the Registry class:
19+
20+
```
21+
namespace Bluz\Registry;
22+
23+
use Bluz\Container;
24+
25+
class Registry {
26+
use Container\Container;
27+
use Container\JsonSerialize;
28+
use Container\RegularAccess;
29+
}
30+
```
31+
32+
### Methods
33+
34+
Trait `Container\Container`:
35+
36+
* `setFromArray(array $data)`
37+
* `toArray()`
38+
* `resetArray()`
39+
40+
Methods of the `Container\ArrayAccess` (implementation of the interface `ArrayAccess`):
41+
* `offsetSet($offset, $value)`
42+
* `offsetExists($offset)`
43+
* `offsetUnset($offset)`
44+
* `offsetGet($offset)`
45+
46+
Methods of the `Container\MagicAccess`:
47+
* `__set($key, $value)`
48+
* `__get($key)`
49+
* `__isset($key)`
50+
* `__unset($key)`
51+
52+
Methods of the `Container\RegularAccess`:
53+
* `set($key, $value)`
54+
* `get($key)` - implementation of the PSR-11: Container interface
55+
* `has($key)` - implementation of the PSR-11: Container interface
56+
* `remove($key)`
57+
58+
Methods of the `Container\JsonSerialize`
59+
* `jsonSerialize()` - implementation of the interface `JsonSerializable`

composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "bluzphp/container",
3+
"description": "Container package",
4+
"type": "library",
5+
"require": {
6+
"php": ">=8.0"
7+
},
8+
"require-dev": {
9+
"phpunit/phpunit": "~9.5",
10+
"php-coveralls/php-coveralls": "~2.5",
11+
"squizlabs/php_codesniffer": "~3.7",
12+
"phploc/phploc": "~7.0"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"Bluz\\": "src/"
17+
}
18+
},
19+
"autoload-dev": {
20+
"psr-4": {
21+
"Bluz\\Tests\\": "tests/"
22+
}
23+
},
24+
"authors": [
25+
{
26+
"name": "Bluz Framework Contributors",
27+
"homepage": "https://github.com/bluzphp/framework/graphs/contributors"
28+
}
29+
],
30+
"support": {
31+
"issues": "https://github.com/bluzphp/framework/issues",
32+
"wiki": "https://github.com/bluzphp/framework/wiki"
33+
},
34+
"license": "MIT"
35+
}

phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
stopOnFailure="false"
7+
colors="true"
8+
verbose="true"
9+
bootstrap="./tests/_bootstrap.php" >
10+
<coverage processUncoveredFiles="true">
11+
<include>
12+
<directory suffix=".php">./src/</directory>
13+
</include>
14+
<exclude>
15+
<directory suffix=".php">./vendor/</directory>
16+
</exclude>
17+
</coverage>
18+
<testsuites>
19+
<testsuite name="Container Test Suite">
20+
<directory>./tests/</directory>
21+
</testsuite>
22+
</testsuites>
23+
</phpunit>

src/Container/ArrayAccess.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/**
4+
* Bluz Framework Component
5+
*
6+
* @copyright Bluz PHP Team
7+
* @link https://github.com/bluzphp/framework
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Bluz\Container;
13+
14+
use InvalidArgumentException;
15+
16+
/**
17+
* Container implements ArrayAccess
18+
*
19+
* @package Bluz\Common
20+
* @author Anton Shevchuk
21+
* @see ArrayAccess
22+
*
23+
* @method void doSetContainer(string $key, mixed $value)
24+
* @method mixed doGetContainer(string $key)
25+
* @method bool doContainsContainer(string $key)
26+
* @method void doDeleteContainer(string $key)
27+
*/
28+
trait ArrayAccess
29+
{
30+
/**
31+
* Offset to set
32+
*
33+
* @param mixed $offset
34+
* @param mixed $value
35+
*
36+
* @throws InvalidArgumentException
37+
*/
38+
public function offsetSet(mixed $offset, mixed $value): void
39+
{
40+
if (null === $offset) {
41+
throw new InvalidArgumentException('Class `Common\Container\ArrayAccess` support only associative arrays');
42+
}
43+
$this->doSetContainer($offset, $value);
44+
}
45+
46+
/**
47+
* Offset to retrieve
48+
*
49+
* @param mixed $offset
50+
*
51+
* @return mixed
52+
*/
53+
public function offsetGet(mixed $offset): mixed
54+
{
55+
return $this->doGetContainer($offset);
56+
}
57+
58+
/**
59+
* Whether a offset exists
60+
*
61+
* @param mixed $offset
62+
*
63+
* @return bool
64+
*/
65+
public function offsetExists(mixed $offset): bool
66+
{
67+
return $this->doContainsContainer($offset);
68+
}
69+
70+
/**
71+
* Offset to unset
72+
*
73+
* @param mixed $offset
74+
*/
75+
public function offsetUnset(mixed $offset): void
76+
{
77+
$this->doDeleteContainer($offset);
78+
}
79+
}

src/Container/Container.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/**
4+
* Bluz Framework Component
5+
*
6+
* @copyright Bluz PHP Team
7+
* @link https://github.com/bluzphp/framework
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Bluz\Container;
13+
14+
/**
15+
* Container of data for object
16+
*
17+
* @package Bluz\Common
18+
* @author Anton Shevchuk
19+
* @link https://github.com/bluzphp/framework/wiki/Trait-Container
20+
*/
21+
trait Container
22+
{
23+
/**
24+
* @var array Container of elements
25+
*/
26+
protected array $container = [];
27+
28+
/**
29+
* Set key/value pair
30+
*
31+
* @param string $key
32+
* @param mixed $value
33+
*
34+
* @return void
35+
*/
36+
protected function doSetContainer(string $key, mixed $value): void
37+
{
38+
$this->container[$key] = $value;
39+
}
40+
41+
/**
42+
* Get value by key
43+
*
44+
* @param string $key
45+
*
46+
* @return mixed
47+
*/
48+
protected function doGetContainer(string $key): mixed
49+
{
50+
if ($this->doContainsContainer($key)) {
51+
return $this->container[$key];
52+
}
53+
return null;
54+
}
55+
56+
/**
57+
* Check contains key in container
58+
*
59+
* @param string $key
60+
*
61+
* @return bool
62+
*/
63+
protected function doContainsContainer(string $key): bool
64+
{
65+
return array_key_exists($key, $this->container);
66+
}
67+
68+
/**
69+
* Delete value by key
70+
*
71+
* @param string $key
72+
*
73+
* @return void
74+
*/
75+
protected function doDeleteContainer(string $key): void
76+
{
77+
unset($this->container[$key]);
78+
}
79+
80+
/**
81+
* Sets all data in the row from an array
82+
*
83+
* @param array $data
84+
*
85+
* @return void
86+
*/
87+
public function setFromArray(array $data): void
88+
{
89+
foreach ($data as $key => $value) {
90+
$this->container[$key] = $value;
91+
}
92+
}
93+
94+
/**
95+
* Returns the column/value data as an array
96+
*
97+
* @return array
98+
*/
99+
public function toArray(): array
100+
{
101+
return $this->container;
102+
}
103+
104+
/**
105+
* Reset container array
106+
*
107+
* @return void
108+
*/
109+
public function resetArray(): void
110+
{
111+
foreach ($this->container as &$value) {
112+
$value = null;
113+
}
114+
}
115+
}

src/Container/JsonSerialize.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* Bluz Framework Component
5+
*
6+
* @copyright Bluz PHP Team
7+
* @link https://github.com/bluzphp/framework
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Bluz\Container;
13+
14+
/**
15+
* Container implements JsonSerializable interface
16+
*
17+
* @package Bluz\Common
18+
* @author Anton Shevchuk
19+
* @see JsonSerializable
20+
*/
21+
trait JsonSerialize
22+
{
23+
/**
24+
* Specify data which should be serialized to JSON
25+
*
26+
* @return array
27+
*/
28+
public function jsonSerialize(): mixed
29+
{
30+
return $this->toArray();
31+
}
32+
}

0 commit comments

Comments
 (0)