Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Attribute] Component and Contract #210

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ docs/ @JoshuaEstes
/src/SonsOfPHP/Bard @JoshuaEstes

# Each component/contract needs a Team
/src/SonsOfPHP/**/Attribute @JoshuaEstes
/src/SonsOfPHP/**/Cache @JoshuaEstes
/src/SonsOfPHP/**/Clock @JoshuaEstes
/src/SonsOfPHP/**/Container @JoshuaEstes
Expand Down
5 changes: 5 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ Bard:
- docs/bard/*
- src/SonsOfPHP/Bard/*

Attribute:
- docs/components/attribute/*
- docs/contracts/attribute/*
- src/SonsOfPHP/**/Attribute/*

Cache:
- docs/components/cache/*
- src/SonsOfPHP/**/Cache/*
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ To get the diff between two versions, go to https://github.com/SonsOfPHP/sonsofp

## [Unreleased]

* [PR #210](https://github.com/SonsOfPHP/sonsofphp/pull/210) Added new Attribute component and contact
* [PR #51](https://github.com/SonsOfPHP/sonsofphp/pull/51) Added new Filesystem component
* [PR #59](https://github.com/SonsOfPHP/sonsofphp/pull/59) Added new HttpMessage component
* [PR #59](https://github.com/SonsOfPHP/sonsofphp/pull/59) Added new HttpFactory component
Expand Down
8 changes: 8 additions & 0 deletions bard.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
"path": "src/SonsOfPHP/Bard",
"repository": "git@github.com:SonsOfPHP/bard.git"
},
{
"path": "src/SonsOfPHP/Contract/Attribute",
"repository": "git@github.com:SonsOfPHP/attribute-contract.git"
},
{
"path": "src/SonsOfPHP/Component/Attribute",
"repository": "git@github.com:SonsOfPHP/attribute.git"
},
{
"path": "src/SonsOfPHP/Contract/Mailer",
"repository": "git@github.com:SonsOfPHP/mailer-contract.git"
Expand Down
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"psr/http-server-handler-implementation": "^1.0",
"psr/http-server-middleware-implementation": "^1.0",
"sonsofphp/http-handler-implementation": "0.3.x-dev",
"sonsofphp/mailer-implementation": "0.3.x-dev"
"sonsofphp/mailer-implementation": "0.3.x-dev",
"sonsofphp/attribute-implementation": "0.3.x-dev"
},
"require": {
"php": ">=8.1",
Expand Down Expand Up @@ -127,11 +128,15 @@
"sonsofphp/http-handler": "self.version",
"sonsofphp/http-handler-contract": "self.version",
"sonsofphp/mailer-contract": "self.version",
"sonsofphp/mailer": "self.version"
"sonsofphp/mailer": "self.version",
"sonsofphp/attribute-contract": "self.version",
"sonsofphp/attribute": "self.version"
},
"autoload": {
"psr-4": {
"SonsOfPHP\\Bard\\": "src/SonsOfPHP/Bard/src",
"SonsOfPHP\\Contract\\Attribute\\": "src/SonsOfPHP/Contract/Attribute",
"SonsOfPHP\\Component\\Attribute\\": "src/SonsOfPHP/Component/Attribute",
"SonsOfPHP\\Contract\\Mailer\\": "src/SonsOfPHP/Contract/Mailer",
"SonsOfPHP\\Component\\Mailer\\": "src/SonsOfPHP/Component/Mailer",
"SonsOfPHP\\Component\\Cache\\": "src/SonsOfPHP/Component/Cache",
Expand Down Expand Up @@ -174,6 +179,7 @@
},
"exclude-from-classmap": [
"src/SonsOfPHP/Bard/Tests",
"src/SonsOfPHP/Component/Attribute/Tests",
"src/SonsOfPHP/Component/Mailer/Tests",
"src/SonsOfPHP/Component/Cache/Tests",
"src/SonsOfPHP/Component/Clock/Tests",
Expand Down
15 changes: 15 additions & 0 deletions docs/contracts/attribute/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Attribute Contracts - Overview
description: PHP Attribute Contracts
---

# Attribute Contracts

Attributes can be added to any object. These are used for things like a product
or customer where a user may define custom attributes on that object.

## Installation

```shell
composer require sonsofphp/attribute-contract
```
4 changes: 4 additions & 0 deletions src/SonsOfPHP/Component/Attribute/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/SonsOfPHP/Component/Attribute/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
73 changes: 73 additions & 0 deletions src/SonsOfPHP/Component/Attribute/Attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Attribute;

use SonsOfPHP\Contract\Attribute\AttributeInterface;
use SonsOfPHP\Contract\Attribute\AttributeTypeInterface;

/**
* @author Joshua Estes <joshua@sonsofphp.com>
*/
class Attribute implements AttributeInterface, \Stringable
{
protected ?string $code = null;
protected ?string $name = null;
protected int $position = 0;
protected ?AttributeTypeInterface $type = null;

public function __toString(): string
{
return (string) $this->getName();
}

public function getCode(): ?string
{
return $this->code;
}

public function setCode(?string $code): static
{
// Normalize value
$this->code = strtolower((string) $code);

return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(?string $name): static
{
$this->name = $name;

return $this;
}

public function getPosition(): int
{
return $this->position;
}

public function setPosition(int $position): static
{
$this->position = $position;

return $this;
}

public function getType(): ?AttributeTypeInterface
{
return $this->type;
}

public function setType(AttributeTypeInterface $type): static
{
$this->type = $type;

return $this;
}
}
40 changes: 40 additions & 0 deletions src/SonsOfPHP/Component/Attribute/AttributeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Attribute;

use SonsOfPHP\Contract\Attribute\AttributeTypeInterface;

/**
* @author Joshua Estes <joshua@sonsofphp.com>
*/
enum AttributeType: string implements AttributeTypeInterface
{
case TYPE_TEXT = 'text';
// -- OR --
case TextType = 'text';
// -- OR --
case Text = 'text';

//case TextareaType = 'textarea';
//case CheckboxType = 'checkbook';
//case IntegerType = 'integer';
//case FloatType = 'float';
//case DatetimeType = 'datetime';
//case DateType = 'date';
//case SelectType = 'select';

public function getDisplayName(): string
{
return match($this) {
self::TYPE_TEXT => 'Text',
default => 'Unknown',
};
}

public function getType()
{
return $this;
}
}
71 changes: 71 additions & 0 deletions src/SonsOfPHP/Component/Attribute/AttributeValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Attribute;

use SonsOfPHP\Contract\Attribute\AttributeInterface;
use SonsOfPHP\Contract\Attribute\AttributeSubjectInterface;
use SonsOfPHP\Contract\Attribute\AttributeTypeInterface;
use SonsOfPHP\Contract\Attribute\AttributeValueInterface;

/**
* @author Joshua Estes <joshua@sonsofphp.com>
*/
class AttributeValue implements AttributeValueInterface
{
protected ?AttributeSubjectInterface $subject = null;
protected ?AttributeInterface $attribute = null;
protected $value = null;

public function getSubject(): ?AttributeSubjectInterface
{
return $this->subject;
}

public function setSubject(?AttributeSubjectInterface $subject): static
{
$this->subject = $subject;

return $this;
}

public function getAttribute(): ?AttributeInterface
{
return $this->attribute;
}

public function setAttribute(?AttributeInterface $attribute): static
{
$this->attribute = $attribute;

return $this;
}

public function getType(): ?AttributeTypeInterface
{
return $this->getAttribute()?->getType();
}

public function getValue()
{
return $this->value;
}

public function setValue($value): static
{
$this->value = $value;

return $this;
}

public function getCode(): ?string
{
return $this->getAttribute()?->getCode();
}

public function getName(): ?string
{
return $this->getAttribute()?->getName();
}
}
19 changes: 19 additions & 0 deletions src/SonsOfPHP/Component/Attribute/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright 2022 to Present Joshua Estes

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 16 additions & 0 deletions src/SonsOfPHP/Component/Attribute/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Sons of PHP - Attribute
=======================

## Learn More

* [Documentation][docs]
* [Contributing][contributing]
* [Report Issues][issues] and [Submit Pull Requests][pull-requests] in the [Mother Repository][mother-repo]
* Get Help & Support using [Discussions][discussions]

[discussions]: https://github.com/orgs/SonsOfPHP/discussions
[mother-repo]: https://github.com/SonsOfPHP/sonsofphp
[contributing]: https://docs.sonsofphp.com/contributing/
[docs]: https://docs.sonsofphp.com/components/attribute/
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues?q=is%3Aopen+is%3Aissue+label%3AAttribute
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls?q=is%3Aopen+is%3Apr+label%3AAttribute
63 changes: 63 additions & 0 deletions src/SonsOfPHP/Component/Attribute/Tests/AttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Attribute\Tests;

use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Attribute\Attribute;
use SonsOfPHP\Contract\Attribute\AttributeInterface;

/**
* @coversDefaultClass \SonsOfPHP\Component\Attribute\Attribute
*/
final class AttributeTest extends TestCase
{
private $model;

protected function setUp(): void
{
$this->model = new Attribute();
}

/**
* @coversNothing
*/
public function testItHasTheCorrectInterfaces(): void
{
$this->assertInstanceOf(AttributeInterface::class, $this->model);
}

/**
* @covers ::getCode
* @covers ::setCode
*/
public function testCode(): void
{
$this->assertNull($this->model->getCode());
$this->model->setCode('sku');
$this->assertSame('sku', $this->model->getCode());
}

/**
* @covers ::getName
* @covers ::setName
*/
public function testName(): void
{
$this->assertNull($this->model->getName());
$this->model->setName('Test Attribute');
$this->assertSame('Test Attribute', $this->model->getName());
}

/**
* @covers ::getPosition
* @covers ::setPosition
*/
public function testPosition(): void
{
$this->assertSame(0, $this->model->getPosition());
$this->model->setPosition(100);
$this->assertSame(100, $this->model->getPosition());
}
}
Loading
Loading