-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add trait for common tasks in entities * Fix bug where entity tried to create entity with already created entities in prop * Add convenience methods
- Loading branch information
Showing
16 changed files
with
652 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Avolle\Fotballdata\Entity; | ||
|
||
/** | ||
* Helper methods for entities | ||
*/ | ||
trait EntityHelperTrait | ||
{ | ||
/** | ||
* Convert a property name's value into a datetime string | ||
* | ||
* @param string $property Property name to convert date | ||
* @return string | ||
*/ | ||
public function toDate(string $property): string | ||
{ | ||
#/Date(1650578307030-0000)/ | ||
$pattern = '/\/Date\((\d+)-(\d+)\)\//'; | ||
if (!isset($this->$property) || !preg_match($pattern, $this->$property, $matches)) { | ||
return 'Unknown'; | ||
} | ||
[, $timeInMilliseconds] = $matches; | ||
|
||
return date('Y-m-d H:i:s', (int)($timeInMilliseconds / 1000)); | ||
} | ||
|
||
/** | ||
* Returns an array of name parts for a given full name. | ||
* Makes a best-guess where only the last part of the name is determined to be the surname, and the rest first name. | ||
* | ||
* If $indexed = false: | ||
* - Key 0: First name | ||
* - Key 1: Surname | ||
* | ||
* If $indexed = true | ||
* - Key `firstName` = First name | ||
* - Key `surname` = Surname | ||
* | ||
* @return array<string> | ||
*/ | ||
public function toNameParts(string $fullName, bool $indexed = false): array | ||
{ | ||
$firstName = substr($fullName, 0, strrpos($fullName, ' ')); | ||
$surname = substr($fullName, strrpos($fullName, ' ') + 1, strlen($fullName)); | ||
|
||
if ($indexed) { | ||
return compact('firstName', 'surname'); | ||
} | ||
|
||
return [$firstName, $surname]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ | |
*/ | ||
class Season extends Entity | ||
{ | ||
use EntityHelperTrait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
*/ | ||
class Stadium extends Entity | ||
{ | ||
use EntityHelperTrait; | ||
|
||
/* | ||
* @inheritdoc | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,8 @@ | |
*/ | ||
class Tournament extends Entity | ||
{ | ||
use EntityHelperTrait; | ||
|
||
/* | ||
* @inheritdoc | ||
*/ | ||
|
Oops, something went wrong.