Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
Show position (#38)
Browse files Browse the repository at this point in the history
* Added .php_cs.dist for php-cs-fixer

* Added phpstan with config file

* Position in show field

* Removed invalid config

* Added new exception class for ShowReader

* Updated doc

* Added dump() function

* Fixed logic error with show

* Added position to list

* Updated doc

* Added name var

* Added name var

* Removed dev packages

* Reverted dev packages

* Updated travis

https://lorenzo.mile.si/travis-ci-and-composer-out-of-memory/739/

* Travis function fix

* Source travis_retry

* Removed travis_retry

* Downgraded var-dumper version for Symfony 3.4

* Changed var-dumper version
  • Loading branch information
lukepass committed Jul 5, 2019
1 parent cff80d3 commit 815042a
Show file tree
Hide file tree
Showing 17 changed files with 372 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/vendor
composer.lock
.php_cs.*
12 changes: 12 additions & 0 deletions .php_cs.dist
@@ -0,0 +1,12 @@
<?php

return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'binary_operator_spaces' => [
'default' => 'align_single_space_minimal',
],
])
;

// vim: syntax=php
2 changes: 1 addition & 1 deletion .travis/install_test.sh
Expand Up @@ -12,4 +12,4 @@ wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coverall
chmod u+x "${HOME}/bin/coveralls"

if [ "$SYMFONY" != "" ]; then composer require "symfony/symfony:$SYMFONY" --no-update; fi;
composer install --dev --prefer-dist
COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-dist --no-interaction
19 changes: 15 additions & 4 deletions README.md
Expand Up @@ -307,6 +307,10 @@ class Category

### ShowField

You are able to set the position of the field. Position 1 would be the first
field to render and higher numbers after. If field doesn't have position, it
will be rendered after all fields with position.

```php
<?php

Expand All @@ -326,7 +330,8 @@ class Category
/**
* @Sonata\ShowField(
* type="",
* fieldDescriptionOptions={}
* fieldDescriptionOptions={},
* position=1
* )
*
* @ORM\Column(name="name", type="string", length=255)
Expand Down Expand Up @@ -371,7 +376,8 @@ class Category
* @Sonata\ShowAssociationField(
* field="email",
* type="",
* fieldDescriptionOptions={}
* fieldDescriptionOptions={},
* position=1
* )
*
* @ORM\ManyToOne(targetEntity="Owner")
Expand All @@ -383,6 +389,10 @@ class Category

### ListField

You are able to set the position of the field. Position 1 would be the first
field to render and higher numbers after. If field doesn't have position, it
will be rendered after all fields with position.

```php
<?php

Expand Down Expand Up @@ -412,15 +422,16 @@ class Category
* @Sonata\ListField(
* type="",
* fieldDescriptionOptions={},
* identifier=false
* identifier=false,
* position=1
* )
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;

/**
* @Sonata\ListField()
* @Sonata\ListField(position=1)
*/
public function listThis(): string
{
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -39,9 +39,11 @@
"behat/symfony2-extension": "^2.1",
"doctrine/doctrine-fixtures-bundle": "^3.0",
"matthiasnoback/symfony-dependency-injection-test": "^3.0",
"phpstan/phpstan-phpunit": "^0.11.2",
"sonata-project/doctrine-orm-admin-bundle": "^3.6",
"symfony/framework-bundle": "^3.4 || ^4.0",
"symfony/phpunit-bridge": "^4.0"
"symfony/phpunit-bridge": "^4.0",
"symfony/var-dumper": "^3.4 || ^4.0"
},
"config": {
"sort-packages": true
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
@@ -0,0 +1,5 @@
parameters:
ignoreErrors:
- '#on an unknown class Prophecy#'
autoload_files:
- vendor/bin/.phpunit/phpunit-7.4/vendor/autoload.php
1 change: 0 additions & 1 deletion phpunit.xml.dist
Expand Up @@ -8,7 +8,6 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
Expand Down
15 changes: 15 additions & 0 deletions src/Annotation/ListField.php
Expand Up @@ -16,4 +16,19 @@ class ListField extends AbstractField
* @var bool
*/
public $identifier = false;

/**
* @var int
*/
public $position;

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

public function hasPosition(): bool
{
return null !== $this->position;
}
}
14 changes: 14 additions & 0 deletions src/Annotation/ShowField.php
Expand Up @@ -12,4 +12,18 @@
*/
class ShowField extends AbstractField
{
/**
* @var int
*/
public $position;

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

public function hasPosition(): bool
{
return null !== $this->position;
}
}
88 changes: 79 additions & 9 deletions src/Reader/ListReader.php
Expand Up @@ -18,33 +18,102 @@ final class ListReader

public function configureFields(\ReflectionClass $class, ListMapper $listMapper): void
{
$propertiesAndMethodsWithPosition = [];
$propertiesAndMethodsWithoutPosition = [];

//
// Properties
//

foreach ($class->getProperties() as $property) {
foreach ($this->getPropertyAnnotations($property) as $annotation) {
if (!$annotation instanceof ListField && !$annotation instanceof ListAssociationField) {
continue;
}

// the name property changes for ListAssociationField
$name = $property->getName();
if ($annotation instanceof ListAssociationField) {
$this->addField(
$property->getName() . '.' . $annotation->getField(),
$annotation,
$listMapper
);
$name .= '.'.$annotation->getField();
}

if (!$annotation->hasPosition()) {
$propertiesAndMethodsWithoutPosition[] = [
'name' => $name,
'annotation' => $annotation,
];

continue;
}

if ($annotation instanceof ListField) {
$this->addField($property->getName(), $annotation, $listMapper);
if (\array_key_exists($annotation->position, $propertiesAndMethodsWithPosition)) {
throw new \InvalidArgumentException(sprintf(
'Position "%s" is already in use by "%s", try setting a different position for "%s".',
$annotation->position,
$propertiesAndMethodsWithPosition[$annotation->position]['name'],
$property->getName()
));
}

$propertiesAndMethodsWithPosition[$annotation->position] = [
'name' => $name,
'annotation' => $annotation,
];
}
}

//
// Methods
//

foreach ($class->getMethods() as $method) {
if ($annotation = $this->getMethodAnnotation($method, ListField::class)) {
$this->addField($method->getName(), $annotation, $listMapper);
$name = $method->getName();

if (!$annotation->hasPosition()) {
$propertiesAndMethodsWithoutPosition[] = [
'name' => $name,
'annotation' => $annotation,
];

continue;
}

if (\array_key_exists($annotation->position, $propertiesAndMethodsWithPosition)) {
throw new \InvalidArgumentException(sprintf(
'Position "%s" is already in use by "%s", try setting a different position for "%s".',
$annotation->position,
$propertiesAndMethodsWithPosition[$annotation->position]['name'],
$name
));
}

$propertiesAndMethodsWithPosition[$annotation->position] = [
'name' => $name,
'annotation' => $annotation,
];
}
}

//
// Sorting
//

\ksort($propertiesAndMethodsWithPosition);

$propertiesAndMethods = \array_merge($propertiesAndMethodsWithPosition, $propertiesAndMethodsWithoutPosition);

foreach ($propertiesAndMethods as $propertyAndMethod) {
$this->addField($propertyAndMethod['name'], $propertyAndMethod['annotation'], $listMapper);
}

//
// Actions
//

if ($actions = $this->getListActions($this->getClassAnnotations($class))) {
$listMapper->add('_action', null, [
'actions' => $actions
'actions' => $actions,
]);
}
}
Expand All @@ -53,6 +122,7 @@ private function addField(string $name, ListField $annotation, ListMapper $listM
{
if ($annotation->identifier) {
$listMapper->addIdentifier($name, ...$annotation->getSettings());

return;
}

Expand Down
80 changes: 73 additions & 7 deletions src/Reader/ShowReader.php
Expand Up @@ -17,27 +17,93 @@ final class ShowReader

public function configureFields(\ReflectionClass $class, ShowMapper $showMapper): void
{
$propertiesAndMethodsWithPosition = [];
$propertiesAndMethodsWithoutPosition = [];

//
// Properties
//

foreach ($class->getProperties() as $property) {
foreach ($this->getPropertyAnnotations($property) as $annotation) {
if (!$annotation instanceof ShowField && !$annotation instanceof ShowAssociationField) {
continue;
}

// the name property changes for ShowAssociationField
$name = $property->getName();
if ($annotation instanceof ShowAssociationField) {
$showMapper->add(
$property->getName() . '.' . $annotation->getField(),
...$annotation->getSettings()
);
$name .= '.'.$annotation->getField();
}

if (!$annotation->hasPosition()) {
$propertiesAndMethodsWithoutPosition[] = [
'name' => $name,
'settings' => $annotation->getSettings(),
];

continue;
}

if ($annotation instanceof ShowField) {
$showMapper->add($property->getName(), ...$annotation->getSettings());
if (\array_key_exists($annotation->position, $propertiesAndMethodsWithPosition)) {
throw new \InvalidArgumentException(sprintf(
'Position "%s" is already in use by "%s", try setting a different position for "%s".',
$annotation->position,
$propertiesAndMethodsWithPosition[$annotation->position]['name'],
$property->getName()
));
}

$propertiesAndMethodsWithPosition[$annotation->position] = [
'name' => $name,
'settings' => $annotation->getSettings(),
];
}
}

//
// Methods
//

foreach ($class->getMethods() as $method) {
if ($annotation = $this->getMethodAnnotation($method, ShowField::class)) {
$showMapper->add($method->getName(), ...$annotation->getSettings());
$name = $method->getName();

if (!$annotation->hasPosition()) {
$propertiesAndMethodsWithoutPosition[] = [
'name' => $name,
'settings' => $annotation->getSettings(),
];

continue;
}

if (\array_key_exists($annotation->position, $propertiesAndMethodsWithPosition)) {
throw new \InvalidArgumentException(sprintf(
'Position "%s" is already in use by "%s", try setting a different position for "%s".',
$annotation->position,
$propertiesAndMethodsWithPosition[$annotation->position]['name'],
$name
));
}

$propertiesAndMethodsWithPosition[$annotation->position] = [
'name' => $name,
'settings' => $annotation->getSettings(),
];
}
}

//
// Sorting
//

\ksort($propertiesAndMethodsWithPosition);

$propertiesAndMethods = \array_merge($propertiesAndMethodsWithPosition, $propertiesAndMethodsWithoutPosition);

foreach ($propertiesAndMethods as $propertyAndMethod) {
$showMapper->add($propertyAndMethod['name'], ...$propertyAndMethod['settings']);
}
}
}

0 comments on commit 815042a

Please sign in to comment.